reactor.netty.resources.ConnectionProvider Java Examples
The following examples show how to use
reactor.netty.resources.ConnectionProvider.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Http2ConnectionProvider.java From reactor-netty with Apache License 2.0 | 6 votes |
PooledConnectionAllocator( ConnectionProvider parent, TransportConfig config, int maxConnections, PoolFactory<Connection> poolFactory, Supplier<SocketAddress> remoteAddress, AddressResolverGroup<?> resolver) { this.parent = parent; this.config = (HttpClientConfig) config; this.initialMaxConnection = maxConnections; this.remoteAddress = remoteAddress; this.resolver = resolver; this.allocationStrategy = new SizeBasedAllocationStrategy(0, maxConnections); this.pool = poolFactory.newPool(connectChannel(), new SizeBasedAllocationStrategy(0, maxConnections), DEFAULT_DESTROY_HANDLER, DEFAULT_EVICTION_PREDICATE); }
Example #2
Source File: HttpMetricsHandlerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Before public void setUp() { httpServer = customizeServerOptions( HttpServer.create() .host("127.0.0.1") .port(0) .metrics(true, s -> s) .route(r -> r.post("/1", (req, res) -> res.header("Connection", "close") .send(req.receive().retain().delayElements(Duration.ofMillis(10)))) .post("/2", (req, res) -> res.header("Connection", "close") .send(req.receive().retain().delayElements(Duration.ofMillis(10)))))); provider = ConnectionProvider.create("HttpMetricsHandlerTests", 1); httpClient = customizeClientOptions(HttpClient.create(provider) .remoteAddress(() -> disposableServer.address()) .metrics(true, s -> s)); registry = new SimpleMeterRegistry(); Metrics.addRegistry(registry); }
Example #3
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 6 votes |
private ChannelId[] doTestConnectionIdleTime(ConnectionProvider provider) throws Exception { disposableServer = HttpServer.create() .port(0) .wiretap(true) .handle((req, res) -> res.sendString(Mono.just("hello"))) .bindNow(); Flux<ChannelId> id = createHttpClientForContextWithAddress(provider) .get() .uri("/") .responseConnection((res, conn) -> Mono.just(conn.channel().id()) .delayUntil(ch -> conn.inbound().receive())); ChannelId id1 = id.blockLast(Duration.ofSeconds(30)); Thread.sleep(30); ChannelId id2 = id.blockLast(Duration.ofSeconds(30)); assertThat(id1).isNotNull(); assertThat(id2).isNotNull(); provider.dispose(); return new ChannelId[] {id1, id2}; }
Example #4
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 6 votes |
private ChannelId[] doTestConnectionLifeTime(ConnectionProvider provider) throws Exception { disposableServer = HttpServer.create() .port(0) .handle((req, resp) -> resp.sendObject(ByteBufFlux.fromString(Mono.delay(Duration.ofMillis(30)) .map(Objects::toString)))) .wiretap(true) .bindNow(); Flux<ChannelId> id = createHttpClientForContextWithAddress(provider) .get() .uri("/") .responseConnection((res, conn) -> Mono.just(conn.channel().id()) .delayUntil(ch -> conn.inbound().receive())); ChannelId id1 = id.blockLast(Duration.ofSeconds(30)); Thread.sleep(10); ChannelId id2 = id.blockLast(Duration.ofSeconds(30)); assertThat(id1).isNotNull(); assertThat(id2).isNotNull(); provider.dispose(); return new ChannelId[] {id1, id2}; }
Example #5
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void testIssue473() throws Exception { SelfSignedCertificate cert = new SelfSignedCertificate(); SslContextBuilder serverSslContextBuilder = SslContextBuilder.forServer(cert.certificate(), cert.privateKey()); disposableServer = HttpServer.create() .port(0) .wiretap(true) .secure(spec -> spec.sslContext(serverSslContextBuilder)) .bindNow(); StepVerifier.create( HttpClient.create(ConnectionProvider.newConnection()) .secure() .websocket() .uri("wss://" + disposableServer.host() + ":" + disposableServer.port()) .handle((in, out) -> Mono.empty())) .expectErrorMatches(t -> t.getCause() instanceof CertificateException) .verify(Duration.ofSeconds(30)); }
Example #6
Source File: ReactorResourceFactoryTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void localResources() throws Exception { this.resourceFactory.setUseGlobalResources(false); this.resourceFactory.afterPropertiesSet(); ConnectionProvider connectionProvider = this.resourceFactory.getConnectionProvider(); LoopResources loopResources = this.resourceFactory.getLoopResources(); assertNotSame(HttpResources.get(), connectionProvider); assertNotSame(HttpResources.get(), loopResources); // The below does not work since ConnectionPoolProvider simply checks if pool is empty. // assertFalse(connectionProvider.isDisposed()); assertFalse(loopResources.isDisposed()); this.resourceFactory.destroy(); assertTrue(connectionProvider.isDisposed()); assertTrue(loopResources.isDisposed()); }
Example #7
Source File: HttpClientWithTomcatTest.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void contentHeader() { ConnectionProvider fixed = ConnectionProvider.create("contentHeader", 1); HttpClient client = HttpClient.create(fixed) .wiretap(true) .headers(h -> h.add("content-length", "1")); HttpResponseStatus r = client.request(HttpMethod.GET) .uri(getURL()) .send(ByteBufFlux.fromString(Mono.just(" "))) .responseSingle((res, buf) -> Mono.just(res.status())) .block(Duration.ofSeconds(30)); client.request(HttpMethod.GET) .uri(getURL()) .send(ByteBufFlux.fromString(Mono.just(" "))) .responseSingle((res, buf) -> Mono.just(res.status())) .block(Duration.ofSeconds(30)); Assert.assertEquals(r, HttpResponseStatus.BAD_REQUEST); fixed.dispose(); }
Example #8
Source File: TcpClientTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test public void tcpClientHandlesLineFeedDataFixedPool() throws InterruptedException { Consumer<? super Connection> channelInit = c -> c .addHandler("codec", new LineBasedFrameDecoder(8 * 1024)); // ConnectionProvider p = ConnectionProvider.fixed // ("tcpClientHandlesLineFeedDataFixedPool", 1); ConnectionProvider p = ConnectionProvider.newConnection(); tcpClientHandlesLineFeedData( TcpClient.create(p) .host("localhost") .port(echoServerPort) .doOnConnected(channelInit) ); }
Example #9
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test //https://github.com/reactor/reactor-pool/issues/82 public void testConnectionRefusedConcurrentRequests(){ ConnectionProvider provider = ConnectionProvider.create("testConnectionRefusedConcurrentRequests", 1); HttpClient httpClient = HttpClient.create(provider) .port(8282); Mono<String> mono1 = httpClient.get() .responseSingle((r, buf) -> buf.asString()) .log("mono1"); Mono<String> mono2 = httpClient.get() .responseSingle((r, buf) -> buf.asString()) .log("mono2"); StepVerifier.create(Flux.just(mono1.onErrorResume(e -> Mono.empty()), mono2) .flatMap(Function.identity())) .expectError() .verify(Duration.ofSeconds(5)); provider.disposeLater() .block(Duration.ofSeconds(5)); }
Example #10
Source File: TcpMetricsTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Before public void setUp() { tcpServer = customizeServerOptions(TcpServer.create() .host("127.0.0.1") .port(0) .metrics(true)); provider = ConnectionProvider.create("TcpMetricsTests", 1); tcpClient = customizeClientOptions(TcpClient.create(provider) .remoteAddress(() -> disposableServer.address()) .metrics(true)); registry = new SimpleMeterRegistry(); Metrics.addRegistry(registry); }
Example #11
Source File: ReactorResourceFactoryTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void localResources() throws Exception { this.resourceFactory.setUseGlobalResources(false); this.resourceFactory.afterPropertiesSet(); ConnectionProvider connectionProvider = this.resourceFactory.getConnectionProvider(); LoopResources loopResources = this.resourceFactory.getLoopResources(); assertNotSame(HttpResources.get(), connectionProvider); assertNotSame(HttpResources.get(), loopResources); // The below does not work since ConnectionPoolProvider simply checks if pool is empty. // assertFalse(connectionProvider.isDisposed()); assertFalse(loopResources.isDisposed()); this.resourceFactory.destroy(); assertTrue(connectionProvider.isDisposed()); assertTrue(loopResources.isDisposed()); }
Example #12
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@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 = HttpServer.create() .secure(ssl -> ssl.sslContext(sslServer)) .port(0) .handle((req, res) -> res.send(req.receive().retain())) .bindNow(); ConnectionProvider pool = ConnectionProvider.create("testExplicitEmptyBodyOnGetWorks", 1); for (int i = 0; i < 4; i++) { StepVerifier.create(createHttpClientForContextWithAddress(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(); }
Example #13
Source File: HttpConnectionProvider.java From reactor-netty with Apache License 2.0 | 5 votes |
static ConnectionProvider getOrCreate(ConnectionProvider http1ConnectionProvider, int maxHttp2Connections) { ConnectionProvider provider = h2ConnectionProvider.get(); if (provider == null) { h2ConnectionProvider.compareAndSet(null, new Http2ConnectionProvider(http1ConnectionProvider, maxHttp2Connections)); provider = getOrCreate(http1ConnectionProvider, maxHttp2Connections); } return provider; }
Example #14
Source File: Http2Tests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testMaxActiveStreams_1() throws Exception { doTestMaxActiveStreams(HttpClient.create(), 1, 1, 1); ConnectionProvider provider = ConnectionProvider.create("testMaxActiveStreams_1", 1); doTestMaxActiveStreams(HttpClient.create(provider), 1, 1, 1); provider.disposeLater() .block(); doTestMaxActiveStreams(HttpClient.newConnection(), 1, 1, 1); }
Example #15
Source File: Http2Tests.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testConcurrentStreamsNegotiatedProtocolHTTP11() throws Exception { doTestConcurrentStreams(HttpClient.create(), true, new HttpProtocol[]{HttpProtocol.HTTP11}, new HttpProtocol[]{HttpProtocol.H2, HttpProtocol.HTTP11}); ConnectionProvider provider = ConnectionProvider.create("testConcurrentStreamsH2CUpgrade", 1); doTestConcurrentStreams(HttpClient.create(provider), true, new HttpProtocol[]{HttpProtocol.HTTP11}, new HttpProtocol[]{HttpProtocol.H2, HttpProtocol.HTTP11}); provider.disposeLater() .block(); doTestConcurrentStreams(HttpClient.newConnection(), true, new HttpProtocol[]{HttpProtocol.HTTP11}, new HttpProtocol[]{HttpProtocol.H2, HttpProtocol.HTTP11}); }
Example #16
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testConnectionLifeTimeElasticPool() throws Exception { ConnectionProvider provider = ConnectionProvider.builder("testConnectionNoLifeTimeElasticPool") .maxConnections(Integer.MAX_VALUE) .maxLifeTime(Duration.ofMillis(30)) .build(); ChannelId[] ids = doTestConnectionLifeTime(provider); assertThat(ids[0]).isNotEqualTo(ids[1]); }
Example #17
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 5 votes |
private void doTestIssue600(boolean withLoop) { disposableServer = HttpServer.create() .port(0) .handle((req, res) -> res.send(req.receive() .retain() .delaySubscription(Duration.ofSeconds(1)))) .wiretap(true) .bindNow(); ConnectionProvider pool = ConnectionProvider.create("doTestIssue600", 10); LoopResources loop = LoopResources.create("test", 4, true); HttpClient client; if (withLoop) { client = createHttpClientForContextWithAddress(pool) .runOn(loop); } else { client = createHttpClientForContextWithAddress(pool); } Set<String> threadNames = new ConcurrentSkipListSet<>(); StepVerifier.create( Flux.range(1,4) .flatMap(i -> client.request(HttpMethod.GET) .uri("/") .send((req, out) -> out.send(Flux.empty())) .responseContent() .doFinally(s -> threadNames.add(Thread.currentThread().getName())))) .expectComplete() .verify(Duration.ofSeconds(30)); pool.dispose(); loop.dispose(); assertThat(threadNames.size()).isGreaterThan(1); }
Example #18
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testConnectionLifeTimeFixedPool() throws Exception { ConnectionProvider provider = ConnectionProvider.builder("testConnectionLifeTimeFixedPool") .maxConnections(1) .pendingAcquireTimeout(Duration.ofMillis(100)) .maxLifeTime(Duration.ofMillis(30)) .build(); ChannelId[] ids = doTestConnectionLifeTime(provider); assertThat(ids[0]).isNotEqualTo(ids[1]); }
Example #19
Source File: ReactorClientHttpConnector.java From java-technology-stack with MIT License | 5 votes |
private static HttpClient initHttpClient(ReactorResourceFactory resourceFactory) { ConnectionProvider provider = resourceFactory.getConnectionProvider(); LoopResources resources = resourceFactory.getLoopResources(); Assert.notNull(provider, "No ConnectionProvider: is ReactorResourceFactory not initialized yet?"); Assert.notNull(resources, "No LoopResources: is ReactorResourceFactory not initialized yet?"); return HttpClient.create(provider).tcpConfiguration(tcpClient -> tcpClient.runOn(resources)); }
Example #20
Source File: ReactorNettyTcpClient.java From spring-analysis-note with MIT License | 5 votes |
/** * Simple constructor with the host and port to use to connect to. * <p>This constructor manages the lifecycle of the {@link TcpClient} and * underlying resources such as {@link ConnectionProvider}, * {@link LoopResources}, and {@link ChannelGroup}. * <p>For full control over the initialization and lifecycle of the * TcpClient, use {@link #ReactorNettyTcpClient(TcpClient, ReactorNettyCodec)}. * @param host the host to connect to * @param port the port to connect to * @param codec for encoding and decoding the input/output byte streams * @see org.springframework.messaging.simp.stomp.StompReactorNettyCodec */ public ReactorNettyTcpClient(String host, int port, ReactorNettyCodec<P> codec) { Assert.notNull(host, "host is required"); Assert.notNull(codec, "ReactorNettyCodec is required"); this.channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE); this.loopResources = LoopResources.create("tcp-client-loop"); this.poolResources = ConnectionProvider.elastic("tcp-client-pool"); this.codec = codec; this.tcpClient = TcpClient.create(this.poolResources) .host(host).port(port) .runOn(this.loopResources, false) .doOnConnected(conn -> this.channelGroup.add(conn.channel())); }
Example #21
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 5 votes |
private HttpClient createHttpClientForContextWithPort(ConnectionProvider pool) { HttpClient client; if (pool == null) { client = HttpClient.create(); } else { client = HttpClient.create(pool); } return client.port(disposableServer.port()) .wiretap(true); }
Example #22
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 5 votes |
private HttpClient createHttpClientForContextWithAddress(ConnectionProvider pool) { HttpClient client; if (pool == null) { client = HttpClient.create(); } else { client = HttpClient.create(pool); } return client.remoteAddress(disposableServer::address) .wiretap(true); }
Example #23
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void closePool() { ConnectionProvider pr = ConnectionProvider.create("closePool", 1); disposableServer = HttpServer.create() .port(0) .handle((in, out) -> out.sendString(Mono.just("test") .delayElement(Duration.ofMillis(100)) .repeat())) .wiretap(true) .bindNow(); Flux<String> ws = createHttpClientForContextWithPort(pr) .get() .uri("/") .responseContent() .asString(); List<String> expected = Flux.range(1, 20) .map(v -> "test") .collectList() .block(); Assert.assertNotNull(expected); StepVerifier.create( Flux.range(1, 10) .concatMap(i -> ws.take(2) .log())) .expectNextSequence(expected) .expectComplete() .verify(); pr.dispose(); }
Example #24
Source File: ClientTransportConfig.java From reactor-netty with Apache License 2.0 | 5 votes |
protected ClientTransportConfig(ConnectionProvider connectionProvider, Map<ChannelOption<?>, ?> options, Supplier<? extends SocketAddress> remoteAddress) { super(options); this.connectionProvider = Objects.requireNonNull(connectionProvider, "connectionProvider"); this.remoteAddress = Objects.requireNonNull(remoteAddress, "remoteAddress"); this.resolver = DefaultAddressResolverGroup.INSTANCE; }
Example #25
Source File: HttpClientWithTomcatTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void simpleClientPooling() { ConnectionProvider p = ConnectionProvider.create("simpleClientPooling", 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(getURL() + "/status/404") .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(getURL() + "/status/404") .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(); }
Example #26
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void testClientReuseIssue405(){ disposableServer = HttpServer.create() .port(0) .handle((in,out)->out.sendString(Flux.just("hello"))) .wiretap(true) .bindNow(); ConnectionProvider pool = ConnectionProvider.create("testClientReuseIssue405", 1); HttpClient httpClient = createHttpClientForContextWithPort(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)); pool.dispose(); }
Example #27
Source File: HttpClientWithTomcatTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void disableChunkImplicitDefault() { ConnectionProvider p = ConnectionProvider.create("disableChunkImplicitDefault", 1); HttpClient client = HttpClient.create(p) .host("localhost") .port(getPort()) .wiretap(true); Tuple2<HttpResponseStatus, Channel> r = client.get() .uri("/status/404") .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("/status/404") .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(); }
Example #28
Source File: HttpClientTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void userIssue() throws Exception { final ConnectionProvider pool = ConnectionProvider.create("userIssue", 1); CountDownLatch latch = new CountDownLatch(3); Set<String> localAddresses = ConcurrentHashMap.newKeySet(); disposableServer = HttpServer.create() .port(8080) .route(r -> r.post("/", (req, resp) -> req.receive() .asString() .flatMap(data -> { latch.countDown(); return resp.status(200) .send(); }))) .wiretap(true) .bindNow(); final HttpClient client = createHttpClientForContextWithAddress(pool); Flux.just("1", "2", "3") .concatMap(data -> client.doOnResponse((res, conn) -> localAddresses.add(conn.channel() .localAddress() .toString())) .post() .uri("/") .send(ByteBufFlux.fromString(Flux.just(data))) .responseContent()) .subscribe(); latch.await(); pool.dispose(); System.out.println("Local Addresses used: " + localAddresses); }
Example #29
Source File: HttpClientWithTomcatTest.java From reactor-netty with Apache License 2.0 | 5 votes |
@Test public void simpleTest404_1() { ConnectionProvider pool = ConnectionProvider.create("simpleTest404_1", 1); HttpClient client = HttpClient.create(pool) .port(getPort()) .host("localhost") .wiretap(true); doSimpleTest404(client); doSimpleTest404(client); pool.dispose(); }
Example #30
Source File: HttpRedirectTest.java From reactor-netty with Apache License 2.0 | 5 votes |
private void redirectTests(String url) { AtomicInteger counter = new AtomicInteger(1); DisposableServer server = HttpServer.create() .port(0) .handle((req, res) -> { if (req.uri().contains("/login") && req.method().equals(HttpMethod.POST) && counter.getAndDecrement() > 0) { return res.sendRedirect(url); } else { return res.status(200) .send(); } }) .wiretap(true) .bindNow(); ConnectionProvider pool = ConnectionProvider.create("redirectTests", 1); HttpClient client = HttpClient.create(pool) .remoteAddress(server::address); try { Flux.range(0, 100) .concatMap(i -> client.followRedirect(true) .post() .uri("/login") .responseContent() .then()) .blockLast(Duration.ofSeconds(30)); } finally { server.disposeNow(); } }