Skip to content

Commit

Permalink
Fix BZ 64467. Improve performance of closing idle streams
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed May 22, 2020
1 parent 3756bc6 commit 9a02316
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
10 changes: 5 additions & 5 deletions java/org/apache/coyote/http2/Http2UpgradeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1473,11 +1473,11 @@ public HeaderEmitter headersStart(int streamId, boolean headersEndStream)
}


private void closeIdleStreams(int newMaxActiveRemoteStreamId) throws Http2Exception {
for (int i = maxActiveRemoteStreamId + 2; i < newMaxActiveRemoteStreamId; i += 2) {
Stream stream = getStream(i, false);
if (stream != null) {
stream.closeIfIdle();
private void closeIdleStreams(int newMaxActiveRemoteStreamId) {
for (Entry<Integer,Stream> entry : streams.entrySet()) {
if (entry.getKey().intValue() > maxActiveRemoteStreamId &&
entry.getKey().intValue() < newMaxActiveRemoteStreamId) {
entry.getValue().closeIfIdle();
}
}
maxActiveRemoteStreamId = newMaxActiveRemoteStreamId;
Expand Down
31 changes: 27 additions & 4 deletions test/org/apache/coyote/http2/TestHttp2Section_5_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,44 @@ public void testClientSendOldStream() throws Exception {

@Test
public void testImplicitClose() throws Exception {
doTestImplicitClose(5);
}


// https://bz.apache.org/bugzilla/show_bug.cgi?id=64467
@Test
public void testImplicitCloseLargeId() throws Exception {
doTestImplicitClose(Integer.MAX_VALUE - 8);
}


private void doTestImplicitClose(int lastStreamId) throws Exception {

long startFirst = System.nanoTime();
http2Connect();
long durationFirst = System.nanoTime() - startFirst;

sendPriority(3, 0, 16);
sendPriority(5, 0, 16);
sendPriority(lastStreamId, 0, 16);

sendSimpleGetRequest(5);
long startSecond = System.nanoTime();
sendSimpleGetRequest(lastStreamId);
readSimpleGetResponse();
Assert.assertEquals(getSimpleResponseTrace(5), output.getTrace());
long durationSecond = System.nanoTime() - startSecond;

Assert.assertEquals(getSimpleResponseTrace(lastStreamId), output.getTrace());
output.clearTrace();

// Allow second request to take up to 5 times first request or up to 1 second - whichever is the larger - mainly
// to allow for CI systems under load that can exhibit significant timing variation.
Assert.assertTrue("First request took [" + durationFirst/1000000 + "ms], second request took [" +
durationSecond/1000000 + "ms]", durationSecond < 1000000000 || durationSecond < durationFirst * 3);

// Should trigger an error since stream 3 should have been implicitly
// closed.
sendSimpleGetRequest(3);

handleGoAwayResponse(5);
handleGoAwayResponse(lastStreamId);
}


Expand Down
4 changes: 4 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
<update>
Add support for ALPN on recent OpenJDK 8 releases. (remm)
</update>
<fix>
<bug>64467</bug>: Improve performance of closing idle HTTP/2 streams.
(markt)
</fix>
</changelog>
</subsection>
<subsection name="WebSocket">
Expand Down

0 comments on commit 9a02316

Please sign in to comment.