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());
}
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));
}
}
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);
}
});
}
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();
}
}
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());
}
}
});
}
Aggregations