Search in sources :

Example 21 with GenericFutureListener

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener in project vert.x by eclipse.

the class HttpServerWorker method handle.

@Override
public void handle(Channel ch) {
    if (HAProxyMessageCompletionHandler.canUseProxyProtocol(options.isUseProxyProtocol())) {
        IdleStateHandler idle;
        io.netty.util.concurrent.Promise<Channel> p = ch.eventLoop().newPromise();
        ch.pipeline().addLast(new HAProxyMessageDecoder());
        if (options.getProxyProtocolTimeout() > 0) {
            ch.pipeline().addLast("idle", idle = new IdleStateHandler(0, 0, options.getProxyProtocolTimeout(), options.getProxyProtocolTimeoutUnit()));
        } else {
            idle = null;
        }
        ch.pipeline().addLast(new HAProxyMessageCompletionHandler(p));
        p.addListener((GenericFutureListener<Future<Channel>>) future -> {
            if (future.isSuccess()) {
                if (idle != null) {
                    ch.pipeline().remove(idle);
                }
                configurePipeline(future.getNow());
            } else {
                handleException(future.cause());
            }
        });
    } else {
        configurePipeline(ch);
    }
}
Also used : HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) SniHandler(io.netty.handler.ssl.SniHandler) LoggingHandler(io.netty.handler.logging.LoggingHandler) FlashPolicyHandler(io.vertx.core.http.impl.cgbystrom.FlashPolicyHandler) ContextInternal(io.vertx.core.impl.ContextInternal) SslHandshakeCompletionHandler(io.vertx.core.net.impl.SslHandshakeCompletionHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) Supplier(java.util.function.Supplier) Unpooled(io.netty.buffer.Unpooled) HttpServerMetrics(io.vertx.core.spi.metrics.HttpServerMetrics) IdleState(io.netty.handler.timeout.IdleState) io.netty.channel(io.netty.channel) VertxHandler(io.vertx.core.net.impl.VertxHandler) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) VertxInternal(io.vertx.core.impl.VertxInternal) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) SSLHelper(io.vertx.core.net.impl.SSLHelper) StandardCharsets(java.nio.charset.StandardCharsets) HAProxyMessageDecoder(io.netty.handler.codec.haproxy.HAProxyMessageDecoder) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) EventLoopContext(io.vertx.core.impl.EventLoopContext) SslHandler(io.netty.handler.ssl.SslHandler) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) Future(io.netty.util.concurrent.Future) Handler(io.vertx.core.Handler) HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HAProxyMessageDecoder(io.netty.handler.codec.haproxy.HAProxyMessageDecoder) Future(io.netty.util.concurrent.Future)

Example 22 with GenericFutureListener

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener in project riposte by Nike-Inc.

the class DTraceEndHandlerTest method endDtrace_completes_the_trace_using_ChannelFutureListener_if_state_is_not_null_and_isResponseSendingLastChunkSent_returns_true.

@Test
public void endDtrace_completes_the_trace_using_ChannelFutureListener_if_state_is_not_null_and_isResponseSendingLastChunkSent_returns_true() throws Exception {
    // given
    assertThat(state.isTraceCompletedOrScheduled(), is(false));
    assertThat(state.isResponseSendingLastChunkSent(), is(true));
    assertThat(state.getDistributedTraceStack(), nullValue());
    Pair<Deque<Span>, Map<String, String>> expectedDtraceInfo = setupStateWithNewSpan("blahTrace");
    assertThat(state.getDistributedTraceStack(), notNullValue());
    assertThat(state.getDistributedTraceStack(), is(expectedDtraceInfo.getLeft()));
    assertThat(state.getDistributedTraceStack().size(), is(1));
    assertThat(state.isTracingResponseTaggingAndFinalSpanNameCompleted(), is(false));
    Span expectedSpan = expectedDtraceInfo.getLeft().peek();
    // when
    handlerSpy.endDtrace(ctxMock);
    // then
    // completeCurrentSpan() not immediately called, but scheduled
    verify(handlerSpy, never()).completeCurrentSpan();
    assertThat(state.isTraceCompletedOrScheduled(), is(true));
    // Response tagging was done.
    assertThat(state.isTracingResponseTaggingAndFinalSpanNameCompleted(), is(true));
    // Extract the listener that was attached to the last chunk future.
    GenericFutureListener lastChunkListener = extractChannelFutureListenerAddedToLastChunkFuture();
    assertThat(lastChunkListener, notNullValue());
    assertThat(lastChunkListener, instanceOf(ChannelFutureListenerWithTracingAndMdc.class));
    assertThat(Whitebox.getInternalState(lastChunkListener, "distributedTraceStackForExecution"), is(expectedDtraceInfo.getLeft()));
    assertThat(Whitebox.getInternalState(lastChunkListener, "mdcContextMapForExecution"), is(expectedDtraceInfo.getRight()));
    Consumer<ChannelFuture> embeddedListenerConsumer = (Consumer<ChannelFuture>) Whitebox.getInternalState(lastChunkListener, "postCompleteOperation");
    // Execute the embedded listener so we can validate what it does. Note that we can't verify using mockito spy verify(),
    // because the method call goes through the internal handler, not the spy impl. But we can still verify by
    // setting up the Tracer state to what we expect, execute the embedded listener, and verify subsequent Tracer state.
    AsyncNettyHelper.linkTracingAndMdcToCurrentThread(expectedDtraceInfo);
    assertThat(Tracer.getInstance().getCurrentSpan(), is(expectedSpan));
    embeddedListenerConsumer.accept(null);
    assertThat(Tracer.getInstance().getCurrentSpan(), nullValue());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Consumer(java.util.function.Consumer) ChannelFutureListenerWithTracingAndMdc(com.nike.riposte.util.asynchelperwrapper.ChannelFutureListenerWithTracingAndMdc) Deque(java.util.Deque) Map(java.util.Map) Span(com.nike.wingtips.Span) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) Test(org.junit.Test)

Example 23 with GenericFutureListener

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener in project ambry by linkedin.

the class MultiplexedChannelRecord method acquireClaimedStream.

void acquireClaimedStream(Promise<Channel> promise) {
    NettyUtils.doInEventLoop(parentChannel.eventLoop(), () -> {
        if (state != RecordState.OPEN) {
            String message;
            // GOAWAY
            if (state == RecordState.CLOSED_TO_NEW) {
                message = String.format("Connection %s received GOAWAY with Last Stream ID %d. Unable to open new " + "streams on this connection.", parentChannel, lastStreamId);
            } else {
                message = String.format("Connection %s was closed while acquiring new stream.", parentChannel);
            }
            log.warn(message);
            promise.setFailure(new IOException(message));
            return;
        }
        Future<Http2StreamChannel> streamFuture = new Http2StreamChannelBootstrap(parentChannel).handler(streamChannelInitializer).open();
        streamFuture.addListener((GenericFutureListener<Future<Http2StreamChannel>>) future -> {
            NettyUtils.warnIfNotInEventLoop(parentChannel.eventLoop());
            if (!future.isSuccess()) {
                promise.setFailure(future.cause());
                return;
            }
            Http2StreamChannel channel = future.getNow();
            streamChannels.put(channel.id(), channel);
            promise.setSuccess(channel);
            if (closeIfIdleTask == null && allowedIdleTimeInMs != null && allowedIdleTimeInMs > 0) {
                enableCloseIfIdleTask();
            }
        });
    }, promise);
}
Also used : Logger(org.slf4j.Logger) ChannelInitializer(io.netty.channel.ChannelInitializer) Promise(io.netty.util.concurrent.Promise) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) ChannelId(io.netty.channel.ChannelId) LoggerFactory(org.slf4j.LoggerFactory) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) IOException(java.io.IOException) HashMap(java.util.HashMap) Http2GoAwayFrame(io.netty.handler.codec.http2.Http2GoAwayFrame) ArrayList(java.util.ArrayList) NettyUtils(com.github.ambry.commons.NettyUtils) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) Http2StreamChannelBootstrap(io.netty.handler.codec.http2.Http2StreamChannelBootstrap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ChannelOutboundInvoker(io.netty.channel.ChannelOutboundInvoker) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel) Future(io.netty.util.concurrent.Future) Http2StreamChannelBootstrap(io.netty.handler.codec.http2.Http2StreamChannelBootstrap) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) Future(io.netty.util.concurrent.Future) IOException(java.io.IOException) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel)

Example 24 with GenericFutureListener

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener in project netty by netty.

the class Http2ConnectionHandlerTest method canSendGoAwayFrame.

@SuppressWarnings("unchecked")
@Test
public void canSendGoAwayFrame() throws Exception {
    ByteBuf data = dummyData();
    long errorCode = Http2Error.INTERNAL_ERROR.code();
    when(future.isDone()).thenReturn(true);
    when(future.isSuccess()).thenReturn(true);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((GenericFutureListener) invocation.getArgument(0)).operationComplete(future);
            return null;
        }
    }).when(future).addListener(any(GenericFutureListener.class));
    handler = newHandler();
    handler.goAway(ctx, STREAM_ID, errorCode, data, promise);
    verify(connection).goAwaySent(eq(STREAM_ID), eq(errorCode), eq(data));
    verify(frameWriter).writeGoAway(eq(ctx), eq(STREAM_ID), eq(errorCode), eq(data), eq(promise));
    verify(ctx).close();
    assertEquals(0, data.refCnt());
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) ByteBuf(io.netty.buffer.ByteBuf) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) Test(org.junit.jupiter.api.Test)

Example 25 with GenericFutureListener

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.GenericFutureListener in project netty by netty.

the class FixedChannelPoolTest method testCloseAsync.

@Test
public void testCloseAsync() throws ExecutionException, InterruptedException {
    LocalAddress addr = new LocalAddress(getLocalAddrId());
    Bootstrap cb = new Bootstrap();
    cb.remoteAddress(addr);
    cb.group(group).channel(LocalChannel.class);
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
        }
    });
    // Start server
    final Channel sc = sb.bind(addr).syncUninterruptibly().channel();
    final FixedChannelPool pool = new FixedChannelPool(cb, new TestChannelPoolHandler(), 2);
    pool.acquire().get();
    pool.acquire().get();
    final ChannelPromise closePromise = sc.newPromise();
    pool.closeAsync().addListener(new GenericFutureListener<Future<? super Void>>() {

        @Override
        public void operationComplete(Future<? super Void> future) throws Exception {
            assertEquals(0, pool.acquiredChannelCount());
            sc.close(closePromise).syncUninterruptibly();
        }
    }).awaitUninterruptibly();
    closePromise.awaitUninterruptibly();
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Channel(io.netty.channel.Channel) ChannelPromise(io.netty.channel.ChannelPromise) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Future(io.netty.util.concurrent.Future) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Aggregations

GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)26 ChannelFuture (io.netty.channel.ChannelFuture)11 Future (io.netty.util.concurrent.Future)11 Channel (io.netty.channel.Channel)7 InetSocketAddress (java.net.InetSocketAddress)7 Map (java.util.Map)7 Future (io.vertx.core.Future)5 Handler (io.vertx.core.Handler)5 ContextInternal (io.vertx.core.impl.ContextInternal)5 VertxInternal (io.vertx.core.impl.VertxInternal)5 IOException (java.io.IOException)5 Bootstrap (io.netty.bootstrap.Bootstrap)4 ChannelFutureListener (io.netty.channel.ChannelFutureListener)4 ChannelOption (io.netty.channel.ChannelOption)4 AsyncResult (io.vertx.core.AsyncResult)4 PromiseInternal (io.vertx.core.impl.future.PromiseInternal)4 SocketAddress (io.vertx.core.net.SocketAddress)4 List (java.util.List)4 Test (org.junit.Test)4 Logger (org.slf4j.Logger)4