Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confirm concurrent HTTP/2 requests with empty flow-control window. #4127

Merged
merged 1 commit into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,7 @@ private static OkHttpClient buildHttp2Client() {
.build());
Response response1 = call1.execute();

// Wait until the server has completely filled the stream and connection flow-control windows.
int expectedFrameCount = Http2Connection.OKHTTP_CLIENT_WINDOW_SIZE / 16384;
int dataFrameCount = 0;
while (dataFrameCount < expectedFrameCount) {
String log = http2Handler.take();
if (log.equals("FINE: << 0x00000003 16384 DATA ")) {
dataFrameCount++;
}
}
waitForDataFrames(Http2Connection.OKHTTP_CLIENT_WINDOW_SIZE);

// Cancel the call and discard what we've buffered for the response body. This should free up
// the connection flow-control window so new requests can proceed.
Expand All @@ -336,6 +328,18 @@ private static OkHttpClient buildHttp2Client() {
assertEquals("abc", response2.body().string());
}

/** Wait for the client to receive {@code dataLength} DATA frames. */
private void waitForDataFrames(int dataLength) throws Exception {
int expectedFrameCount = dataLength / 16384;
int dataFrameCount = 0;
while (dataFrameCount < expectedFrameCount) {
String log = http2Handler.take();
if (log.equals("FINE: << 0x00000003 16384 DATA ")) {
dataFrameCount++;
}
}
}

@Test public void connectionWindowUpdateOnClose() throws Exception {
server.enqueue(new MockResponse()
.setBody(new Buffer().write(new byte[Http2Connection.OKHTTP_CLIENT_WINDOW_SIZE + 1])));
Expand All @@ -347,15 +351,7 @@ private static OkHttpClient buildHttp2Client() {
.build());
Response response1 = call1.execute();

// Wait until the server has completely filled the stream and connection flow-control windows.
int expectedFrameCount = Http2Connection.OKHTTP_CLIENT_WINDOW_SIZE / 16384;
int dataFrameCount = 0;
while (dataFrameCount < expectedFrameCount) {
String log = http2Handler.take();
if (log.equals("FINE: << 0x00000003 16384 DATA ")) {
dataFrameCount++;
}
}
waitForDataFrames(Http2Connection.OKHTTP_CLIENT_WINDOW_SIZE);

// Cancel the call and close the response body. This should discard the buffered data and update
// the connnection flow-control window.
Expand All @@ -369,6 +365,38 @@ private static OkHttpClient buildHttp2Client() {
assertEquals("abc", response2.body().string());
}

@Test public void concurrentRequestWithEmptyFlowControlWindow() throws Exception {
server.enqueue(new MockResponse()
.setBody(new Buffer().write(new byte[Http2Connection.OKHTTP_CLIENT_WINDOW_SIZE])));
server.enqueue(new MockResponse()
.setBody("abc"));

Call call1 = client.newCall(new Request.Builder()
.url(server.url("/"))
.build());
Response response1 = call1.execute();

waitForDataFrames(Http2Connection.OKHTTP_CLIENT_WINDOW_SIZE);

assertEquals(Http2Connection.OKHTTP_CLIENT_WINDOW_SIZE, response1.body().contentLength());
int read = response1.body().source().read(new byte[8192]);
assertEquals(8192, read);

// Make a second call that should transmit the response headers. The response body won't be
// transmitted until the flow-control window is updated from the first request.
Call call2 = client.newCall(new Request.Builder()
.url(server.url("/"))
.build());
Response response2 = call2.execute();
assertEquals(200, response2.code());

// Close the response body. This should discard the buffered data and update the connection
// flow-control window.
response1.close();

assertEquals("abc", response2.body().string());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the test stronger e.g. assert nothing can be read until after response1.close? Perhaps using inputStream.available()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if there is a good way of checking that. It looks like available() will always return 0 unless you trigger a read. From my perspective, this test is proving that the second request will proceed as usual given the actions from the first request.

}

/** https://github.com/square/okhttp/issues/373 */
@Test @Ignore public void synchronousRequest() throws Exception {
server.enqueue(new MockResponse().setBody("A"));
Expand Down
13 changes: 12 additions & 1 deletion okhttp/src/main/java/okhttp3/internal/http2/Http2Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,27 @@ public void sendResponseHeaders(List<Header> responseHeaders, boolean out) throw
throw new NullPointerException("responseHeaders == null");
}
boolean outFinished = false;
boolean flushHeaders = false;
synchronized (this) {
this.hasResponseHeaders = true;
if (!out) {
this.sink.finished = true;
flushHeaders = true;
outFinished = true;
}
}

// Only DATA frames are subject to flow-control. Transmit the HEADER frame if the connection
// flow-control window is fully depleted.
if (!flushHeaders) {
synchronized (connection) {
flushHeaders = connection.bytesLeftInWriteWindow == 0L;
}
}

connection.writeSynReply(id, outFinished, responseHeaders);

if (outFinished) {
if (flushHeaders) {
connection.flush();
}
}
Expand Down