-
Notifications
You must be signed in to change notification settings - Fork 355
Cap total number of concurrent commands per 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.
+348
−8
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
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
229 changes: 229 additions & 0 deletions
229
...c/core5/http2/impl/nio/bootstrap/TestH2MultiplexingRequesterMaxRequestsPerConnection.java
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 |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| /* | ||
| * ==================================================================== | ||
| * 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.core5.http2.impl.nio.bootstrap; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.RejectedExecutionException; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| 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.HttpHost; | ||
| import org.apache.hc.core5.http.HttpRequest; | ||
| import org.apache.hc.core5.http.HttpResponse; | ||
| 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.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.entity.StringAsyncEntityConsumer; | ||
| import org.apache.hc.core5.http.nio.support.AsyncRequestBuilder; | ||
| import org.apache.hc.core5.http.nio.support.AsyncResponseBuilder; | ||
| import org.apache.hc.core5.http.nio.support.BasicRequestConsumer; | ||
| import org.apache.hc.core5.http.nio.support.BasicResponseConsumer; | ||
| 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.io.CloseMode; | ||
| import org.apache.hc.core5.reactor.IOReactorConfig; | ||
| import org.apache.hc.core5.reactor.ListenerEndpoint; | ||
| import org.apache.hc.core5.util.Timeout; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class TestH2MultiplexingRequesterMaxRequestsPerConnection { | ||
|
|
||
| @Test | ||
| @org.junit.jupiter.api.Timeout(value = 60, unit = TimeUnit.SECONDS) | ||
| void testRejectsWhenLimitReached() throws Exception { | ||
| final int maxPerConn = 2; | ||
| final int totalRequests = 30; | ||
| final Timeout timeout = Timeout.ofSeconds(30); | ||
| final long serverDelayMillis = 5000; | ||
|
|
||
| final IOReactorConfig ioReactorConfig = IOReactorConfig.custom() | ||
| .setIoThreadCount(1) | ||
| .build(); | ||
|
|
||
| final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); | ||
|
|
||
| final H2Config serverH2Config = H2Config.custom() | ||
| .setPushEnabled(false) | ||
| .setMaxConcurrentStreams(1) // force backlog | ||
| .build(); | ||
|
|
||
| final HttpAsyncServer server = H2ServerBootstrap.bootstrap() | ||
| .setIOReactorConfig(ioReactorConfig) | ||
| .setH2Config(serverH2Config) | ||
| .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2) | ||
| .setCanonicalHostName("127.0.0.1") // avoid 421 | ||
| .register("*", new AsyncServerRequestHandler<Message<HttpRequest, Void>>() { | ||
|
|
||
| @Override | ||
| public AsyncRequestConsumer<Message<HttpRequest, Void>> prepare( | ||
| final HttpRequest request, | ||
| final EntityDetails entityDetails, | ||
| final HttpContext context) { | ||
| return new BasicRequestConsumer<>( | ||
| entityDetails != null ? new DiscardingEntityConsumer<>() : null); | ||
| } | ||
|
|
||
| @Override | ||
| public void handle( | ||
| final Message<HttpRequest, Void> message, | ||
| final ResponseTrigger responseTrigger, | ||
| final HttpContext localContext) { | ||
|
|
||
| final HttpCoreContext context = HttpCoreContext.cast(localContext); | ||
| final String path = message.getHead().getPath(); | ||
|
|
||
| final Runnable task = () -> { | ||
| try { | ||
| responseTrigger.submitResponse( | ||
| AsyncResponseBuilder.create(200) | ||
| .setEntity("ok\n", ContentType.TEXT_PLAIN) | ||
| .build(), | ||
| context); | ||
| } catch (final Exception ignore) { | ||
| // ignore | ||
| } | ||
| }; | ||
|
|
||
| if ("/warmup".equals(path)) { | ||
| task.run(); | ||
| } else { | ||
| scheduler.schedule(task, serverDelayMillis, TimeUnit.MILLISECONDS); | ||
| } | ||
| } | ||
|
|
||
| }) | ||
| .create(); | ||
|
|
||
| server.start(); | ||
| final ListenerEndpoint ep = server.listen(new InetSocketAddress("127.0.0.1", 0), URIScheme.HTTP).get(); | ||
| final int port = ((InetSocketAddress) ep.getAddress()).getPort(); | ||
|
|
||
| final H2MultiplexingRequester requester = H2MultiplexingRequesterBootstrap.bootstrap() | ||
| .setIOReactorConfig(ioReactorConfig) | ||
| .setH2Config(H2Config.custom().setPushEnabled(false).build()) | ||
| .setMaxCommandsPerConnection(maxPerConn) | ||
| .create(); | ||
|
|
||
|
|
||
| requester.start(); | ||
|
|
||
| try { | ||
| final HttpHost target = new HttpHost("http", "127.0.0.1", port); | ||
|
|
||
| // Warmup | ||
| requester.execute( | ||
| target, | ||
| AsyncRequestBuilder.get().setHttpHost(target).setPath("/warmup").build(), | ||
| new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), | ||
| timeout, | ||
| HttpCoreContext.create(), | ||
| null).get(); | ||
|
|
||
| final AtomicInteger ok = new AtomicInteger(0); | ||
| final AtomicInteger rejected = new AtomicInteger(0); | ||
| final AtomicInteger failed = new AtomicInteger(0); | ||
|
|
||
| final CountDownLatch done = new CountDownLatch(totalRequests); | ||
| final CountDownLatch start = new CountDownLatch(1); | ||
| final ExecutorService exec = Executors.newFixedThreadPool(16); | ||
|
|
||
| for (int i = 0; i < totalRequests; i++) { | ||
| final int id = i; | ||
| exec.execute(new Runnable() { | ||
| @Override | ||
| public void run() { | ||
| try { | ||
| start.await(); | ||
| } catch (final InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| done.countDown(); | ||
| return; | ||
| } | ||
|
|
||
| requester.execute( | ||
| target, | ||
| AsyncRequestBuilder.get().setHttpHost(target).setPath("/slow?i=" + id).build(), | ||
| new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), | ||
| timeout, | ||
| HttpCoreContext.create(), | ||
| new FutureCallback<Message<HttpResponse, String>>() { | ||
|
|
||
| @Override | ||
| public void completed(final Message<HttpResponse, String> message) { | ||
| ok.incrementAndGet(); | ||
| done.countDown(); | ||
| } | ||
|
|
||
| @Override | ||
| public void failed(final Exception ex) { | ||
| if (ex instanceof RejectedExecutionException) { | ||
| rejected.incrementAndGet(); | ||
| } else { | ||
| failed.incrementAndGet(); | ||
| } | ||
| done.countDown(); | ||
| } | ||
|
|
||
| @Override | ||
| public void cancelled() { | ||
| failed.incrementAndGet(); | ||
| done.countDown(); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| start.countDown(); | ||
|
|
||
| final boolean allDone = done.await(60, TimeUnit.SECONDS); | ||
| exec.shutdownNow(); | ||
|
|
||
| Assertions.assertTrue(allDone, "Timed out"); | ||
| Assertions.assertEquals(totalRequests, ok.get() + rejected.get() + failed.get()); | ||
| Assertions.assertTrue(rejected.get() > 0, "Expected at least one RejectedExecutionException"); | ||
| Assertions.assertEquals(0, failed.get(), "Unexpected non-rejection failures: " + failed.get()); | ||
| } finally { | ||
| requester.close(CloseMode.GRACEFUL); | ||
| server.close(CloseMode.GRACEFUL); | ||
| scheduler.shutdownNow(); | ||
| } | ||
| } | ||
| } | ||
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 This test should be moved to client and be made a part of a larger suite of related integration tests, but if you want to keep it here, so be it, but at least be consistent and provide a similar one for
HttpAsyncRequester.