use of reactor.netty.Connection in project reactor-netty by reactor.
the class ChannelOperationsHandler method exceptionCaught.
@Override
public final void exceptionCaught(ChannelHandlerContext ctx, Throwable err) {
Connection connection = Connection.from(ctx.channel());
ChannelOperations<?, ?> ops = connection.as(ChannelOperations.class);
if (ops != null) {
ops.onInboundError(err);
} else {
listener.onUncaughtException(connection, err);
}
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class HttpProtocolsTests method doTestResponseTimeout.
private void doTestResponseTimeout(HttpClient client, long expectedTimeout) throws Exception {
AtomicBoolean onRequest = new AtomicBoolean();
AtomicBoolean onResponse = new AtomicBoolean();
AtomicBoolean onDisconnected = new AtomicBoolean();
AtomicLong timeout = new AtomicLong();
Predicate<Connection> handlerAvailable = conn -> conn.channel().pipeline().get(NettyPipeline.ResponseTimeoutHandler) != null;
HttpClient localClient = client.doOnRequest((req, conn) -> onRequest.set(handlerAvailable.test(conn))).doOnResponse((req, conn) -> {
if (handlerAvailable.test(conn)) {
ChannelHandler handler = conn.channel().pipeline().get(NettyPipeline.ResponseTimeoutHandler);
onResponse.set(true);
timeout.set(((ReadTimeoutHandler) handler).getReaderIdleTimeInMillis());
}
}).doOnDisconnected(conn -> onDisconnected.set(handlerAvailable.test(conn)));
Mono<String> response = localClient.get().uri("/").responseContent().aggregate().asString();
StepVerifier.create(response).expectNext("testProtocolVariationsResponseTimeout").expectComplete().verify(Duration.ofSeconds(30));
assertThat(onRequest.get()).isFalse();
assertThat(onResponse.get()).isTrue();
assertThat(onDisconnected.get()).isFalse();
assertThat(timeout.get()).isEqualTo(expectedTimeout);
Thread.sleep(expectedTimeout + 50);
StepVerifier.create(response).expectNext("testProtocolVariationsResponseTimeout").expectComplete().verify(Duration.ofSeconds(30));
assertThat(onRequest.get()).isFalse();
assertThat(onResponse.get()).isTrue();
assertThat(onDisconnected.get()).isFalse();
assertThat(timeout.get()).isEqualTo(expectedTimeout);
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class HttpServerOperations method sendDecodingFailures.
static void sendDecodingFailures(ChannelHandlerContext ctx, ConnectionObserver listener, boolean secure, Throwable t, Object msg) {
Connection conn = Connection.from(ctx.channel());
Throwable cause = t.getCause() != null ? t.getCause() : t;
if (log.isWarnEnabled()) {
log.warn(format(ctx.channel(), "Decoding failed: " + msg + " : "), cause);
}
ReferenceCountUtil.release(msg);
HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, cause instanceof TooLongFrameException ? HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE : HttpResponseStatus.BAD_REQUEST);
response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 0).set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
HttpRequest request = null;
if (msg instanceof HttpRequest) {
request = (HttpRequest) msg;
}
listener.onStateChange(new FailedHttpServerRequest(conn, listener, request, response, secure), REQUEST_DECODING_FAILED);
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class DefaultPooledConnectionProviderTest method testRetryConnect.
@Test
@SuppressWarnings("unchecked")
void testRetryConnect() throws Exception {
EventLoopGroup group = new NioEventLoopGroup(1);
InetSocketAddress address = AddressUtils.createUnresolved("localhost", 12122);
AddressResolverGroup<SocketAddress> resolverGroup = Mockito.mock(AddressResolverGroup.class);
AddressResolver<SocketAddress> resolver = Mockito.mock(AddressResolver.class);
io.netty.util.concurrent.Future<List<SocketAddress>> resolveFuture = Mockito.mock(io.netty.util.concurrent.Future.class);
List<SocketAddress> resolveAllResult = Arrays.asList(// connection refused
new InetSocketAddress(NetUtil.LOCALHOST4, 12122), // connection refused
new InetSocketAddress(NetUtil.LOCALHOST6, 12122), // connection established
new InetSocketAddress("example.com", 80));
Mockito.when(resolverGroup.getResolver(group.next())).thenReturn(resolver);
Mockito.when(resolver.isSupported(address)).thenReturn(true);
Mockito.when(resolver.isResolved(address)).thenReturn(false);
Mockito.when(resolver.resolveAll(address)).thenReturn(resolveFuture);
Mockito.when(resolveFuture.isDone()).thenReturn(true);
Mockito.when(resolveFuture.cause()).thenReturn(null);
Mockito.when(resolveFuture.getNow()).thenReturn(resolveAllResult);
ConnectionProvider pool = ConnectionProvider.create("testRetryConnect", 1);
Supplier<? extends SocketAddress> remoteAddress = () -> address;
ConnectionObserver observer = ConnectionObserver.emptyListener();
ClientTransportConfigImpl config = new ClientTransportConfigImpl(group, pool, Collections.emptyMap(), remoteAddress, resolverGroup);
Connection conn = null;
try {
conn = pool.acquire(config, observer, remoteAddress, config.resolverInternal()).block(Duration.ofSeconds(5));
assertThat(((InetSocketAddress) conn.address()).getHostString()).isEqualTo("example.com");
} finally {
if (conn != null) {
conn.disposeNow(Duration.ofSeconds(5));
}
pool.disposeLater().block(Duration.ofSeconds(5));
group.shutdownGracefully().get(5, TimeUnit.SECONDS);
}
}
use of reactor.netty.Connection in project reactor-netty by reactor.
the class DefaultPooledConnectionProviderTest method testDisposeInactivePoolsInBackground.
@Test
void testDisposeInactivePoolsInBackground() throws Exception {
EventLoopGroup group = new NioEventLoopGroup(1);
InetSocketAddress address = AddressUtils.createUnresolved("example.com", 80);
ConnectionProvider.Builder builder = ConnectionProvider.builder("testDisposeInactivePoolsInBackground").maxConnections(1).disposeInactivePoolsInBackground(Duration.ofMillis(100), Duration.ofSeconds(1));
VirtualTimeScheduler vts = VirtualTimeScheduler.create();
DefaultPooledConnectionProvider pool = new DefaultPooledConnectionProvider(builder, SchedulerClock.of(vts));
Supplier<? extends SocketAddress> remoteAddress = () -> address;
ConnectionObserver observer = ConnectionObserver.emptyListener();
ClientTransportConfigImpl config = new ClientTransportConfigImpl(group, pool, Collections.emptyMap(), remoteAddress, DefaultAddressResolverGroup.INSTANCE);
Connection conn = null;
try {
conn = pool.acquire(config, observer, remoteAddress, config.resolverInternal()).block(Duration.ofSeconds(5));
assertThat(((InetSocketAddress) conn.address()).getHostString()).isEqualTo("example.com");
} finally {
if (conn != null) {
CountDownLatch latch = new CountDownLatch(1);
conn.channel().close().addListener(f -> latch.countDown());
assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
}
assertThat(pool.channelPools.size()).isEqualTo(1);
vts.advanceTimeBy(Duration.ofSeconds(5));
await().atMost(500, TimeUnit.MILLISECONDS).with().pollInterval(10, TimeUnit.MILLISECONDS).untilAsserted(() -> assertThat(pool.channelPools.size()).isEqualTo(0));
pool.disposeLater().block(Duration.ofSeconds(5));
group.shutdownGracefully().get(5, TimeUnit.SECONDS);
}
}
Aggregations