Skip to content

Commit

Permalink
HTTPCLIENT-2352: update window management improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ok2c committed Jan 3, 2025
1 parent ce44602 commit da99c2b
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public String getId() {

abstract H2StreamHandler createRemotelyInitiatedStream(
H2StreamChannel channel,

HttpProcessor httpProcessor,
BasicHttpConnectionMetrics connMetrics,
HandlerFactory<AsyncPushConsumer> pushHandlerFactory) throws IOException;
Expand All @@ -211,6 +212,15 @@ private int updateWindow(final AtomicInteger window, final int delta) throws Ari
}
}

private int updateWindowMax(final AtomicInteger window) throws ArithmeticException {
for (;;) {
final int current = window.get();
if (window.compareAndSet(current, Integer.MAX_VALUE)) {
return Integer.MAX_VALUE - current;
}
}
}

private int updateInputWindow(
final int streamId, final AtomicInteger window, final int delta) throws ArithmeticException {
final int newSize = updateWindow(window, delta);
Expand Down Expand Up @@ -371,9 +381,9 @@ private void incrementInputCapacity(
final int remainingCapacity = Integer.MAX_VALUE - streamWinSize;
final int chunk = Math.min(inputCapacity, remainingCapacity);
if (chunk != 0) {
updateInputWindow(streamId, inputWindow, chunk);
final RawFrame windowUpdateFrame = frameFactory.createWindowUpdate(streamId, chunk);
commitFrame(windowUpdateFrame);
updateInputWindow(streamId, inputWindow, chunk);
}
}
}
Expand Down Expand Up @@ -412,7 +422,7 @@ public final void onConnect() throws HttpException, IOException {

commitFrame(settingsFrame);
localSettingState = SettingsHandshake.TRANSMITTED;
maximizeConnWindow(connInputWindow.get());
maximizeWindow(0, connInputWindow);

if (streamListener != null) {
final int initInputWindow = connInputWindow.get();
Expand Down Expand Up @@ -1024,7 +1034,7 @@ private void consumeDataFrame(final RawFrame frame, final H2Stream stream) throw
}
final int connWinSize = updateInputWindow(0, connInputWindow, -frameLength);
if (connWinSize < CONNECTION_WINDOW_LOW_MARK) {
maximizeConnWindow(connWinSize);
maximizeWindow(0, connInputWindow);
}
}
if (stream.isRemoteClosed()) {
Expand All @@ -1039,12 +1049,11 @@ private void consumeDataFrame(final RawFrame frame, final H2Stream stream) throw
stream.consumeData(payload);
}

private void maximizeConnWindow(final int connWinSize) throws IOException {
final int delta = Integer.MAX_VALUE - connWinSize;
private void maximizeWindow(final int streamId, final AtomicInteger window) throws IOException {
final int delta = updateWindowMax(window);
if (delta > 0) {
final RawFrame windowUpdateFrame = frameFactory.createWindowUpdate(0, delta);
final RawFrame windowUpdateFrame = frameFactory.createWindowUpdate(streamId, delta);
commitFrame(windowUpdateFrame);
updateInputWindow(0, connInputWindow, delta);
}
}

Expand Down

0 comments on commit da99c2b

Please sign in to comment.