Search in sources :

Example 56 with Http2Stream

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

the class Http2FrameCodecTest method streamErrorShouldFireExceptionForInbound.

@Test
public void streamErrorShouldFireExceptionForInbound() throws Exception {
    frameInboundWriter.writeInboundHeaders(3, request, 31, false);
    Http2Stream stream = frameCodec.connection().stream(3);
    assertNotNull(stream);
    StreamException streamEx = new StreamException(3, Http2Error.INTERNAL_ERROR, "foo");
    channel.pipeline().fireExceptionCaught(streamEx);
    Http2FrameStreamEvent event = inboundHandler.readInboundMessageOrUserEvent();
    assertEquals(Http2FrameStreamEvent.Type.State, event.type());
    assertEquals(State.OPEN, event.stream().state());
    Http2HeadersFrame headersFrame = inboundHandler.readInboundMessageOrUserEvent();
    assertNotNull(headersFrame);
    Http2FrameStreamException e = assertThrows(Http2FrameStreamException.class, new Executable() {

        @Override
        public void execute() throws Throwable {
            inboundHandler.checkException();
        }
    });
    assertEquals(streamEx, e.getCause());
    assertNull(inboundHandler.readInboundMessageOrUserEvent());
}
Also used : Executable(org.junit.jupiter.api.function.Executable) StreamException(io.netty.handler.codec.http2.Http2Exception.StreamException) Test(org.junit.jupiter.api.Test)

Example 57 with Http2Stream

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

the class NoPriorityByteDistributionBenchmark method setupTrial.

@Setup(Level.Trial)
public void setupTrial() throws Exception {
    connection = new DefaultHttp2Connection(false);
    dataRefresherKey = connection.newKey();
    // Create the flow controller
    switch(algorithm) {
        case WFQ:
            distributor = new WeightedFairQueueByteDistributor(connection, 0);
            break;
        case UNIFORM:
            distributor = new UniformStreamByteDistributor(connection);
            break;
    }
    controller = new DefaultHttp2RemoteFlowController(connection, new ByteCounter(distributor));
    connection.remote().flowController(controller);
    Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder().encoderEnforceMaxConcurrentStreams(false).validateHeaders(false).frameListener(new Http2FrameAdapter()).connection(connection).build();
    ctx = new EmbeddedChannelWriteReleaseHandlerContext(PooledByteBufAllocator.DEFAULT, handler) {

        @Override
        protected void handleException(Throwable t) {
            handleUnexpectedException(t);
        }
    };
    handler.handlerAdded(ctx);
    handler.channelActive(ctx);
    // Create the streams, each initialized with MAX_INT bytes.
    for (int i = 0; i < numStreams; ++i) {
        Http2Stream stream = connection.local().createStream(toStreamId(i), false);
        addData(stream, Integer.MAX_VALUE);
        stream.setProperty(dataRefresherKey, new DataRefresher(stream));
    }
}
Also used : DefaultHttp2Connection(io.netty.handler.codec.http2.DefaultHttp2Connection) Http2FrameAdapter(io.netty.handler.codec.http2.Http2FrameAdapter) Http2ConnectionHandler(io.netty.handler.codec.http2.Http2ConnectionHandler) WeightedFairQueueByteDistributor(io.netty.handler.codec.http2.WeightedFairQueueByteDistributor) Http2ConnectionHandlerBuilder(io.netty.handler.codec.http2.Http2ConnectionHandlerBuilder) EmbeddedChannelWriteReleaseHandlerContext(io.netty.microbench.channel.EmbeddedChannelWriteReleaseHandlerContext) DefaultHttp2RemoteFlowController(io.netty.handler.codec.http2.DefaultHttp2RemoteFlowController) Http2Stream(io.netty.handler.codec.http2.Http2Stream) UniformStreamByteDistributor(io.netty.handler.codec.http2.UniformStreamByteDistributor) Setup(org.openjdk.jmh.annotations.Setup)

Example 58 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project aws-sdk-java-v2 by aws.

the class Http2MultiplexedChannelPool method tryExpandConnectionWindow.

/**
 * By default, connection window size is a constant value:
 * connectionWindowSize = 65535 + (configureInitialWindowSize - 65535) * 2.
 * See https://github.com/netty/netty/blob/5c458c9a98d4d3d0345e58495e017175156d624f/codec-http2/src/main/java/io/netty
 * /handler/codec/http2/Http2FrameCodec.java#L255
 * We should expand connection window so that the window size proportional to the number of concurrent streams within the
 * connection.
 * Note that when {@code WINDOW_UPDATE} will be sent depends on the processedWindow in DefaultHttp2LocalFlowController.
 */
private void tryExpandConnectionWindow(Channel parentChannel) {
    doInEventLoop(parentChannel.eventLoop(), () -> {
        Http2Connection http2Connection = parentChannel.attr(HTTP2_CONNECTION).get();
        Integer initialWindowSize = parentChannel.attr(HTTP2_INITIAL_WINDOW_SIZE).get();
        Validate.notNull(http2Connection, "http2Connection should not be null on channel " + parentChannel);
        Validate.notNull(http2Connection, "initialWindowSize should not be null on channel " + parentChannel);
        Http2Stream connectionStream = http2Connection.connectionStream();
        log.debug(parentChannel, () -> "Expanding connection window size for " + parentChannel + " by " + initialWindowSize);
        try {
            Http2LocalFlowController localFlowController = http2Connection.local().flowController();
            localFlowController.incrementWindowSize(connectionStream, initialWindowSize);
        } catch (Http2Exception e) {
            log.warn(parentChannel, () -> "Failed to increment windowSize of connection " + parentChannel, e);
        }
    });
}
Also used : Http2LocalFlowController(io.netty.handler.codec.http2.Http2LocalFlowController) Http2Exception(io.netty.handler.codec.http2.Http2Exception) Http2Connection(io.netty.handler.codec.http2.Http2Connection) Http2Stream(io.netty.handler.codec.http2.Http2Stream)

Example 59 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project aws-sdk-java-v2 by aws.

the class Http2MultiplexedChannelPoolTest method acquire_shouldExpandConnectionWindowSizeProportionally.

@Test
public void acquire_shouldExpandConnectionWindowSizeProportionally() {
    int maxConcurrentStream = 3;
    EmbeddedChannel channel = newHttp2Channel();
    channel.attr(ChannelAttributeKey.MAX_CONCURRENT_STREAMS).set((long) maxConcurrentStream);
    try {
        ChannelPool connectionPool = Mockito.mock(ChannelPool.class);
        loopGroup.register(channel).awaitUninterruptibly();
        Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());
        channelPromise.setSuccess(channel);
        Mockito.when(connectionPool.acquire()).thenReturn(channelPromise);
        Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup, Collections.emptySet(), null);
        Future<Channel> acquire = h2Pool.acquire();
        acquire.awaitUninterruptibly();
        channel.runPendingTasks();
        Http2Connection http2Connection = channel.attr(HTTP2_CONNECTION).get();
        Http2LocalFlowController flowController = http2Connection.local().flowController();
        System.out.println(flowController.initialWindowSize());
        Http2Stream connectionStream = http2Connection.stream(0);
        // 1_048_576 (initial configured window size), 65535 (configured initial window size)
        // (1048576 - 65535) *2 + 65535 = 2031617
        assertThat(flowController.windowSize(connectionStream)).isEqualTo(2031617);
        // 2031617 + 1048576 (configured initial window size) = 3080193
        assertThat(flowController.initialWindowSize(connectionStream)).isEqualTo(3080193);
        // acquire again
        h2Pool.acquire().awaitUninterruptibly();
        channel.runPendingTasks();
        // 3080193 + 1048576 (configured initial window size) = 4128769
        assertThat(flowController.initialWindowSize(connectionStream)).isEqualTo(4128769);
        Mockito.verify(connectionPool, Mockito.times(1)).acquire();
    } finally {
        channel.close();
    }
}
Also used : ChannelPool(io.netty.channel.pool.ChannelPool) Http2LocalFlowController(io.netty.handler.codec.http2.Http2LocalFlowController) Http2Connection(io.netty.handler.codec.http2.Http2Connection) DefaultPromise(io.netty.util.concurrent.DefaultPromise) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) Http2TestUtils.newHttp2Channel(software.amazon.awssdk.http.nio.netty.internal.http2.utils.Http2TestUtils.newHttp2Channel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Http2Stream(io.netty.handler.codec.http2.Http2Stream) Test(org.junit.Test)

Example 60 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project vert.x by eclipse-vertx.

the class Http2ServerConnection method doSendPush.

private synchronized void doSendPush(int streamId, String host, HttpMethod method, MultiMap headers, String path, StreamPriority streamPriority, Promise<HttpServerResponse> promise) {
    Http2Headers headers_ = new DefaultHttp2Headers();
    headers_.method(method.name());
    headers_.path(path);
    headers_.scheme(isSsl() ? "https" : "http");
    if (host != null) {
        headers_.authority(host);
    }
    if (headers != null) {
        headers.forEach(header -> headers_.add(header.getKey(), header.getValue()));
    }
    handler.writePushPromise(streamId, headers_, new Handler<AsyncResult<Integer>>() {

        @Override
        public void handle(AsyncResult<Integer> ar) {
            if (ar.succeeded()) {
                synchronized (Http2ServerConnection.this) {
                    int promisedStreamId = ar.result();
                    String contentEncoding = HttpUtils.determineContentEncoding(headers_);
                    Http2Stream promisedStream = handler.connection().stream(promisedStreamId);
                    Push push = new Push(context, contentEncoding, method, path, promise);
                    push.priority(streamPriority);
                    push.init(promisedStream);
                    int maxConcurrentStreams = handler.maxConcurrentStreams();
                    if (concurrentStreams < maxConcurrentStreams) {
                        concurrentStreams++;
                        push.complete();
                    } else {
                        pendingPushes.add(push);
                    }
                }
            } else {
                promise.fail(ar.cause());
            }
        }
    });
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) Http2Stream(io.netty.handler.codec.http2.Http2Stream) AsyncResult(io.vertx.core.AsyncResult)

Aggregations

Http2Stream (io.netty.handler.codec.http2.Http2Stream)44 Http2Exception (io.netty.handler.codec.http2.Http2Exception)15 ByteBuf (io.netty.buffer.ByteBuf)12 Test (org.junit.Test)12 Test (org.junit.jupiter.api.Test)9 Http2Connection (io.netty.handler.codec.http2.Http2Connection)8 ChannelFuture (io.netty.channel.ChannelFuture)7 Http2StreamVisitor (io.netty.handler.codec.http2.Http2StreamVisitor)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 Metadata (io.grpc.Metadata)6 Http2LocalFlowController (io.netty.handler.codec.http2.Http2LocalFlowController)6 Status (io.grpc.Status)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 StreamException (io.netty.handler.codec.http2.Http2Exception.StreamException)5 Channel (io.netty.channel.Channel)4 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)4 Http2Headers (io.netty.handler.codec.http2.Http2Headers)4 AsyncResult (io.vertx.core.AsyncResult)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Bootstrap (io.netty.bootstrap.Bootstrap)3