Search in sources :

Example 76 with Connection

use of reactor.netty.Connection in project reactor-netty by reactor.

the class ContextAwareChannelMetricsHandler method recordException.

@Override
protected void recordException(ChannelHandlerContext ctx, SocketAddress address) {
    Connection connection = Connection.from(ctx.channel());
    ChannelOperations<?, ?> ops = connection.as(ChannelOperations.class);
    if (ops != null) {
        recorder().incrementErrorsCount(ops.currentContext(), address);
    } else if (connection instanceof ConnectionObserver) {
        recorder().incrementErrorsCount(((ConnectionObserver) connection).currentContext(), address);
    } else {
        super.recordException(ctx, address);
    }
}
Also used : ConnectionObserver(reactor.netty.ConnectionObserver) Connection(reactor.netty.Connection)

Example 77 with Connection

use of reactor.netty.Connection in project reactor-netty by reactor.

the class ChannelOperationsHandler method channelInactive.

@Override
public final void channelInactive(ChannelHandlerContext ctx) {
    try {
        Connection connection = Connection.from(ctx.channel());
        ChannelOperations<?, ?> ops = connection.as(ChannelOperations.class);
        if (ops != null) {
            ops.onInboundClose();
        } else {
            listener.onStateChange(connection, ConnectionObserver.State.DISCONNECTING);
        }
    } catch (Throwable err) {
        exceptionCaught(ctx, err);
    }
}
Also used : Connection(reactor.netty.Connection)

Example 78 with Connection

use of reactor.netty.Connection in project reactor-netty by reactor.

the class ClientTransport method connect.

/**
 * Connect the {@link ClientTransport} and return a {@link Mono} of {@link Connection}. If
 * {@link Mono} is cancelled, the underlying connection will be aborted. Once the
 * {@link Connection} has been emitted and is not necessary anymore, disposing must be
 * done by the user via {@link Connection#dispose()}.
 *
 * @return a {@link Mono} of {@link Connection}
 */
protected Mono<? extends Connection> connect() {
    CONF config = configuration();
    ConnectionObserver observer = config.defaultConnectionObserver().then(config.observer);
    AddressResolverGroup<?> resolver = config.resolverInternal();
    Mono<? extends Connection> mono = config.connectionProvider().acquire(config, observer, config.remoteAddress, resolver);
    if (config.doOnConnect != null) {
        mono = mono.doOnSubscribe(s -> config.doOnConnect.accept(config));
    }
    return mono;
}
Also used : ConnectionObserver(reactor.netty.ConnectionObserver) LoopResources(reactor.netty.resources.LoopResources) Properties(java.util.Properties) SocketAddress(java.net.SocketAddress) Mono(reactor.core.publisher.Mono) Supplier(java.util.function.Supplier) Objects(java.util.Objects) Consumer(java.util.function.Consumer) NoopAddressResolverGroup(io.netty.resolver.NoopAddressResolverGroup) ConnectionObserver(reactor.netty.ConnectionObserver) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) Duration(java.time.Duration) BiConsumer(java.util.function.BiConsumer) Connection(reactor.netty.Connection)

Example 79 with Connection

use of reactor.netty.Connection in project reactor-netty by reactor.

the class Http2Pool method destroyPoolable.

Mono<Void> destroyPoolable(Http2PooledRef ref) {
    Mono<Void> mono = Mono.empty();
    try {
        if (ref.slot.decrementConcurrencyAndGet() == 0) {
            ref.slot.invalidate();
            Connection connection = ref.poolable();
            Http2FrameCodec frameCodec = connection.channel().pipeline().get(Http2FrameCodec.class);
            if (frameCodec != null) {
                releaseConnection(connection);
            }
        }
    } catch (Throwable destroyFunctionError) {
        mono = Mono.error(destroyFunctionError);
    }
    return mono;
}
Also used : Connection(reactor.netty.Connection) Http2FrameCodec(io.netty.handler.codec.http2.Http2FrameCodec)

Example 80 with Connection

use of reactor.netty.Connection in project reactor-netty by reactor.

the class HttpServerTests method httpPipelining.

@Test
void httpPipelining() throws Exception {
    AtomicInteger i = new AtomicInteger();
    disposableServer = createServer().handle((req, resp) -> resp.header(HttpHeaderNames.CONTENT_LENGTH, "1").sendString(Mono.just(i.incrementAndGet()).flatMap(d -> Mono.delay(Duration.ofSeconds(4 - d)).map(x -> d + "\n")))).bindNow();
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/plaintext");
    CountDownLatch latch = new CountDownLatch(6);
    Connection client = TcpClient.create().port(disposableServer.port()).handle((in, out) -> {
        in.withConnection(x -> x.addHandlerFirst(new HttpClientCodec())).receiveObject().ofType(DefaultHttpContent.class).as(ByteBufFlux::fromInbound).asString().log().map(Integer::parseInt).subscribe(d -> {
            for (int x = 0; x < d; x++) {
                latch.countDown();
            }
        });
        return out.sendObject(Flux.just(request.retain(), request.retain(), request.retain())).neverComplete();
    }).wiretap(true).connectNow();
    assertThat(latch.await(45, TimeUnit.SECONDS)).as("latch await").isTrue();
    client.disposeNow();
}
Also used : AttributeKey(io.netty.util.AttributeKey) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) SNIHostName(javax.net.ssl.SNIHostName) AbortedException(reactor.netty.channel.AbortedException) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) HttpMessage(io.netty.handler.codec.http.HttpMessage) ByteBufHolder(io.netty.buffer.ByteBufHolder) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) TcpClient(reactor.netty.tcp.TcpClient) ConnectionObserver(reactor.netty.ConnectionObserver) Future(java.util.concurrent.Future) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) DEFAULT_FORM_DECODER_SPEC(reactor.netty.http.server.HttpServerFormDecoderProvider.DEFAULT_FORM_DECODER_SPEC) Path(java.nio.file.Path) LoopResources(reactor.netty.resources.LoopResources) HttpObjectDecoder(io.netty.handler.codec.http.HttpObjectDecoder) HttpRequest(io.netty.handler.codec.http.HttpRequest) Context(reactor.util.context.Context) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) Assertions.fail(org.assertj.core.api.Assertions.fail) NettyOutbound(reactor.netty.NettyOutbound) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) GZIPOutputStream(java.util.zip.GZIPOutputStream) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HttpClient(reactor.netty.http.client.HttpClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ChannelOption(io.netty.channel.ChannelOption) HttpProtocol(reactor.netty.http.HttpProtocol) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ValueSource(org.junit.jupiter.params.provider.ValueSource) PrematureCloseException(reactor.netty.http.client.PrematureCloseException) SslContext(io.netty.handler.ssl.SslContext) Publisher(org.reactivestreams.Publisher) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) Field(java.lang.reflect.Field) ReferenceCounted(io.netty.util.ReferenceCounted) Channel(io.netty.channel.Channel) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultEventExecutor(io.netty.util.concurrent.DefaultEventExecutor) Flux(reactor.core.publisher.Flux) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) FutureMono(reactor.netty.FutureMono) Paths(java.nio.file.Paths) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) ServerCookieEncoder(io.netty.handler.codec.http.cookie.ServerCookieEncoder) HttpUtil(io.netty.handler.codec.http.HttpUtil) SocketAddress(java.net.SocketAddress) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) TimeoutException(java.util.concurrent.TimeoutException) BaseHttpTest(reactor.netty.BaseHttpTest) Random(java.util.Random) Unpooled(io.netty.buffer.Unpooled) GlobalEventExecutor(io.netty.util.concurrent.GlobalEventExecutor) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) TransportConfig(reactor.netty.transport.TransportConfig) ServerCookieDecoder(io.netty.handler.codec.http.cookie.ServerCookieDecoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteBufFlux(reactor.netty.ByteBufFlux) NettyPipeline(reactor.netty.NettyPipeline) MethodSource(org.junit.jupiter.params.provider.MethodSource) ChannelGroup(io.netty.channel.group.ChannelGroup) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) SignalType(reactor.core.publisher.SignalType) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ChannelBindException(reactor.netty.ChannelBindException) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) DisposableServer(reactor.netty.DisposableServer) HttpVersion(io.netty.handler.codec.http.HttpVersion) Tuple3(reactor.util.function.Tuple3) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketCloseStatus(io.netty.handler.codec.http.websocketx.WebSocketCloseStatus) Tuple2(reactor.util.function.Tuple2) HttpClientRequest(reactor.netty.http.client.HttpClientRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SniCompletionEvent(io.netty.handler.ssl.SniCompletionEvent) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Connection(reactor.netty.Connection) ExecutorService(java.util.concurrent.ExecutorService) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpMethod(io.netty.handler.codec.http.HttpMethod) CertificateException(java.security.cert.CertificateException) TimeUnit(java.util.concurrent.TimeUnit) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) TcpServer(reactor.netty.tcp.TcpServer) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) Comparator(java.util.Comparator) Timeout(org.junit.jupiter.api.Timeout) ByteBufFlux(reactor.netty.ByteBufFlux) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Connection(reactor.netty.Connection) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Aggregations

Connection (reactor.netty.Connection)137 Test (org.junit.jupiter.api.Test)69 CountDownLatch (java.util.concurrent.CountDownLatch)52 Duration (java.time.Duration)49 Mono (reactor.core.publisher.Mono)47 InetSocketAddress (java.net.InetSocketAddress)41 TimeUnit (java.util.concurrent.TimeUnit)39 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)38 LoopResources (reactor.netty.resources.LoopResources)38 ByteBuf (io.netty.buffer.ByteBuf)35 Flux (reactor.core.publisher.Flux)35 DisposableServer (reactor.netty.DisposableServer)35 DomainSocketAddress (io.netty.channel.unix.DomainSocketAddress)33 AtomicReference (java.util.concurrent.atomic.AtomicReference)32 List (java.util.List)31 IOException (java.io.IOException)30 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)30 ChannelOption (io.netty.channel.ChannelOption)29 Unpooled (io.netty.buffer.Unpooled)28 Charset (java.nio.charset.Charset)28