Skip to content
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

Increase default pool size in Rest Client Reactive #20977

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package io.quarkus.rest.client.reactive;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.rest.client.reactive.headers.ClientHeaderParamFromPropertyTest;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.smallrye.mutiny.Uni;
import io.vertx.core.Vertx;

public class ConnectionPoolSizeTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something tells me that these tests are going to a little flaky on CI, but let's see how it goes :)

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class).addClasses(ClientHeaderParamFromPropertyTest.Client.class));

@TestHTTPResource
URI uri;

@Test
void shouldPerform20CallsWithoutQueuing() throws InterruptedException {
Client client = RestClientBuilder.newBuilder().baseUri(uri)
.build(Client.class);

CountDownLatch latch = executeCalls(client, 20);

assertThat(latch.await(2, TimeUnit.SECONDS))
.overridingErrorMessage("Failed to do 20 calls in 2 seconds")
.isTrue();
}

@Test
@Timeout(5)
void shouldPerform21CallsWithQueuing() throws InterruptedException {
Client client = RestClientBuilder.newBuilder().baseUri(uri)
.build(Client.class);

long start = System.currentTimeMillis();
CountDownLatch latch = executeCalls(client, 21);
latch.await();

assertThat(System.currentTimeMillis() - start).isLessThan(3000).isGreaterThanOrEqualTo(2000);
}

@Test
@Timeout(5)
void shouldPerform5CallsWithoutQueueingOnQueue6() throws InterruptedException {
Client client = RestClientBuilder.newBuilder().baseUri(uri)
.property(QuarkusRestClientProperties.CONNECTION_POOL_SIZE, 6)
.build(Client.class);

long start = System.currentTimeMillis();
CountDownLatch latch = executeCalls(client, 5);
latch.await();

assertThat(System.currentTimeMillis() - start).isLessThan(2000);
}

@Test
@Timeout(5)
void shouldPerform5CallsWithQueueingOnQueue4() throws InterruptedException {
Client client = RestClientBuilder.newBuilder().baseUri(uri)
.property(QuarkusRestClientProperties.CONNECTION_POOL_SIZE, 4)
.build(Client.class);

long start = System.currentTimeMillis();
CountDownLatch latch = executeCalls(client, 5);
latch.await();

assertThat(System.currentTimeMillis() - start).isLessThan(3000).isGreaterThanOrEqualTo(2000);
}

private CountDownLatch executeCalls(Client client, int callAmount) {
ExecutorService executorService = Executors.newFixedThreadPool(callAmount);
CountDownLatch latch = new CountDownLatch(callAmount);
for (int i = 0; i < callAmount; i++) {
executorService.execute(() -> {
String result = client.get();
latch.countDown();
assertThat(result).isEqualTo("hello, world!");
});
}
return latch;
}

@Path("/hello")
public interface Client {
@GET
String get();
}

@Path("/hello")
public static class SlowResource {
@Inject
Vertx vertx;

@GET
public Uni<String> getSlowly() {
return Uni.createFrom().emitter(emitter -> vertx.setTimer(1000 /* ms */,
val -> emitter.complete("hello, world!")));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class ClientImpl implements Client {
private static final Logger log = Logger.getLogger(ClientImpl.class); // TODO: remove

private static final int DEFAULT_CONNECT_TIMEOUT = 15000;
private static final int DEFAULT_CONNECTION_POOL_SIZE = 20;

final ClientContext clientContext;
final boolean closeVertx;
Expand Down Expand Up @@ -125,10 +126,13 @@ public Vertx get() {
}

Object connectionPoolSize = configuration.getProperty(CONNECTION_POOL_SIZE);
if (connectionPoolSize != null) {
log.infof("Setting connectionPoolSize to %d s", connectionPoolSize);
options.setMaxPoolSize((int) connectionPoolSize);
if (connectionPoolSize == null) {
connectionPoolSize = DEFAULT_CONNECTION_POOL_SIZE;
} else {
log.debugf("Setting connectionPoolSize to %d s", connectionPoolSize);
}
options.setMaxPoolSize((int) connectionPoolSize);

this.httpClient = this.vertx.createHttpClient(options);
handlerChain = new HandlerChain(followRedirects);
}
Expand Down