@@ -391,7 +391,8 @@ void drainLoop() {
391391 // when cached connections are below minimum connections, then allocate a new connection
392392 boolean belowMinConnections = minConnections > 0 &&
393393 poolConfig .allocationStrategy ().permitGranted () < minConnections ;
394- Slot slot = belowMinConnections ? null : findConnection (resources );
394+ int resourcesCount = idleSize ;
395+ Slot slot = belowMinConnections ? null : findConnection (resources , resourcesCount );
395396 if (slot != null ) {
396397 Borrower borrower = pollPending (borrowers , true );
397398 if (borrower == null || borrower .get ()) {
@@ -406,19 +407,18 @@ void drainLoop() {
406407 log .debug (format (slot .connection .channel (), "Channel activated" ));
407408 }
408409 ACQUIRED .incrementAndGet (this );
409- // Reserve concurrency and re-offer the slot before async deliver so concurrent acquires can reuse the connection
410- slot .incrementConcurrencyAndGet ();
411- slot .deactivate ();
412410 slot .connection .channel ().eventLoop ().execute (() -> {
413- borrower .deliver (new Http2PooledRef (slot ), true );
411+ borrower .deliver (new Http2PooledRef (slot ));
414412 drain ();
415413 });
416414 }
417415 else {
418- int resourcesCount = idleSize ;
419416 if (minConnections > 0 &&
420417 poolConfig .allocationStrategy ().permitGranted () >= minConnections &&
421- resourcesCount == 0 ) {
418+ poolConfig .allocationStrategy ().permitGranted () > resourcesCount ) {
419+ // connections allocations were triggered
420+ }
421+ else if (minConnections == 0 && poolConfig .allocationStrategy ().permitGranted () > resourcesCount ) {
422422 // connections allocations were triggered
423423 }
424424 else {
@@ -542,8 +542,7 @@ void evictInBackground() {
542542 scheduleEviction ();
543543 }
544544
545- @ Nullable Slot findConnection (ConcurrentLinkedQueue <Slot > resources ) {
546- int resourcesCount = idleSize ;
545+ @ Nullable Slot findConnection (ConcurrentLinkedQueue <Slot > resources , int resourcesCount ) {
547546 while (resourcesCount > 0 ) {
548547 // There are connections in the queue
549548
@@ -837,31 +836,17 @@ public String toString() {
837836 }
838837
839838 void deliver (Http2PooledRef poolSlot ) {
840- deliver (poolSlot , false );
841- }
842-
843- void deliver (Http2PooledRef poolSlot , boolean alreadyReserved ) {
844839 assert poolSlot .slot .connection .channel ().eventLoop ().inEventLoop ();
845840 poolSlot .slot .updateMaxConcurrentStreams ();
846-
847- int effectiveConcurrency = poolSlot .slot .concurrency () - (alreadyReserved ? 1 : 0 );
848- if (!poolSlot .slot .canOpenStream (effectiveConcurrency )) {
849- if (alreadyReserved ) {
850- // Concurrency was reserved in drainLoop, rollback reservation
851- poolSlot .slot .decrementConcurrencyAndGet ();
852- }
853- else {
854- poolSlot .slot .deactivate ();
855- }
841+ if (!poolSlot .slot .canOpenStream ()) {
842+ poolSlot .slot .deactivate ();
856843 pool .addPending (pool .pending , this , true );
857844 return ;
858845 }
859846 stopPendingCountdown (true );
860- if (!alreadyReserved ) {
861- // Increment concurrency BEFORE deactivate so that canOpenStream() is correct for other threads
862- poolSlot .slot .incrementConcurrencyAndGet ();
863- poolSlot .slot .deactivate ();
864- }
847+ // Increment concurrency BEFORE deactivate so that canOpenStream() is correct for other threads
848+ poolSlot .slot .incrementConcurrencyAndGet ();
849+ poolSlot .slot .deactivate ();
865850 if (get ()) {
866851 //CANCELLED or timeout reached
867852 poolSlot .invalidate ().subscribe (aVoid -> {}, e -> Operators .onErrorDropped (e , Context .empty ()));
@@ -1052,13 +1037,7 @@ private long computeMaxConcurrentStreams() {
10521037 }
10531038
10541039 boolean canOpenStream () {
1055- return canOpenStream (this .concurrency );
1056- }
1057-
1058- boolean canOpenStream (int concurrency ) {
1059- if (concurrency < 0 ) {
1060- return false ;
1061- }
1040+ int concurrency = this .concurrency ;
10621041 long max = this .maxConcurrentStreams ;
10631042 // For non-HTTP/2 connections (max == 0), allow opening a stream if concurrency is 0
10641043 // For HTTP/2 connections, check that we haven't reached max concurrent streams
0 commit comments