connectionProvider = ConnectionProvider.newConnection(); } else if (pool.getType() == FIXED) { connectionProvider = ConnectionProvider.fixed(pool.getName(), pool.getMaxConnections(), pool.getAcquireTimeout()); } else {
/** * a new {@link ConnectionProvider} to cache and reuse a fixed maximum * number of {@link Connection}. * <p>A Fixed {@link ConnectionProvider} will open up to the given max number of * processors observed by this jvm (minimum 4). * Further connections will be pending acquisition indefinitely. * * @param name the connection pool name * * @return a new {@link ConnectionProvider} to cache and reuse a fixed maximum * number of {@link Connection} */ static ConnectionProvider fixed(String name) { return fixed(name, DEFAULT_POOL_MAX_CONNECTIONS); }
/** * Create a new {@link ConnectionProvider} to cache and reuse a fixed maximum * number of {@link Connection}. * <p>A Fixed {@link ConnectionProvider} will open up to the given max connection value. * Further connections will be pending acquisition indefinitely. * * @param name the connection pool name * @param maxConnections the maximum number of connections before starting pending * acquisition on existing ones * * @return a new {@link ConnectionProvider} to cache and reuse a fixed maximum * number of {@link Connection} */ static ConnectionProvider fixed(String name, int maxConnections) { return fixed(name, maxConnections, DEFAULT_POOL_ACQUIRE_TIMEOUT); }
/** * a new {@link ConnectionProvider} to cache and reuse a fixed maximum * number of {@link Connection}. * <p>A Fixed {@link ConnectionProvider} will open up to the given max number of * processors observed by this jvm (minimum 4). * Further connections will be pending acquisition indefinitely. * * @param name the connection pool name * * @return a new {@link ConnectionProvider} to cache and reuse a fixed maximum * number of {@link Connection} */ static ConnectionProvider fixed(String name) { return fixed(name, DEFAULT_POOL_MAX_CONNECTIONS); }
/** * Create a new {@link ConnectionProvider} to cache and reuse a fixed maximum * number of {@link Connection}. * <p>A Fixed {@link ConnectionProvider} will open up to the given max connection value. * Further connections will be pending acquisition indefinitely. * * @param name the connection pool name * @param maxConnections the maximum number of connections before starting pending * acquisition on existing ones * * @return a new {@link ConnectionProvider} to cache and reuse a fixed maximum * number of {@link Connection} */ static ConnectionProvider fixed(String name, int maxConnections) { return fixed(name, maxConnections, DEFAULT_POOL_ACQUIRE_TIMEOUT); }
@Test public void testIssue416() { TestResources resources = TestResources.get(); TestResources.set(ConnectionProvider.fixed("test")); assertThat(resources.provider.isDisposed()).isTrue(); assertThat(resources.loops.isDisposed()).isFalse(); TestResources.set(LoopResources.create("test")); assertThat(resources.loops.isDisposed()).isTrue(); assertThat(resources.isDisposed()).isTrue(); }
@Test public void contentHeader() { ConnectionProvider fixed = ConnectionProvider.fixed("test", 1); HttpClient client = HttpClient.create(fixed) .wiretap(true) .headers(h -> h.add("content-length", "1")); HttpResponseStatus r = client.request(HttpMethod.GET) .uri("http://google.com") .send(ByteBufFlux.fromString(Mono.just(" "))) .responseSingle((res, buf) -> Mono.just(res.status())) .block(Duration.ofSeconds(30)); client.request(HttpMethod.GET) .uri("http://google.com") .send(ByteBufFlux.fromString(Mono.just(" "))) .responseSingle((res, buf) -> Mono.just(res.status())) .block(Duration.ofSeconds(30)); Assert.assertEquals(r, HttpResponseStatus.BAD_REQUEST); fixed.dispose(); }
@Test public void simpleClientPooling() { ConnectionProvider p = ConnectionProvider.fixed("test", 1); AtomicReference<Channel> ch1 = new AtomicReference<>(); AtomicReference<Channel> ch2 = new AtomicReference<>(); HttpResponseStatus r = HttpClient.create(p) .doOnResponse((res, c) -> ch1.set(c.channel())) .wiretap(true) .get() .uri("http://google.com/unsupportedURI") .responseSingle((res, buf) -> buf.thenReturn(res.status())) .block(Duration.ofSeconds(30)); HttpClient.create(p) .doOnResponse((res, c) -> ch2.set(c.channel())) .wiretap(true) .get() .uri("http://google.com/unsupportedURI") .responseSingle((res, buf) -> buf.thenReturn(res.status())) .block(Duration.ofSeconds(30)); AtomicBoolean same = new AtomicBoolean(); same.set(ch1.get() == ch2.get()); Assert.assertTrue(same.get()); Assert.assertEquals(r, HttpResponseStatus.NOT_FOUND); p.dispose(); }
ConnectionProvider connectionProvider = ConnectionProvider.fixed("test", 1); HttpClient client = createHttpClientForContextWithPort(server, connectionProvider);
@Test public void connectionWillRetryConnectionAttemptWhenItFailsFixedChannelPool() throws InterruptedException { connectionWillRetryConnectionAttemptWhenItFails( TcpClient.create(ConnectionProvider.fixed("test", 1)) .host("localhost") .port(abortServerPort + 3) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100)); }
HttpClient.create(ConnectionProvider.fixed("test", 1)) .addressSupplier(server::address);
@Test public void simpleTest404_1() { ConnectionProvider pool = ConnectionProvider.fixed("http", 1); HttpClient client = HttpClient.create(pool) .port(80) .tcpConfiguration(tcpClient -> tcpClient.host("google.com")) .wiretap(true); doSimpleTest404(client); doSimpleTest404(client); pool.dispose(); }
@Test public void userIssue() throws Exception { final ConnectionProvider pool = ConnectionProvider.fixed("local", 1); CountDownLatch latch = new CountDownLatch(3); Set<String> localAddresses = ConcurrentHashMap.newKeySet();
@Test public void disableChunkImplicitDefault() { ConnectionProvider p = ConnectionProvider.fixed("test", 1); HttpClient client = HttpClient.create(p) .tcpConfiguration(tcpClient -> tcpClient.host("google.com")) .wiretap(true) .chunkedTransfer(false); Tuple2<HttpResponseStatus, Channel> r = client.get() .uri("/unsupportedURI") .responseConnection((res, conn) -> Mono.just(res.status()) .delayUntil(s -> conn.inbound().receive()) .zipWith(Mono.just(conn.channel()))) .blockLast(Duration.ofSeconds(30)); assertThat(r).isNotNull(); Channel r2 = client.get() .uri("/unsupportedURI") .responseConnection((res, conn) -> Mono.just(conn.channel()) .delayUntil(s -> conn.inbound().receive())) .blockLast(Duration.ofSeconds(30)); assertThat(r2).isNotNull(); Assert.assertSame(r.getT2(), r2); Assert.assertEquals(r.getT1(), HttpResponseStatus.NOT_FOUND); p.dispose(); }
@Test public void closePool() { ConnectionProvider pr = ConnectionProvider.fixed("wstest", 1); httpServer = HttpServer.create() .port(0)
@Test public void closePool() { ConnectionProvider pr = ConnectionProvider.fixed("wstest", 1); DisposableServer httpServer = HttpServer.create()
.bindNow(); ConnectionProvider pool = ConnectionProvider.fixed("test", 1);
@Test public void testIssue186() { DisposableServer server = HttpServer.create() .port(0) .handle((req, res) -> res.status(200).send()) .wiretap(true) .bindNow(); HttpClient client = HttpClient.create(ConnectionProvider.fixed("test", 1)) .addressSupplier(server::address) .wiretap(true); try { doTestIssue186(client); doTestIssue186(client); } finally { server.disposeNow(); } }
@Test public void testExplicitEmptyBodyOnGetWorks() throws Exception { SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()) .build(); SslContext sslClient = SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .build(); DisposableServer server = HttpServer.create() .secure(ssl -> ssl.sslContext(sslServer)) .port(0) .handle((req, res) -> res.send(req.receive().retain())) .bindNow(); ConnectionProvider pool = ConnectionProvider.fixed("test", 1); for (int i = 0; i < 4; i++) { StepVerifier.create(createHttpClientForContextWithAddress(server, pool) .secure(ssl -> ssl.sslContext(sslClient)) .request(HttpMethod.GET) .uri("/") .send((req, out) -> out.send(Flux.empty())) .responseContent()) .expectComplete() .verify(Duration.ofSeconds(30)); } pool.dispose(); server.disposeNow(); }
@Test public void testClientReuseIssue405(){ DisposableServer c = HttpServer.create() .port(0) .handle((in,out)->out.sendString(Flux.just("hello"))) .wiretap(true) .bindNow(); ConnectionProvider pool = ConnectionProvider.fixed("test", 1); HttpClient httpClient = createHttpClientForContextWithPort(c, pool); Mono<String> mono1 = httpClient.get() .responseSingle((r, buf) -> buf.asString()) .log("mono1"); Mono<String> mono2 = httpClient.get() .responseSingle((r, buf) -> buf.asString()) .log("mono1"); StepVerifier.create(Flux.zip(mono1,mono2)) .expectNext(Tuples.of("hello","hello")) .expectComplete() .verify(Duration.ofSeconds(20)); c.disposeNow(); pool.dispose(); }