-
Notifications
You must be signed in to change notification settings - Fork 355
Cap total number of concurrent requests per HTTP/2 connection #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arturobernalg
wants to merge
1
commit into
apache:master
Choose a base branch
from
arturobernalg:max-requests-per-connection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+976
−81
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,10 +29,9 @@ | |
|
|
||
| import java.io.IOException; | ||
| import java.net.InetSocketAddress; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.RejectedExecutionException; | ||
|
|
||
| import org.apache.hc.core5.annotation.Internal; | ||
| import org.apache.hc.core5.concurrent.Cancellable; | ||
|
|
@@ -44,21 +43,15 @@ | |
| import org.apache.hc.core5.function.Decorator; | ||
| import org.apache.hc.core5.function.Resolver; | ||
| import org.apache.hc.core5.http.ConnectionClosedException; | ||
| import org.apache.hc.core5.http.EntityDetails; | ||
| import org.apache.hc.core5.http.Header; | ||
| import org.apache.hc.core5.http.HttpException; | ||
| import org.apache.hc.core5.http.HttpHost; | ||
| import org.apache.hc.core5.http.HttpResponse; | ||
| import org.apache.hc.core5.http.impl.DefaultAddressResolver; | ||
| import org.apache.hc.core5.http.impl.bootstrap.AsyncRequester; | ||
| import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler; | ||
| import org.apache.hc.core5.http.nio.AsyncPushConsumer; | ||
| import org.apache.hc.core5.http.nio.AsyncRequestProducer; | ||
| import org.apache.hc.core5.http.nio.AsyncResponseConsumer; | ||
| import org.apache.hc.core5.http.nio.CapacityChannel; | ||
| import org.apache.hc.core5.http.nio.DataStreamChannel; | ||
| import org.apache.hc.core5.http.nio.HandlerFactory; | ||
| import org.apache.hc.core5.http.nio.RequestChannel; | ||
| import org.apache.hc.core5.http.nio.command.RequestExecutionCommand; | ||
| import org.apache.hc.core5.http.nio.command.ShutdownCommand; | ||
| import org.apache.hc.core5.http.nio.ssl.TlsStrategy; | ||
|
|
@@ -87,6 +80,12 @@ public class H2MultiplexingRequester extends AsyncRequester { | |
|
|
||
| private final H2ConnPool connPool; | ||
|
|
||
| /** | ||
| * Hard cap on per-connection queued / in-flight requests. | ||
| * {@code <= 0} disables the cap. | ||
| */ | ||
| private final int maxRequestsPerConnection; | ||
|
|
||
| /** | ||
| * Use {@link H2MultiplexingRequesterBootstrap} to create instances of this class. | ||
| */ | ||
|
|
@@ -100,11 +99,13 @@ public H2MultiplexingRequester( | |
| final Resolver<HttpHost, InetSocketAddress> addressResolver, | ||
| final TlsStrategy tlsStrategy, | ||
| final IOReactorMetricsListener threadPoolListener, | ||
| final IOWorkerSelector workerSelector) { | ||
| final IOWorkerSelector workerSelector, | ||
| final int maxRequestsPerConnection) { | ||
| super(eventHandlerFactory, ioReactorConfig, ioSessionDecorator, exceptionCallback, sessionListener, | ||
| ShutdownCommand.GRACEFUL_IMMEDIATE_CALLBACK, DefaultAddressResolver.INSTANCE, | ||
| threadPoolListener, workerSelector); | ||
| this.connPool = new H2ConnPool(this, addressResolver, tlsStrategy); | ||
| this.maxRequestsPerConnection = maxRequestsPerConnection; | ||
| } | ||
|
|
||
| public void closeIdle(final TimeValue idleTime) { | ||
|
|
@@ -182,83 +183,42 @@ private void execute( | |
| if (request.getAuthority() == null) { | ||
| request.setAuthority(new URIAuthority(host)); | ||
| } | ||
| if (request.getScheme() == null) { | ||
| request.setScheme(host.getSchemeName()); | ||
| } | ||
| connPool.getSession(host, timeout, new FutureCallback<IOSession>() { | ||
|
|
||
| @Override | ||
| public void completed(final IOSession ioSession) { | ||
| final AsyncClientExchangeHandler handlerProxy = new AsyncClientExchangeHandler() { | ||
|
|
||
| @Override | ||
| public void releaseResources() { | ||
| final int max = maxRequestsPerConnection; | ||
| if (max > 0) { | ||
| final int current = ioSession.getPendingCommandCount(); | ||
| if (current >= 0 && current >= max) { | ||
| exchangeHandler.failed(new RejectedExecutionException( | ||
| "Maximum number of pending requests per connection reached (max=" + max + ")")); | ||
| exchangeHandler.releaseResources(); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void produceRequest(final RequestChannel channel, final HttpContext httpContext) throws HttpException, IOException { | ||
| channel.sendRequest(request, entityDetails, httpContext); | ||
| } | ||
|
|
||
| @Override | ||
| public int available() { | ||
| return exchangeHandler.available(); | ||
| } | ||
|
|
||
| @Override | ||
| public void produce(final DataStreamChannel channel) throws IOException { | ||
| exchangeHandler.produce(channel); | ||
| } | ||
|
|
||
| @Override | ||
| public void consumeInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException { | ||
| exchangeHandler.consumeInformation(response, httpContext); | ||
| } | ||
|
|
||
| @Override | ||
| public void consumeResponse( | ||
| final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException { | ||
| exchangeHandler.consumeResponse(response, entityDetails, httpContext); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateCapacity(final CapacityChannel capacityChannel) throws IOException { | ||
| exchangeHandler.updateCapacity(capacityChannel); | ||
| } | ||
|
|
||
| @Override | ||
| public void consume(final ByteBuffer src) throws IOException { | ||
| exchangeHandler.consume(src); | ||
| } | ||
|
|
||
| @Override | ||
| public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException { | ||
| exchangeHandler.streamEnd(trailers); | ||
| } | ||
|
|
||
| @Override | ||
| public void cancel() { | ||
| exchangeHandler.cancel(); | ||
| } | ||
|
|
||
| @Override | ||
| public void failed(final Exception cause) { | ||
| exchangeHandler.failed(cause); | ||
| } | ||
|
|
||
| }; | ||
| final Timeout socketTimeout = ioSession.getSocketTimeout(); | ||
| ioSession.enqueue(new RequestExecutionCommand( | ||
| handlerProxy, | ||
| pushHandlerFactory, | ||
| context, | ||
| streamControl -> { | ||
| cancellableDependency.setDependency(streamControl); | ||
| if (socketTimeout != null) { | ||
| streamControl.setTimeout(socketTimeout); | ||
| } | ||
| }), | ||
| Command.Priority.NORMAL); | ||
| final RequestExecutionCommand command = new RequestExecutionCommand( | ||
| exchangeHandler, | ||
| pushHandlerFactory, | ||
| context, | ||
| streamControl -> { | ||
| cancellableDependency.setDependency(streamControl); | ||
| if (socketTimeout != null) { | ||
| streamControl.setTimeout(socketTimeout); | ||
| } | ||
| }); | ||
|
|
||
| ioSession.enqueue(command, Command.Priority.NORMAL); | ||
|
|
||
| if (!ioSession.isOpen()) { | ||
| exchangeHandler.failed(new ConnectionClosedException()); | ||
| exchangeHandler.releaseResources(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -350,4 +310,33 @@ public H2ConnPool getConnPool() { | |
| return connPool; | ||
| } | ||
|
|
||
| /** | ||
| * Cancellable that can be wired to the stream control once it becomes available. | ||
| */ | ||
| private static final class CancellableExecution implements Cancellable, CancellableDependency { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @arturobernalg I am not sure why this is necessary. The original |
||
|
|
||
| private volatile Cancellable dependency; | ||
|
|
||
| @Override | ||
| public void setDependency(final Cancellable dependency) { | ||
| this.dependency = dependency; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isCancelled() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean cancel() { | ||
| final Cancellable local = this.dependency; | ||
| if (local != null) { | ||
| local.cancel(); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arturobernalg Why, why all this complexity? You are not going to impress no young ladies with it. You almost had it right last time. Restore the command count method from the previous revision and fail the request if the total number of pending commands is over the max limit. That should be all. The tricky bit is to come up with a reasonable config mechanism for the max pending command limit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.