Search in sources :

Example 1 with DefaultHttp2LocalFlowController

use of io.netty.handler.codec.http2.DefaultHttp2LocalFlowController in project grpc-java by grpc.

the class NettyServerHandler method newHandler.

@VisibleForTesting
static NettyServerHandler newHandler(Http2FrameReader frameReader, Http2FrameWriter frameWriter, ServerTransportListener transportListener, int maxStreams, int flowControlWindow, int maxHeaderListSize, int maxMessageSize) {
    Preconditions.checkArgument(maxStreams > 0, "maxStreams must be positive");
    Preconditions.checkArgument(flowControlWindow > 0, "flowControlWindow must be positive");
    Preconditions.checkArgument(maxHeaderListSize > 0, "maxHeaderListSize must be positive");
    Preconditions.checkArgument(maxMessageSize > 0, "maxMessageSize must be positive");
    Http2Connection connection = new DefaultHttp2Connection(true);
    // Create the local flow controller configured to auto-refill the connection window.
    connection.local().flowController(new DefaultHttp2LocalFlowController(connection, DEFAULT_WINDOW_UPDATE_RATIO, true));
    Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter);
    // TODO(ejona): swap back to DefaultHttp2Connection with Netty-4.1.9
    Http2ConnectionDecoder decoder = new FixedHttp2ConnectionDecoder(connection, encoder, frameReader);
    Http2Settings settings = new Http2Settings();
    settings.initialWindowSize(flowControlWindow);
    settings.maxConcurrentStreams(maxStreams);
    settings.maxHeaderListSize(maxHeaderListSize);
    return new NettyServerHandler(transportListener, decoder, encoder, settings, maxMessageSize);
}
Also used : Http2ConnectionDecoder(io.netty.handler.codec.http2.Http2ConnectionDecoder) DefaultHttp2Connection(io.netty.handler.codec.http2.DefaultHttp2Connection) DefaultHttp2Connection(io.netty.handler.codec.http2.DefaultHttp2Connection) Http2Connection(io.netty.handler.codec.http2.Http2Connection) DefaultHttp2LocalFlowController(io.netty.handler.codec.http2.DefaultHttp2LocalFlowController) Http2ConnectionEncoder(io.netty.handler.codec.http2.Http2ConnectionEncoder) DefaultHttp2ConnectionEncoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder) Http2Settings(io.netty.handler.codec.http2.Http2Settings) DefaultHttp2ConnectionEncoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with DefaultHttp2LocalFlowController

use of io.netty.handler.codec.http2.DefaultHttp2LocalFlowController in project grpc-java by grpc.

the class NettyClientHandler method newHandler.

@VisibleForTesting
static NettyClientHandler newHandler(Http2Connection connection, Http2FrameReader frameReader, Http2FrameWriter frameWriter, ClientTransportLifecycleManager lifecycleManager, KeepAliveManager keepAliveManager, int flowControlWindow, int maxHeaderListSize, Ticker ticker) {
    Preconditions.checkNotNull(connection, "connection");
    Preconditions.checkNotNull(frameReader, "frameReader");
    Preconditions.checkNotNull(lifecycleManager, "lifecycleManager");
    Preconditions.checkArgument(flowControlWindow > 0, "flowControlWindow must be positive");
    Preconditions.checkArgument(maxHeaderListSize > 0, "maxHeaderListSize must be positive");
    Preconditions.checkNotNull(ticker, "ticker");
    Http2FrameLogger frameLogger = new Http2FrameLogger(LogLevel.DEBUG, NettyClientHandler.class);
    frameReader = new Http2InboundFrameLogger(frameReader, frameLogger);
    frameWriter = new Http2OutboundFrameLogger(frameWriter, frameLogger);
    StreamBufferingEncoder encoder = new StreamBufferingEncoder(new DefaultHttp2ConnectionEncoder(connection, frameWriter));
    // Create the local flow controller configured to auto-refill the connection window.
    connection.local().flowController(new DefaultHttp2LocalFlowController(connection, DEFAULT_WINDOW_UPDATE_RATIO, true));
    // TODO(ejona): swap back to DefaultHttp2Connection with Netty-4.1.9
    Http2ConnectionDecoder decoder = new FixedHttp2ConnectionDecoder(connection, encoder, frameReader);
    Http2Settings settings = new Http2Settings();
    settings.pushEnabled(false);
    settings.initialWindowSize(flowControlWindow);
    settings.maxConcurrentStreams(0);
    settings.maxHeaderListSize(maxHeaderListSize);
    return new NettyClientHandler(decoder, encoder, settings, lifecycleManager, keepAliveManager, ticker);
}
Also used : Http2ConnectionDecoder(io.netty.handler.codec.http2.Http2ConnectionDecoder) Http2InboundFrameLogger(io.netty.handler.codec.http2.Http2InboundFrameLogger) StreamBufferingEncoder(io.netty.handler.codec.http2.StreamBufferingEncoder) Http2FrameLogger(io.netty.handler.codec.http2.Http2FrameLogger) DefaultHttp2LocalFlowController(io.netty.handler.codec.http2.DefaultHttp2LocalFlowController) Http2OutboundFrameLogger(io.netty.handler.codec.http2.Http2OutboundFrameLogger) Http2Settings(io.netty.handler.codec.http2.Http2Settings) DefaultHttp2ConnectionEncoder(io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with DefaultHttp2LocalFlowController

use of io.netty.handler.codec.http2.DefaultHttp2LocalFlowController in project netty by netty.

the class DataCompressionHttp2Test method bootstrapEnv.

private void bootstrapEnv(int serverOutSize) throws Exception {
    final CountDownLatch prefaceWrittenLatch = new CountDownLatch(1);
    serverOut = new ByteArrayOutputStream(serverOutSize);
    serverLatch = new CountDownLatch(1);
    sb = new ServerBootstrap();
    cb = new Bootstrap();
    // Streams are created before the normal flow for this test, so these connection must be initialized up front.
    serverConnection = new DefaultHttp2Connection(true);
    clientConnection = new DefaultHttp2Connection(false);
    serverConnection.addListener(new Http2ConnectionAdapter() {

        @Override
        public void onStreamClosed(Http2Stream stream) {
            serverLatch.countDown();
        }
    });
    doAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock in) throws Throwable {
            ByteBuf buf = (ByteBuf) in.getArguments()[2];
            int padding = (Integer) in.getArguments()[3];
            int processedBytes = buf.readableBytes() + padding;
            buf.readBytes(serverOut, buf.readableBytes());
            if (in.getArgument(4)) {
                serverConnection.stream((Integer) in.getArgument(1)).close();
            }
            return processedBytes;
        }
    }).when(serverListener).onDataRead(any(ChannelHandlerContext.class), anyInt(), any(ByteBuf.class), anyInt(), anyBoolean());
    final CountDownLatch serverChannelLatch = new CountDownLatch(1);
    sb.group(new NioEventLoopGroup(), new NioEventLoopGroup());
    sb.channel(NioServerSocketChannel.class);
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            serverConnectedChannel = ch;
            ChannelPipeline p = ch.pipeline();
            Http2FrameWriter frameWriter = new DefaultHttp2FrameWriter();
            serverConnection.remote().flowController(new DefaultHttp2RemoteFlowController(serverConnection));
            serverConnection.local().flowController(new DefaultHttp2LocalFlowController(serverConnection).frameWriter(frameWriter));
            Http2ConnectionEncoder encoder = new CompressorHttp2ConnectionEncoder(new DefaultHttp2ConnectionEncoder(serverConnection, frameWriter));
            Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(serverConnection, encoder, new DefaultHttp2FrameReader());
            Http2ConnectionHandler connectionHandler = new Http2ConnectionHandlerBuilder().frameListener(new DelegatingDecompressorFrameListener(serverConnection, serverListener)).codec(decoder, encoder).build();
            p.addLast(connectionHandler);
            serverChannelLatch.countDown();
        }
    });
    cb.group(new NioEventLoopGroup());
    cb.channel(NioSocketChannel.class);
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            Http2FrameWriter frameWriter = new DefaultHttp2FrameWriter();
            clientConnection.remote().flowController(new DefaultHttp2RemoteFlowController(clientConnection));
            clientConnection.local().flowController(new DefaultHttp2LocalFlowController(clientConnection).frameWriter(frameWriter));
            clientEncoder = new CompressorHttp2ConnectionEncoder(new DefaultHttp2ConnectionEncoder(clientConnection, frameWriter));
            Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(clientConnection, clientEncoder, new DefaultHttp2FrameReader());
            clientHandler = new Http2ConnectionHandlerBuilder().frameListener(new DelegatingDecompressorFrameListener(clientConnection, clientListener)).gracefulShutdownTimeoutMillis(0).codec(decoder, clientEncoder).build();
            p.addLast(clientHandler);
            p.addLast(new ChannelInboundHandlerAdapter() {

                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                    if (evt instanceof Http2ConnectionPrefaceWrittenEvent) {
                        prefaceWrittenLatch.countDown();
                        ctx.pipeline().remove(this);
                    }
                }
            });
        }
    });
    serverChannel = sb.bind(new InetSocketAddress(0)).sync().channel();
    int port = ((InetSocketAddress) serverChannel.localAddress()).getPort();
    ChannelFuture ccf = cb.connect(new InetSocketAddress(NetUtil.LOCALHOST, port));
    assertTrue(ccf.awaitUninterruptibly().isSuccess());
    clientChannel = ccf.channel();
    assertTrue(prefaceWrittenLatch.await(5, SECONDS));
    assertTrue(serverChannelLatch.await(5, SECONDS));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Http2TestUtil.runInChannel(io.netty.handler.codec.http2.Http2TestUtil.runInChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) IOException(java.io.IOException) ChannelPipeline(io.netty.channel.ChannelPipeline) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 DefaultHttp2ConnectionEncoder (io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder)2 DefaultHttp2LocalFlowController (io.netty.handler.codec.http2.DefaultHttp2LocalFlowController)2 Http2ConnectionDecoder (io.netty.handler.codec.http2.Http2ConnectionDecoder)2 Http2Settings (io.netty.handler.codec.http2.Http2Settings)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 DefaultHttp2Connection (io.netty.handler.codec.http2.DefaultHttp2Connection)1 Http2Connection (io.netty.handler.codec.http2.Http2Connection)1 Http2ConnectionEncoder (io.netty.handler.codec.http2.Http2ConnectionEncoder)1 Http2FrameLogger (io.netty.handler.codec.http2.Http2FrameLogger)1