Search in sources :

Example 71 with ChannelFuture

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project grpc-java by grpc.

the class NettyClientHandlerTest method receivedGoAwayShouldCancelBufferedStream.

@Test
public void receivedGoAwayShouldCancelBufferedStream() throws Exception {
    // Force the stream to be buffered.
    receiveMaxConcurrentStreams(0);
    ChannelFuture future = enqueue(new CreateStreamCommand(grpcHeaders, streamTransportState));
    channelRead(goAwayFrame(0));
    assertTrue(future.isDone());
    assertFalse(future.isSuccess());
    Status status = Status.fromThrowable(future.cause());
    assertEquals(Status.Code.UNAVAILABLE, status.getCode());
    assertEquals("HTTP/2 error code: NO_ERROR\nReceived Goaway", status.getDescription());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Status(io.grpc.Status) Test(org.junit.Test)

Example 72 with ChannelFuture

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project grpc-java by grpc.

the class NettyClientHandlerTest method receivedGoAwayShouldFailUnknownBufferedStreams.

@Test
public void receivedGoAwayShouldFailUnknownBufferedStreams() throws Exception {
    receiveMaxConcurrentStreams(0);
    ChannelFuture future = enqueue(new CreateStreamCommand(grpcHeaders, streamTransportState));
    // Read a GOAWAY that indicates our stream was never processed by the server.
    channelRead(goAwayFrame(0, 8, /* Cancel */
    Unpooled.copiedBuffer("this is a test", UTF_8)));
    assertTrue(future.isDone());
    assertFalse(future.isSuccess());
    Status status = Status.fromThrowable(future.cause());
    assertEquals(Status.CANCELLED.getCode(), status.getCode());
    assertEquals("HTTP/2 error code: CANCEL\nReceived Goaway\nthis is a test", status.getDescription());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Status(io.grpc.Status) Test(org.junit.Test)

Example 73 with ChannelFuture

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project rest.li by linkedin.

the class RAPResponseDecoder method channelRead0.

@Override
protected void channelRead0(final ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    if (msg instanceof HttpResponse) {
        HttpResponse m = (HttpResponse) msg;
        _shouldCloseConnection = !HttpUtil.isKeepAlive(m);
        if (HttpUtil.is100ContinueExpected(m)) {
            ctx.writeAndFlush(CONTINUE).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        ctx.fireExceptionCaught(future.cause());
                    }
                }
            });
        }
        if (!m.decoderResult().isSuccess()) {
            ctx.fireExceptionCaught(m.decoderResult().cause());
            return;
        }
        // remove chunked encoding.
        if (HttpUtil.isTransferEncodingChunked(m)) {
            HttpUtil.setTransferEncodingChunked(m, false);
        }
        Timeout<None> timeout = ctx.channel().attr(TIMEOUT_ATTR_KEY).getAndRemove();
        if (timeout == null) {
            LOG.debug("dropped a response after channel inactive or exception had happened.");
            return;
        }
        final TimeoutBufferedWriter writer = new TimeoutBufferedWriter(ctx, _maxContentLength, BUFFER_HIGH_WATER_MARK, BUFFER_LOW_WATER_MARK, timeout);
        EntityStream entityStream = EntityStreams.newEntityStream(writer);
        _chunkedMessageWriter = writer;
        StreamResponseBuilder builder = new StreamResponseBuilder();
        builder.setStatus(m.status().code());
        for (Map.Entry<String, String> e : m.headers()) {
            String key = e.getKey();
            String value = e.getValue();
            if (key.equalsIgnoreCase(HttpConstants.RESPONSE_COOKIE_HEADER_NAME)) {
                builder.addCookie(value);
            } else {
                builder.unsafeAddHeaderValue(key, value);
            }
        }
        ctx.fireChannelRead(builder.build(entityStream));
    } else if (msg instanceof HttpContent) {
        HttpContent chunk = (HttpContent) msg;
        TimeoutBufferedWriter currentWriter = _chunkedMessageWriter;
        // Sanity check
        if (currentWriter == null) {
            throw new IllegalStateException("received " + HttpContent.class.getSimpleName() + " without " + HttpResponse.class.getSimpleName());
        }
        if (!chunk.decoderResult().isSuccess()) {
            this.exceptionCaught(ctx, chunk.decoderResult().cause());
        }
        currentWriter.processHttpChunk(chunk);
        if (chunk instanceof LastHttpContent) {
            _chunkedMessageWriter = null;
        }
    } else {
        // something must be wrong, but let's proceed so that
        // handler after us has a chance to process it.
        ctx.fireChannelRead(msg);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ByteString(com.linkedin.data.ByteString) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ChannelFutureListener(io.netty.channel.ChannelFutureListener) TimeoutException(java.util.concurrent.TimeoutException) ClosedChannelException(java.nio.channels.ClosedChannelException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) IOException(java.io.IOException) EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) None(com.linkedin.common.util.None) Map(java.util.Map) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 74 with ChannelFuture

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project rest.li by linkedin.

the class ChannelPoolLifecycle method create.

@Override
public void create(final Callback<Channel> channelCallback) {
    final long start = System.currentTimeMillis();
    _bootstrap.connect(_remoteAddress).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture channelFuture) throws Exception {
            if (channelFuture.isSuccess()) {
                synchronized (_createTimeTracker) {
                    _createTimeTracker.addValue(System.currentTimeMillis() - start);
                }
                Channel c = channelFuture.channel();
                if (_tcpNoDelay) {
                    c.config().setOption(ChannelOption.TCP_NODELAY, true);
                }
                _channelGroup.add(c);
                channelCallback.onSuccess(c);
            } else {
                Throwable cause = channelFuture.cause();
                if (cause instanceof ConnectException) {
                    channelCallback.onError(new RetriableRequestException(cause));
                } else {
                    channelCallback.onError(HttpNettyStreamClient.toException(cause));
                }
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RetriableRequestException(com.linkedin.r2.RetriableRequestException) Channel(io.netty.channel.Channel) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ConnectException(java.net.ConnectException) RetriableRequestException(com.linkedin.r2.RetriableRequestException) ConnectException(java.net.ConnectException)

Example 75 with ChannelFuture

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project rest.li by linkedin.

the class Http2AlpnHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof RequestWithCallback)) {
        ctx.write(msg, promise);
        return;
    }
    _alpnPromise.addListener(f -> {
        ChannelFuture future = (ChannelFuture) f;
        if (future.isSuccess()) {
            ctx.write(msg, promise);
        } else {
            @SuppressWarnings("unchecked") TimeoutAsyncPoolHandle<?> handle = ((RequestWithCallback<?, ?, TimeoutAsyncPoolHandle<?>>) msg).handle();
            handle.error().release();
            TransportCallback<?> callback = ((RequestWithCallback) msg).callback();
            callback.onResponse(TransportResponseImpl.error(future.cause()));
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback)

Aggregations

ChannelFuture (io.netty.channel.ChannelFuture)936 Channel (io.netty.channel.Channel)271 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)246 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)239 Bootstrap (io.netty.bootstrap.Bootstrap)219 InetSocketAddress (java.net.InetSocketAddress)214 ChannelFutureListener (io.netty.channel.ChannelFutureListener)206 Test (org.junit.Test)195 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)194 ByteBuf (io.netty.buffer.ByteBuf)192 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)178 EventLoopGroup (io.netty.channel.EventLoopGroup)167 IOException (java.io.IOException)155 ChannelPipeline (io.netty.channel.ChannelPipeline)151 SocketChannel (io.netty.channel.socket.SocketChannel)118 ArrayList (java.util.ArrayList)118 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)116 ChannelInitializer (io.netty.channel.ChannelInitializer)115 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)112 AtomicReference (java.util.concurrent.atomic.AtomicReference)111