2828package org .apache .hc .client5 .http .impl .async ;
2929
3030import java .io .InterruptedIOException ;
31+ import java .util .concurrent .RejectedExecutionException ;
32+ import java .util .concurrent .atomic .AtomicInteger ;
3133import java .util .concurrent .atomic .AtomicReference ;
3234
3335import org .apache .hc .client5 .http .EndpointInfo ;
@@ -61,17 +63,30 @@ class InternalH2AsyncExecRuntime implements AsyncExecRuntime {
6163 private final InternalH2ConnPool connPool ;
6264 private final HandlerFactory <AsyncPushConsumer > pushHandlerFactory ;
6365 private final AtomicReference <Endpoint > sessionRef ;
66+ private final int maxQueued ;
67+ private final AtomicInteger sharedQueued ;
6468 private volatile boolean reusable ;
6569
6670 InternalH2AsyncExecRuntime (
6771 final Logger log ,
6872 final InternalH2ConnPool connPool ,
6973 final HandlerFactory <AsyncPushConsumer > pushHandlerFactory ) {
74+ this (log , connPool , pushHandlerFactory , -1 , null );
75+ }
76+
77+ InternalH2AsyncExecRuntime (
78+ final Logger log ,
79+ final InternalH2ConnPool connPool ,
80+ final HandlerFactory <AsyncPushConsumer > pushHandlerFactory ,
81+ final int maxQueued ,
82+ final AtomicInteger sharedQueued ) {
7083 super ();
7184 this .log = log ;
7285 this .connPool = connPool ;
7386 this .pushHandlerFactory = pushHandlerFactory ;
7487 this .sessionRef = new AtomicReference <>();
88+ this .maxQueued = maxQueued ;
89+ this .sharedQueued = sharedQueued ;
7590 }
7691
7792 @ Override
@@ -246,20 +261,49 @@ public EndpointInfo getEndpointInfo() {
246261 return null ;
247262 }
248263
264+ private boolean tryAcquireSlot () {
265+ if (sharedQueued == null || maxQueued <= 0 ) {
266+ return true ;
267+ }
268+ for (;;) {
269+ final int q = sharedQueued .get ();
270+ if (q >= maxQueued ) {
271+ return false ;
272+ }
273+ if (sharedQueued .compareAndSet (q , q + 1 )) {
274+ return true ;
275+ }
276+ }
277+ }
278+
279+ private void releaseSlot () {
280+ if (sharedQueued != null && maxQueued > 0 ) {
281+ sharedQueued .decrementAndGet ();
282+ }
283+ }
284+
249285 @ Override
250286 public Cancellable execute (
251287 final String id ,
252288 final AsyncClientExchangeHandler exchangeHandler , final HttpClientContext context ) {
253- final ComplexCancellable complexCancellable = new ComplexCancellable ();
254289 final Endpoint endpoint = ensureValid ();
290+ if (!tryAcquireSlot ()) {
291+ exchangeHandler .failed (new RejectedExecutionException (
292+ "Execution pipeline queue limit reached (max=" + maxQueued + ")" ));
293+ return Operations .nonCancellable ();
294+ }
295+ final AsyncClientExchangeHandler actual = sharedQueued != null
296+ ? AsyncClientExchangeHandlerProxy .newProxy (exchangeHandler , this ::releaseSlot )
297+ : exchangeHandler ;
298+ final ComplexCancellable complexCancellable = new ComplexCancellable ();
255299 final IOSession session = endpoint .session ;
256300 if (session .isOpen ()) {
257301 if (log .isDebugEnabled ()) {
258302 log .debug ("{} start execution {}" , ConnPoolSupport .getId (endpoint ), id );
259303 }
260304 context .setProtocolVersion (HttpVersion .HTTP_2 );
261305 session .enqueue (
262- new RequestExecutionCommand (exchangeHandler , pushHandlerFactory , complexCancellable , context ),
306+ new RequestExecutionCommand (actual , pushHandlerFactory , complexCancellable , context ),
263307 Command .Priority .NORMAL );
264308 } else {
265309 final HttpRoute route = endpoint .route ;
@@ -276,19 +320,19 @@ public void completed(final IOSession ioSession) {
276320 log .debug ("{} start execution {}" , ConnPoolSupport .getId (endpoint ), id );
277321 }
278322 context .setProtocolVersion (HttpVersion .HTTP_2 );
279- session .enqueue (
280- new RequestExecutionCommand (exchangeHandler , pushHandlerFactory , complexCancellable , context ),
323+ ioSession .enqueue (
324+ new RequestExecutionCommand (actual , pushHandlerFactory , complexCancellable , context ),
281325 Command .Priority .NORMAL );
282326 }
283327
284328 @ Override
285329 public void failed (final Exception ex ) {
286- exchangeHandler .failed (ex );
330+ actual .failed (ex );
287331 }
288332
289333 @ Override
290334 public void cancelled () {
291- exchangeHandler .failed (new InterruptedIOException ());
335+ actual .failed (new InterruptedIOException ());
292336 }
293337
294338 });
@@ -325,7 +369,7 @@ public String getId() {
325369
326370 @ Override
327371 public AsyncExecRuntime fork () {
328- return new InternalH2AsyncExecRuntime (log , connPool , pushHandlerFactory );
372+ return new InternalH2AsyncExecRuntime (log , connPool , pushHandlerFactory , maxQueued , sharedQueued );
329373 }
330374
331375}
0 commit comments