类 io.netty.handler.codec.http2.Http2LocalFlowController 源码实例Demo

下面列出了怎么用 io.netty.handler.codec.http2.Http2LocalFlowController 的API类实例代码及写法,或者点击链接到github查看源代码。


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

}
 

@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));
}
 

/**
 * 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);
        }
    });
}
 

@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());
}
 
源代码5 项目: grpc-java   文件: AbstractNettyHandler.java

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());
  }
}
 
源代码6 项目: grpc-java   文件: NettyHandlerTestBase.java

@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));
}
 

@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);
}
 

@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);
}
 

@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));
}
 
源代码10 项目: grpc-java   文件: NettyServerHandlerTest.java

@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);
}
 
源代码11 项目: grpc-java   文件: NettyClientHandlerTest.java

@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);
}
 
源代码12 项目: grpc-java   文件: NettyHandlerTestBase.java

@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));
}
 
 同包方法