Search in sources :

Example 6 with Http2DataFrame

use of io.netty.handler.codec.http2.Http2DataFrame in project ambry by linkedin.

the class Http2ClientStreamStatsHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Http2Frame frame) throws Exception {
    ReferenceCountUtil.retain(frame);
    RequestInfo requestInfo = ctx.channel().attr(Http2NetworkClient.REQUEST_INFO).get();
    requestInfo.responseFramesCount++;
    long time = System.currentTimeMillis() - requestInfo.getStreamSendTime();
    if (frame instanceof Http2HeadersFrame) {
        http2ClientMetrics.http2StreamRoundTripTime.update(time);
        requestInfo.setStreamHeaderFrameReceiveTime(System.currentTimeMillis());
        logger.debug("Header Frame received. Time from send: {}ms. Request: {}", time, requestInfo);
    } else if (frame instanceof Http2DataFrame) {
        logger.debug("Data Frame size: {}. Time from send: {}ms. Request: {}", ((Http2DataFrame) frame).content().readableBytes(), time, requestInfo);
    }
    if (frame instanceof Http2DataFrame && ((Http2DataFrame) frame).isEndStream()) {
        http2ClientMetrics.http2StreamFirstToLastFrameTime.update(time);
        http2ClientMetrics.http2ResponseFrameCount.update(requestInfo.responseFramesCount);
        logger.debug("All Frame received. Time from send: {}ms. Request: {}", time, requestInfo);
    }
    ctx.fireChannelRead(frame);
}
Also used : Http2DataFrame(io.netty.handler.codec.http2.Http2DataFrame) Http2HeadersFrame(io.netty.handler.codec.http2.Http2HeadersFrame) RequestInfo(com.github.ambry.network.RequestInfo)

Example 7 with Http2DataFrame

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

the class HelloWorldHttp2Handler method onDataRead.

/**
 * If receive a frame with end-of-stream set, send a pre-canned response.
 */
private static void onDataRead(ChannelHandlerContext ctx, Http2DataFrame data) throws Exception {
    Http2FrameStream stream = data.stream();
    if (data.isEndStream()) {
        sendResponse(ctx, stream, data.content());
    } else {
        // We do not send back the response to the remote-peer, so we need to release it.
        data.release();
    }
    // Update the flowcontroller
    ctx.write(new DefaultHttp2WindowUpdateFrame(data.initialFlowControlledBytes()).stream(stream));
}
Also used : DefaultHttp2WindowUpdateFrame(io.netty.handler.codec.http2.DefaultHttp2WindowUpdateFrame) Http2FrameStream(io.netty.handler.codec.http2.Http2FrameStream)

Example 8 with Http2DataFrame

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

the class Http2MultiplexTest method childQueueIsDrainedAndNewDataIsDispatchedInParentReadLoopNoAutoRead.

@Test
public void childQueueIsDrainedAndNewDataIsDispatchedInParentReadLoopNoAutoRead() {
    final AtomicInteger numReads = new AtomicInteger(1);
    final AtomicInteger channelReadCompleteCount = new AtomicInteger(0);
    final AtomicBoolean shouldDisableAutoRead = new AtomicBoolean();
    Consumer<ChannelHandlerContext> ctxConsumer = new Consumer<ChannelHandlerContext>() {

        @Override
        public void accept(ChannelHandlerContext obj) {
            channelReadCompleteCount.incrementAndGet();
            if (shouldDisableAutoRead.get()) {
                obj.channel().config().setAutoRead(false);
            }
        }
    };
    final LastInboundHandler inboundHandler = new LastInboundHandler(ctxConsumer);
    Http2StreamChannel childChannel = newInboundStream(3, false, numReads, inboundHandler);
    childChannel.config().setAutoRead(false);
    Http2DataFrame dataFrame1 = new DefaultHttp2DataFrame(bb("1")).stream(childChannel.stream());
    Http2DataFrame dataFrame2 = new DefaultHttp2DataFrame(bb("2")).stream(childChannel.stream());
    Http2DataFrame dataFrame3 = new DefaultHttp2DataFrame(bb("3")).stream(childChannel.stream());
    Http2DataFrame dataFrame4 = new DefaultHttp2DataFrame(bb("4")).stream(childChannel.stream());
    assertEquals(new DefaultHttp2HeadersFrame(request).stream(childChannel.stream()), inboundHandler.readInbound());
    ChannelHandler readCompleteSupressHandler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        // We want to simulate the parent channel calling channelRead and delay calling channelReadComplete.
        }
    };
    parentChannel.pipeline().addFirst(readCompleteSupressHandler);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("1"), 0, false);
    assertEqualsAndRelease(dataFrame1, inboundHandler.<Http2Frame>readInbound());
    // We want one item to be in the queue, and allow the numReads to be larger than 1. This will ensure that
    // when beginRead() is called the child channel is added to the readPending queue of the parent channel.
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("2"), 0, false);
    numReads.set(2);
    childChannel.read();
    assertEqualsAndRelease(dataFrame2, inboundHandler.<Http2Frame>readInbound());
    assertNull(inboundHandler.readInbound());
    // This is the second item that was read, this should be the last until we call read() again. This should also
    // notify of readComplete().
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("3"), 0, false);
    assertEqualsAndRelease(dataFrame3, inboundHandler.<Http2Frame>readInbound());
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("4"), 0, false);
    assertNull(inboundHandler.readInbound());
    childChannel.read();
    assertEqualsAndRelease(dataFrame4, inboundHandler.<Http2Frame>readInbound());
    assertNull(inboundHandler.readInbound());
    // Now we want to call channelReadComplete and simulate the end of the read loop.
    parentChannel.pipeline().remove(readCompleteSupressHandler);
    parentChannel.flushInbound();
    // 3 = 1 for initialization + 1 for first read of 2 items + 1 for second read of 2 items +
    // 1 for parent channel readComplete
    assertEquals(4, channelReadCompleteCount.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(io.netty.handler.codec.http2.LastInboundHandler.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 9 with Http2DataFrame

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

the class Http2MultiplexTest method childQueueIsDrainedAndNewDataIsDispatchedInParentReadLoopAutoRead.

@Test
public void childQueueIsDrainedAndNewDataIsDispatchedInParentReadLoopAutoRead() {
    AtomicInteger numReads = new AtomicInteger(1);
    final AtomicInteger channelReadCompleteCount = new AtomicInteger(0);
    final AtomicBoolean shouldDisableAutoRead = new AtomicBoolean();
    Consumer<ChannelHandlerContext> ctxConsumer = new Consumer<ChannelHandlerContext>() {

        @Override
        public void accept(ChannelHandlerContext obj) {
            channelReadCompleteCount.incrementAndGet();
            if (shouldDisableAutoRead.get()) {
                obj.channel().config().setAutoRead(false);
            }
        }
    };
    LastInboundHandler inboundHandler = new LastInboundHandler(ctxConsumer);
    Http2StreamChannel childChannel = newInboundStream(3, false, numReads, inboundHandler);
    childChannel.config().setAutoRead(false);
    Http2DataFrame dataFrame1 = new DefaultHttp2DataFrame(bb("1")).stream(childChannel.stream());
    Http2DataFrame dataFrame2 = new DefaultHttp2DataFrame(bb("2")).stream(childChannel.stream());
    Http2DataFrame dataFrame3 = new DefaultHttp2DataFrame(bb("3")).stream(childChannel.stream());
    Http2DataFrame dataFrame4 = new DefaultHttp2DataFrame(bb("4")).stream(childChannel.stream());
    assertEquals(new DefaultHttp2HeadersFrame(request).stream(childChannel.stream()), inboundHandler.readInbound());
    ChannelHandler readCompleteSupressHandler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) {
        // We want to simulate the parent channel calling channelRead and delay calling channelReadComplete.
        }
    };
    parentChannel.pipeline().addFirst(readCompleteSupressHandler);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("1"), 0, false);
    assertEqualsAndRelease(dataFrame1, inboundHandler.<Http2DataFrame>readInbound());
    // We want one item to be in the queue, and allow the numReads to be larger than 1. This will ensure that
    // when beginRead() is called the child channel is added to the readPending queue of the parent channel.
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("2"), 0, false);
    numReads.set(10);
    shouldDisableAutoRead.set(true);
    childChannel.config().setAutoRead(true);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("3"), 0, false);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("4"), 0, false);
    // Detecting EOS should flush all pending data regardless of read calls.
    assertEqualsAndRelease(dataFrame2, inboundHandler.<Http2DataFrame>readInbound());
    assertEqualsAndRelease(dataFrame3, inboundHandler.<Http2DataFrame>readInbound());
    assertEqualsAndRelease(dataFrame4, inboundHandler.<Http2DataFrame>readInbound());
    assertNull(inboundHandler.readInbound());
    // Now we want to call channelReadComplete and simulate the end of the read loop.
    parentChannel.pipeline().remove(readCompleteSupressHandler);
    parentChannel.flushInbound();
    // 3 = 1 for initialization + 1 for read when auto read was off + 1 for when auto read was back on
    assertEquals(3, channelReadCompleteCount.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(io.netty.handler.codec.http2.LastInboundHandler.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Aggregations

ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)5 Test (org.junit.jupiter.api.Test)5 ChannelHandler (io.netty.channel.ChannelHandler)3 Http2DataFrame (io.netty.handler.codec.http2.Http2DataFrame)3 Http2HeadersFrame (io.netty.handler.codec.http2.Http2HeadersFrame)3 Consumer (io.netty.handler.codec.http2.LastInboundHandler.Consumer)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 StreamException (io.netty.handler.codec.http2.Http2Exception.StreamException)2 RequestInfo (com.github.ambry.network.RequestInfo)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 UnsupportedMessageTypeException (io.netty.handler.codec.UnsupportedMessageTypeException)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 HttpServerUpgradeHandler (io.netty.handler.codec.http.HttpServerUpgradeHandler)1 UpgradeEvent (io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeEvent)1 DefaultHttp2DataFrame (io.netty.handler.codec.http2.DefaultHttp2DataFrame)1 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)1 DefaultHttp2HeadersFrame (io.netty.handler.codec.http2.DefaultHttp2HeadersFrame)1