Skip to content

Commit 2cb5fb7

Browse files
committed
HTTPCORE-774: update window management improvements
1 parent bb63f07 commit 2cb5fb7

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ public String getId() {
188188

189189
abstract H2StreamHandler createRemotelyInitiatedStream(
190190
H2StreamChannel channel,
191+
191192
HttpProcessor httpProcessor,
192193
BasicHttpConnectionMetrics connMetrics,
193194
HandlerFactory<AsyncPushConsumer> pushHandlerFactory) throws IOException;
@@ -211,6 +212,15 @@ private int updateWindow(final AtomicInteger window, final int delta) throws Ari
211212
}
212213
}
213214

215+
private int updateWindowMax(final AtomicInteger window) throws ArithmeticException {
216+
for (;;) {
217+
final int current = window.get();
218+
if (window.compareAndSet(current, Integer.MAX_VALUE)) {
219+
return Integer.MAX_VALUE - current;
220+
}
221+
}
222+
}
223+
214224
private int updateInputWindow(
215225
final int streamId, final AtomicInteger window, final int delta) throws ArithmeticException {
216226
final int newSize = updateWindow(window, delta);
@@ -371,9 +381,9 @@ private void incrementInputCapacity(
371381
final int remainingCapacity = Integer.MAX_VALUE - streamWinSize;
372382
final int chunk = Math.min(inputCapacity, remainingCapacity);
373383
if (chunk != 0) {
384+
updateInputWindow(streamId, inputWindow, chunk);
374385
final RawFrame windowUpdateFrame = frameFactory.createWindowUpdate(streamId, chunk);
375386
commitFrame(windowUpdateFrame);
376-
updateInputWindow(streamId, inputWindow, chunk);
377387
}
378388
}
379389
}
@@ -412,7 +422,7 @@ public final void onConnect() throws HttpException, IOException {
412422

413423
commitFrame(settingsFrame);
414424
localSettingState = SettingsHandshake.TRANSMITTED;
415-
maximizeConnWindow(connInputWindow.get());
425+
maximizeWindow(0, connInputWindow);
416426

417427
if (streamListener != null) {
418428
final int initInputWindow = connInputWindow.get();
@@ -1024,7 +1034,7 @@ private void consumeDataFrame(final RawFrame frame, final H2Stream stream) throw
10241034
}
10251035
final int connWinSize = updateInputWindow(0, connInputWindow, -frameLength);
10261036
if (connWinSize < CONNECTION_WINDOW_LOW_MARK) {
1027-
maximizeConnWindow(connWinSize);
1037+
maximizeWindow(0, connInputWindow);
10281038
}
10291039
}
10301040
if (stream.isRemoteClosed()) {
@@ -1039,12 +1049,11 @@ private void consumeDataFrame(final RawFrame frame, final H2Stream stream) throw
10391049
stream.consumeData(payload);
10401050
}
10411051

1042-
private void maximizeConnWindow(final int connWinSize) throws IOException {
1043-
final int delta = Integer.MAX_VALUE - connWinSize;
1052+
private void maximizeWindow(final int streamId, final AtomicInteger window) throws IOException {
1053+
final int delta = updateWindowMax(window);
10441054
if (delta > 0) {
1045-
final RawFrame windowUpdateFrame = frameFactory.createWindowUpdate(0, delta);
1055+
final RawFrame windowUpdateFrame = frameFactory.createWindowUpdate(streamId, delta);
10461056
commitFrame(windowUpdateFrame);
1047-
updateInputWindow(0, connInputWindow, delta);
10481057
}
10491058
}
10501059

0 commit comments

Comments
 (0)