ok2c commented on code in PR #776:
URL:
https://github.com/apache/httpcomponents-client/pull/776#discussion_r3143775399
##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttpAsyncExecRuntime.java:
##########
@@ -407,6 +373,39 @@ public void cancelled() {
}
}
+ @Override
+ public Cancellable execute(
+ final String id,
+ final AsyncClientExchangeHandler exchangeHandler,
+ final HttpClientContext context) {
+
+ if (executionQueue == null) {
+ return startExecution(id, exchangeHandler, context);
+ }
+
+ final ComplexCancellable complexCancellable = new ComplexCancellable();
+
+ final Cancellable queued = executionQueue.enqueue(
+ () -> {
+ final AsyncClientExchangeHandler wrapped =
+ new
ReleasingAsyncClientExchangeHandler(exchangeHandler, executionQueue::completed);
+ try {
+ final Cancellable cancellable = startExecution(id,
wrapped, context);
+ complexCancellable.setDependency(cancellable);
+ } catch (final RuntimeException ex) {
+ wrapped.failed(ex);
+ }
+ },
+ exchangeHandler::cancel);
+
+ return () -> {
Review Comment:
@arturobernalg Can be simplified as
`complexCancellable.setDependency(queued)`
##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalH2AsyncExecRuntime.java:
##########
@@ -261,41 +255,47 @@ public EndpointInfo getEndpointInfo() {
return null;
}
- private boolean tryAcquireSlot() {
- if (sharedQueued == null || maxQueued <= 0) {
- return true;
+ @Override
+ public Cancellable execute(
+ final String id,
+ final AsyncClientExchangeHandler exchangeHandler,
+ final HttpClientContext context) {
+
+ final Endpoint endpoint = ensureValid();
+ final ComplexCancellable complexCancellable = new ComplexCancellable();
+
+ if (executionQueue == null) {
+ startExecution(id, endpoint, exchangeHandler, context,
complexCancellable);
+ return complexCancellable;
}
- for (;;) {
- final int q = sharedQueued.get();
- if (q >= maxQueued) {
- return false;
- }
- if (sharedQueued.compareAndSet(q, q + 1)) {
+
+ final Cancellable queued = executionQueue.enqueue(
+ () -> {
+ final AsyncClientExchangeHandler wrapped =
+ new
ReleasingAsyncClientExchangeHandler(exchangeHandler, executionQueue::completed);
+ try {
+ startExecution(id, endpoint, wrapped, context,
complexCancellable);
+ } catch (final RuntimeException ex) {
+ wrapped.failed(ex);
+ }
+ },
+ exchangeHandler::cancel);
+
+ return () -> {
Review Comment:
@arturobernalg Can be simplified as
`complexCancellable.setDependency(queued)`
##########
httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncSharedClientQueueLocalExample.java:
##########
@@ -0,0 +1,255 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.http.examples;
+
+import java.net.InetSocketAddress;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
+import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
+import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
+import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
+import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
+import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
+import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
+import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
+import
org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
+import org.apache.hc.core5.concurrent.FutureCallback;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.Message;
+import org.apache.hc.core5.http.URIScheme;
+import org.apache.hc.core5.http.impl.bootstrap.AsyncServerBootstrap;
+import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer;
+import org.apache.hc.core5.http.message.StatusLine;
+import org.apache.hc.core5.http.nio.AsyncRequestConsumer;
+import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
+import org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer;
+import org.apache.hc.core5.http.nio.support.AsyncResponseBuilder;
+import org.apache.hc.core5.http.nio.support.BasicRequestConsumer;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.http.protocol.HttpCoreContext;
+import org.apache.hc.core5.io.CloseMode;
+import org.apache.hc.core5.reactor.IOReactorConfig;
+import org.apache.hc.core5.reactor.ListenerEndpoint;
+
+/**
+ * Demonstrates client-level request throttling for the async client using a
shared FIFO queue.
+ * <p>
+ * The example configures {@code setMaxQueuedRequests(int)} to cap the number
of concurrently executing requests per
+ * client instance. Requests submitted beyond that limit are queued in FIFO
order and executed when the number of
+ * in-flight requests drops below the configured maximum.
+ *
+ * @since 5.7
+ */
+public final class AsyncSharedClientQueueLocalExample {
Review Comment:
@arturobernalg Examples are not integration tests. They do not need to be
fully functional. They need to be informative and easy to understand. Drop the
server side code. If there are any assumptions about the server side
functionality, just put a comment about it
--
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]