use of reactor.netty.Connection in project reactor-netty by reactor.
the class Http2PoolTest method maxLifeTimeMaxConnectionsNotReached.
@Test
void maxLifeTimeMaxConnectionsNotReached() throws Exception {
PoolBuilder<Connection, PoolConfig<Connection>> poolBuilder = PoolBuilder.from(Mono.fromSupplier(() -> {
Channel channel = new EmbeddedChannel(new TestChannelId(), Http2FrameCodecBuilder.forClient().build());
return Connection.from(channel);
})).idleResourceReuseLruOrder().maxPendingAcquireUnbounded().sizeBetween(0, 2);
Http2Pool http2Pool = poolBuilder.build(config -> new Http2Pool(config, 10));
Connection connection1 = null;
Connection connection2 = null;
try {
PooledRef<Connection> acquired1 = http2Pool.acquire().block();
assertThat(acquired1).isNotNull();
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
assertThat(http2Pool.connections.size()).isEqualTo(1);
connection1 = acquired1.poolable();
ChannelId id1 = connection1.channel().id();
Thread.sleep(10);
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
assertThat(http2Pool.connections.size()).isEqualTo(1);
PooledRef<Connection> acquired2 = http2Pool.acquire().block();
assertThat(acquired2).isNotNull();
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(2);
assertThat(http2Pool.connections.size()).isEqualTo(2);
connection2 = acquired2.poolable();
ChannelId id2 = connection2.channel().id();
assertThat(id1).isNotEqualTo(id2);
acquired1.invalidate().block();
acquired2.invalidate().block();
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
assertThat(http2Pool.connections.size()).isEqualTo(0);
} finally {
if (connection1 != null) {
((EmbeddedChannel) connection1.channel()).finishAndReleaseAll();
connection1.dispose();
}
if (connection2 != null) {
((EmbeddedChannel) connection2.channel()).finishAndReleaseAll();
connection2.dispose();
}
}
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class Http2PoolTest method evictClosedConnectionMaxConnectionsReached.
@Test
void evictClosedConnectionMaxConnectionsReached() throws Exception {
PoolBuilder<Connection, PoolConfig<Connection>> poolBuilder = PoolBuilder.from(Mono.fromSupplier(() -> {
Channel channel = new EmbeddedChannel(new TestChannelId(), Http2FrameCodecBuilder.forClient().build());
return Connection.from(channel);
})).idleResourceReuseLruOrder().maxPendingAcquireUnbounded().sizeBetween(0, 1);
Http2Pool http2Pool = poolBuilder.build(config -> new Http2Pool(config, -1));
Connection connection = null;
try {
PooledRef<Connection> acquired1 = http2Pool.acquire().block();
assertThat(acquired1).isNotNull();
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
assertThat(http2Pool.connections.size()).isEqualTo(1);
connection = acquired1.poolable();
CountDownLatch latch = new CountDownLatch(1);
((EmbeddedChannel) connection.channel()).finishAndReleaseAll();
connection.onDispose(latch::countDown);
connection.dispose();
assertThat(latch.await(1, TimeUnit.SECONDS)).as("latch await").isTrue();
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
assertThat(http2Pool.connections.size()).isEqualTo(1);
http2Pool.acquire(Duration.ofMillis(10)).as(StepVerifier::create).expectError(PoolAcquireTimeoutException.class).verify(Duration.ofSeconds(1));
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(1);
assertThat(http2Pool.connections.size()).isEqualTo(1);
acquired1.invalidate().block();
assertThat(http2Pool.metrics().acquiredSize()).isEqualTo(0);
assertThat(http2Pool.connections.size()).isEqualTo(0);
} finally {
if (connection != null) {
((EmbeddedChannel) connection.channel()).finishAndReleaseAll();
connection.dispose();
}
}
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class HttpClientTest method doTestRetry.
private void doTestRetry(boolean retryDisabled, boolean expectRetry) throws Exception {
ExecutorService threadPool = Executors.newCachedThreadPool();
int serverPort = SocketUtils.findAvailableTcpPort();
ConnectionResetByPeerServer server = new ConnectionResetByPeerServer(serverPort);
Future<?> serverFuture = threadPool.submit(server);
if (!server.await(10, TimeUnit.SECONDS)) {
throw new IOException("fail to start test server");
}
AtomicInteger doOnRequest = new AtomicInteger();
AtomicInteger doOnRequestError = new AtomicInteger();
AtomicInteger doOnResponseError = new AtomicInteger();
HttpClient client = createClient(serverPort).doOnRequest((req, conn) -> doOnRequest.getAndIncrement()).doOnError((req, t) -> doOnRequestError.getAndIncrement(), (res, t) -> doOnResponseError.getAndIncrement());
if (retryDisabled) {
client = client.disableRetry(retryDisabled);
}
AtomicReference<Throwable> error = new AtomicReference<>();
StepVerifier.create(client.request(HttpMethod.GET).uri("/").send((req, out) -> {
if (expectRetry) {
return Mono.error(new IOException("Connection reset by peer"));
}
return out;
}).responseContent()).expectErrorMatches(t -> {
error.set(t);
return t.getMessage() != null && (t.getMessage().contains("Connection reset by peer") || // https://github.com/reactor/reactor-netty/issues/1673
t.getMessage().contains("readAddress(..)") || t.getMessage().contains("Connection prematurely closed BEFORE response"));
}).verify(Duration.ofSeconds(30));
int requestCount = 1;
int requestErrorCount = 1;
if (expectRetry && !(error.get() instanceof PrematureCloseException)) {
requestCount = 2;
requestErrorCount = 2;
}
assertThat(doOnRequest.get()).isEqualTo(requestCount);
assertThat(doOnRequestError.get()).isEqualTo(requestErrorCount);
assertThat(doOnResponseError.get()).isEqualTo(0);
server.close();
assertThat(serverFuture.get()).isNull();
threadPool.shutdown();
assertThat(threadPool.awaitTermination(5, TimeUnit.SECONDS)).isTrue();
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class HttpRedirectTest method testHttpRequestIfRedirectHttpToHttpsEnabled.
@Test
void testHttpRequestIfRedirectHttpToHttpsEnabled() {
Http11SslContextSpec sslContext = Http11SslContextSpec.forServer(ssc.certificate(), ssc.privateKey());
disposableServer = createServer().host("localhost").secure(spec -> spec.sslContext(sslContext), true).bindNow();
AtomicReference<Connection> connectionRef = new AtomicReference<>();
HttpClient client = HttpClient.create().doOnConnected(connectionRef::set).host(disposableServer.host()).port(disposableServer.port());
String uri = String.format("http://%s:%d/for-test/123", disposableServer.host(), disposableServer.port());
StepVerifier.create(client.get().uri(uri).response().doOnNext(response -> {
String location = response.responseHeaders().get(HttpHeaderNames.LOCATION);
String expectedLocation = uri.replace("http://", "https://");
assertThat(response.status()).isEqualTo(HttpResponseStatus.PERMANENT_REDIRECT);
assertThat(location).isEqualTo(expectedLocation);
assertThat(connectionRef.get().isDisposed()).isTrue();
})).expectNextCount(1).expectComplete().verify(Duration.ofSeconds(30));
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class ChannelOperationsHandler method channelActive.
@Override
public void channelActive(ChannelHandlerContext ctx) {
// fireChannelActive will be triggered regardless that the channel might be closed in the meantime
if (ctx.channel().isActive()) {
Connection c = Connection.from(ctx.channel());
listener.onStateChange(c, ConnectionObserver.State.CONNECTED);
ChannelOperations<?, ?> ops = opsFactory.create(c, listener, null);
if (ops != null) {
ops.bind();
listener.onStateChange(ops, ConnectionObserver.State.CONFIGURED);
}
}
}
Aggregations