ok2c commented on code in PR #728:
URL: 
https://github.com/apache/httpcomponents-client/pull/728#discussion_r2573564805


##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttpAsyncExecRuntime.java:
##########
@@ -282,10 +313,115 @@ public EndpointInfo getEndpointInfo() {
         return endpoint != null ? endpoint.getInfo() : null;
     }
 
+    private boolean tryAcquireSlot() {
+        if (sharedQueued == null) {
+            return true;
+        }
+        for (; ; ) {
+            final int q = sharedQueued.get();
+            if (q >= maxQueued) {
+                return false;
+            }
+            if (sharedQueued.compareAndSet(q, q + 1)) {
+                return true;
+            }
+        }
+    }
+
+    private void releaseSlot() {
+        if (sharedQueued != null) {
+            sharedQueued.decrementAndGet();
+        }
+    }
+
+    private AsyncClientExchangeHandler guard(final AsyncClientExchangeHandler 
h) {
+        return new AsyncClientExchangeHandler() {
+            private final AtomicBoolean done = new AtomicBoolean(false);
+
+            private void finish() {
+                if (done.compareAndSet(false, true)) {
+                    releaseSlot();
+                }
+            }
+
+            @Override
+            public void failed(final Exception cause) {
+                try {
+                    h.failed(cause);
+                } finally {
+                    finish();
+                }
+            }
+
+            @Override
+            public void cancel() {
+                try {
+                    h.cancel();
+                } finally {
+                    finish();
+                }
+            }
+
+            @Override
+            public void releaseResources() {

Review Comment:
   @arturobernalg I imagine calling `#finish` in `#releaseResources` only 
should be enough and probably more semantically correct. 
   
   Can we also avoid wrapping each and every `AsyncClientExchangeHandler` 
instead? Can we do it only when `sharedQueued` is being used? By the way, you 
may want to use dynamic proxy here in order to avoid having to override every 
method in the interface.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to