io.netty.handler.codec.http2.Http2LocalFlowController Java Examples

The following examples show how to use io.netty.handler.codec.http2.Http2LocalFlowController. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractNettyHandler.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
public void updateWindow() throws Http2Exception {
  if (!autoTuneFlowControlOn) {
    return;
  }
  pingReturn++;
  long elapsedTime = (System.nanoTime() - lastPingTime);
  if (elapsedTime == 0) {
    elapsedTime = 1;
  }
  long bandwidth = (getDataSincePing() * TimeUnit.SECONDS.toNanos(1)) / elapsedTime;
  Http2LocalFlowController fc = decoder().flowController();
  // Calculate new window size by doubling the observed BDP, but cap at max window
  int targetWindow = Math.min(getDataSincePing() * 2, MAX_WINDOW_SIZE);
  setPinging(false);
  int currentWindow = fc.initialWindowSize(connection().connectionStream());
  if (targetWindow > currentWindow && bandwidth > lastBandwidth) {
    lastBandwidth = bandwidth;
    int increase = targetWindow - currentWindow;
    fc.incrementWindowSize(connection().connectionStream(), increase);
    fc.initialWindowSize(targetWindow);
    Http2Settings settings = new Http2Settings();
    settings.initialWindowSize(targetWindow);
    frameWriter().writeSettings(ctx(), settings, ctx().newPromise());
  }

}
 
Example #2
Source File: NettyHandlerTestBase.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void windowShouldNotExceedMaxWindowSize() throws Exception {
  manualSetUp();
  makeStream();
  AbstractNettyHandler handler = (AbstractNettyHandler) handler();
  handler.setAutoTuneFlowControl(true);
  Http2Stream connectionStream = connection().connectionStream();
  Http2LocalFlowController localFlowController = connection().local().flowController();
  int maxWindow = handler.flowControlPing().maxWindow();

  handler.flowControlPing().setDataSizeSincePing(maxWindow);
  long payload = handler.flowControlPing().payload();
  channelRead(pingFrame(true, payload));

  assertEquals(maxWindow, localFlowController.initialWindowSize(connectionStream));
}
 
Example #3
Source File: Http2MultiplexedChannelPool.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * 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(() -> "Expanding connection window size for " + parentChannel + " by " + initialWindowSize);
        try {
            Http2LocalFlowController localFlowController = http2Connection.local().flowController();
            localFlowController.incrementWindowSize(connectionStream, initialWindowSize);

        } catch (Http2Exception e) {
            log.warn(() -> "Failed to increment windowSize of connection " + parentChannel, e);
        }
    });
}
 
Example #4
Source File: ClientHttp2ObjectEncoder.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFuture doWriteHeaders(int id, int streamId, RequestHeaders headers, boolean endStream) {
    final Http2Connection conn = encoder().connection();
    if (isStreamPresentAndWritable(streamId)) {
        if (keepAliveHandler != null) {
            keepAliveHandler.onReadOrWrite();
        }
        return encoder().writeHeaders(ctx(), streamId, convertHeaders(headers), 0,
                                      endStream, ctx().newPromise());
    }

    final Endpoint<Http2LocalFlowController> local = conn.local();
    if (local.mayHaveCreatedStream(streamId)) {
        final ClosedStreamException closedStreamException =
                new ClosedStreamException("Cannot create a new stream. streamId: " + streamId +
                                          ", lastStreamCreated: " + local.lastStreamCreated());
        return newFailedFuture(UnprocessedRequestException.of(closedStreamException));
    }

    // Client starts a new stream.
    return encoder().writeHeaders(ctx(), streamId, convertHeaders(headers), 0, endStream,
                                  ctx().newPromise());
}
 
Example #5
Source File: AbstractNettyHandler.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
public void updateWindow() throws Http2Exception {
  if (!autoTuneFlowControlOn) {
    return;
  }
  pingReturn++;
  long elapsedTime = (System.nanoTime() - lastPingTime);
  if (elapsedTime == 0) {
    elapsedTime = 1;
  }
  long bandwidth = (getDataSincePing() * TimeUnit.SECONDS.toNanos(1)) / elapsedTime;
  Http2LocalFlowController fc = decoder().flowController();
  // Calculate new window size by doubling the observed BDP, but cap at max window
  int targetWindow = Math.min(getDataSincePing() * 2, MAX_WINDOW_SIZE);
  setPinging(false);
  int currentWindow = fc.initialWindowSize(connection().connectionStream());
  if (targetWindow > currentWindow && bandwidth > lastBandwidth) {
    lastBandwidth = bandwidth;
    int increase = targetWindow - currentWindow;
    fc.incrementWindowSize(connection().connectionStream(), increase);
    fc.initialWindowSize(targetWindow);
    Http2Settings settings = new Http2Settings();
    settings.initialWindowSize(targetWindow);
    frameWriter().writeSettings(ctx(), settings, ctx().newPromise());
  }
}
 
Example #6
Source File: NettyHandlerTestBase.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void windowShouldNotExceedMaxWindowSize() throws Exception {
  manualSetUp();
  makeStream();
  AbstractNettyHandler handler = (AbstractNettyHandler) handler();
  handler.setAutoTuneFlowControl(true);
  Http2Stream connectionStream = connection().connectionStream();
  Http2LocalFlowController localFlowController = connection().local().flowController();
  int maxWindow = handler.flowControlPing().maxWindow();

  handler.flowControlPing().setDataSizeAndSincePing(maxWindow);
  long payload = handler.flowControlPing().payload();
  channelRead(pingFrame(true, payload));

  assertEquals(maxWindow, localFlowController.initialWindowSize(connectionStream));
}
 
Example #7
Source File: NettyServerHandlerTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void connectionWindowShouldBeOverridden() throws Exception {
  flowControlWindow = 1048576; // 1MiB
  manualSetUp();

  Http2Stream connectionStream = connection().connectionStream();
  Http2LocalFlowController localFlowController = connection().local().flowController();
  int actualInitialWindowSize = localFlowController.initialWindowSize(connectionStream);
  int actualWindowSize = localFlowController.windowSize(connectionStream);
  assertEquals(flowControlWindow, actualWindowSize);
  assertEquals(flowControlWindow, actualInitialWindowSize);
}
 
Example #8
Source File: NettyClientHandlerTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void connectionWindowShouldBeOverridden() throws Exception {
  flowControlWindow = 1048576; // 1MiB
  setUp();

  Http2Stream connectionStream = connection().connectionStream();
  Http2LocalFlowController localFlowController = connection().local().flowController();
  int actualInitialWindowSize = localFlowController.initialWindowSize(connectionStream);
  int actualWindowSize = localFlowController.windowSize(connectionStream);
  assertEquals(flowControlWindow, actualWindowSize);
  assertEquals(flowControlWindow, actualInitialWindowSize);
  assertEquals(1048576, actualWindowSize);
}
 
Example #9
Source File: NettyHandlerTestBase.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void windowUpdateMatchesTarget() throws Exception {
  manualSetUp();
  Http2Stream connectionStream = connection().connectionStream();
  Http2LocalFlowController localFlowController = connection().local().flowController();
  makeStream();
  AbstractNettyHandler handler = (AbstractNettyHandler) handler();
  handler.setAutoTuneFlowControl(true);

  ByteBuf data = ctx().alloc().buffer(1024);
  while (data.isWritable()) {
    data.writeLong(1111);
  }
  int length = data.readableBytes();
  ByteBuf frame = dataFrame(3, false, data.copy());
  channelRead(frame);
  int accumulator = length;
  // 40 is arbitrary, any number large enough to trigger a window update would work
  for (int i = 0; i < 40; i++) {
    channelRead(dataFrame(3, false, data.copy()));
    accumulator += length;
  }
  long pingData = handler.flowControlPing().payload();
  channelRead(pingFrame(true, pingData));

  assertEquals(accumulator, handler.flowControlPing().getDataSincePing());
  assertEquals(2 * accumulator, localFlowController.initialWindowSize(connectionStream));
}
 
Example #10
Source File: NettyServerHandlerTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void connectionWindowShouldBeOverridden() throws Exception {
  flowControlWindow = 1048576; // 1MiB
  manualSetUp();

  Http2Stream connectionStream = connection().connectionStream();
  Http2LocalFlowController localFlowController = connection().local().flowController();
  int actualInitialWindowSize = localFlowController.initialWindowSize(connectionStream);
  int actualWindowSize = localFlowController.windowSize(connectionStream);
  assertEquals(flowControlWindow, actualWindowSize);
  assertEquals(flowControlWindow, actualInitialWindowSize);
}
 
Example #11
Source File: NettyClientHandlerTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void connectionWindowShouldBeOverridden() throws Exception {
  flowControlWindow = 1048576; // 1MiB
  setUp();

  Http2Stream connectionStream = connection().connectionStream();
  Http2LocalFlowController localFlowController = connection().local().flowController();
  int actualInitialWindowSize = localFlowController.initialWindowSize(connectionStream);
  int actualWindowSize = localFlowController.windowSize(connectionStream);
  assertEquals(flowControlWindow, actualWindowSize);
  assertEquals(flowControlWindow, actualInitialWindowSize);
  assertEquals(1048576, actualWindowSize);
}
 
Example #12
Source File: NettyHandlerTestBase.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void windowUpdateMatchesTarget() throws Exception {
  manualSetUp();
  Http2Stream connectionStream = connection().connectionStream();
  Http2LocalFlowController localFlowController = connection().local().flowController();
  makeStream();
  AbstractNettyHandler handler = (AbstractNettyHandler) handler();
  handler.setAutoTuneFlowControl(true);

  ByteBuf data = ctx().alloc().buffer(1024);
  while (data.isWritable()) {
    data.writeLong(1111);
  }
  int length = data.readableBytes();
  ByteBuf frame = dataFrame(3, false, data.copy());
  channelRead(frame);
  int accumulator = length;
  // 40 is arbitrary, any number large enough to trigger a window update would work
  for (int i = 0; i < 40; i++) {
    channelRead(dataFrame(3, false, data.copy()));
    accumulator += length;
  }
  long pingData = handler.flowControlPing().payload();
  channelRead(pingFrame(true, pingData));

  assertEquals(accumulator, handler.flowControlPing().getDataSincePing());
  assertEquals(2 * accumulator, localFlowController.initialWindowSize(connectionStream));
}