ok2c commented on code in PR #776: URL: https://github.com/apache/httpcomponents-client/pull/776#discussion_r3173878361
########## httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/SharedRequestExecutionQueue.java: ########## @@ -0,0 +1,138 @@ +/* + * ==================================================================== + * 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.impl.async; + +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; + +import org.apache.hc.core5.annotation.Internal; +import org.apache.hc.core5.concurrent.Cancellable; +import org.apache.hc.core5.util.Args; + +/** + * Shared FIFO execution queue with a hard cap on concurrently executing tasks. + * Tasks beyond the cap are queued and executed when in-flight count drops. + */ +@Internal +final class SharedRequestExecutionQueue { + + private final int maxConcurrent; + private final AtomicInteger inFlight; + private final ConcurrentLinkedQueue<Entry> queue; + private final AtomicBoolean draining; + + SharedRequestExecutionQueue(final int maxConcurrent) { + if (maxConcurrent <= 0) { + throw new IllegalArgumentException("maxConcurrent must be > 0"); + } + this.maxConcurrent = maxConcurrent; + this.inFlight = new AtomicInteger(0); + this.queue = new ConcurrentLinkedQueue<>(); + this.draining = new AtomicBoolean(false); + } + + Cancellable enqueue(final Runnable task, final Runnable onCancel) { Review Comment: @arturobernalg Why not using `Cancellable` here instead of `Runnable`? Would not that be clearer? ########## httpclient5/src/test/java/org/apache/hc/client5/http/examples/H2SharedClientQueueLocalExample.java: ########## @@ -0,0 +1,260 @@ +/* + * ==================================================================== + * 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.H2AsyncClientBuilder; +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.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.http2.HttpVersionPolicy; +import org.apache.hc.core5.http2.config.H2Config; +import org.apache.hc.core5.http2.impl.nio.bootstrap.H2ServerBootstrap; +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 HTTP/2 async client using a shared FIFO queue. + * <p> + * The example configures {@link org.apache.hc.client5.http.impl.async.H2AsyncClientBuilder#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 as soon as the number of in-flight requests drops below the configured maximum. + * <p> + * A local HTTP/2 server is started with a permissive {@code maxConcurrentStreams} setting so that the client-side cap + * is the limiting factor. The example then submits multiple requests and prints summary statistics, including the + * maximum number of concurrent server requests observed. + * + * @since 5.7 + */ +public final class H2SharedClientQueueLocalExample { + + + public static void main(final String[] args) throws Exception { + final int maxConcurrentPerClient = 2; // <-- the feature under test + final int totalRequests = 50; + + final IOReactorConfig ioReactorConfig = IOReactorConfig.custom() + .setIoThreadCount(1) + .build(); + + final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); + + final AtomicInteger serverInFlight = new AtomicInteger(0); + final AtomicInteger serverMaxInFlight = new AtomicInteger(0); + + final H2Config serverH2Config = H2Config.custom() + .setPushEnabled(false) + // Keep server permissive. We want the *client* cap to be the limiting factor. + .setMaxConcurrentStreams(256) + .build(); + + final HttpAsyncServer server = H2ServerBootstrap.bootstrap() Review Comment: @arturobernalg Same here. Drop all server side code. Use `nghttp2.org` ########## httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/SharedRequestExecutionQueue.java: ########## @@ -0,0 +1,138 @@ +/* + * ==================================================================== + * 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.impl.async; + +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; + +import org.apache.hc.core5.annotation.Internal; +import org.apache.hc.core5.concurrent.Cancellable; +import org.apache.hc.core5.util.Args; + +/** + * Shared FIFO execution queue with a hard cap on concurrently executing tasks. + * Tasks beyond the cap are queued and executed when in-flight count drops. + */ +@Internal +final class SharedRequestExecutionQueue { + + private final int maxConcurrent; + private final AtomicInteger inFlight; + private final ConcurrentLinkedQueue<Entry> queue; + private final AtomicBoolean draining; + + SharedRequestExecutionQueue(final int maxConcurrent) { + if (maxConcurrent <= 0) { Review Comment: @arturobernalg Please use `Args` here as well for consistency. ########## httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncSharedClientQueueExample.java: ########## @@ -0,0 +1,149 @@ +/* + * ==================================================================== + * 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.util.concurrent.CountDownLatch; +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.message.StatusLine; +import org.apache.hc.core5.io.CloseMode; +import org.apache.hc.core5.reactor.IOReactorConfig; + +/** + * Demonstrates client-level request throttling for the async client using a shared FIFO queue. + * <p> + * The example configures {@code setMaxQueuedRequests(int)} to limit how many requests may execute concurrently for + * this client instance. Requests submitted beyond that limit are held in a shared FIFO queue and executed later. + * <p> + * This example assumes the target server endpoint is slow enough for several requests to overlap. No local server is + * started here; examples are intended to demonstrate API usage, not to act as integration tests. + * + * @since 5.7 + */ +public final class AsyncSharedClientQueueExample { Review Comment: @arturobernalg Drop all non-essentials for the examples. Examples do not need to be fully functional. Just clear and concise. -- 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]
