Repository navigation
Reuse DefaultClient keep-alive connections when chunked bodies are not read to the end - #3673
Merged
Merged
Conversation
… keep-alive connections Signed-off-by: Marvin Froeder <velo.br@gmail.com>
Signed-off-by: Marvin Froeder <velo.br@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What the profiler showed
The allocation hotspots in #2267 were
Client$Default.convertAndSend(304 GB, mostlyHashMap$Node) andClient$Default.convertResponse(186 GB, mostlybyte[]). In the flame graphs, almost all of that sits underHttpURLConnection.getOutputStream→AbstractDelegateHttpsURLConnection.connect→HttpsClient.afterConnect→SSLSocketImpl.startHandshake→SSLAlgorithmConstraints/HashSet/HashMap. So the client ran a full TLS handshake on every request. The allocations were the handshake plus fresh socket and SSL buffers for each new connection. HTTP parsing was not the cause.Root cause
HttpURLConnectiononly puts a connection back in its keep-alive cache once the response body has been read to the end. With aContent-Lengthbody, the JDK drains the rest on close. With a chunked body,ChunkedInputStream.close()only drains bytes that are alreadyavailable(). Over TLS that is usually 0, because the terminating0\r\n\r\nchunk hasn't been decrypted yet. When that happens the JDK closes the socket.Jackson-based decoders, including the Spring
SpringDecoderused in the report, stop reading at the end of the JSON value and never read the terminating chunk. Spring Boot servers typically return JSON chunked. Put together, every request opens a new HTTPS connection. Apache HttpClient drains the entity on close, which explains why switching tofeign-httpclientmade the problem go away.Fix
When the response length is unknown (chunked or close-delimited),
DefaultClientwraps the connection stream in a privateDrainOnCloseInputStream. Onclose()it reads whatever is left, up to 64 KiB, before it closes the underlying stream. In the common case, where only the terminating chunk is left, that is a singleread()returning-1and no buffer is allocated. The wrapper sits under the gzip/deflate wrappers, so compressed bodies are covered too. If more than 64 KiB is left unread, the connection is dropped as before. Bodies with a known length are left to the JDK's ownKeepAliveStreamhandling. There are no public API changes.Measurements
The test setup was MockWebServer over HTTPS. Each call was a POST with a JSON body, a header and
BasicAuthRequestInterceptor. The response was about 520 bytes of JSON sent chunked, and the decoder was a JacksonObjectMapper.readValue(InputStream). I ran 300 measured calls after 50 warm-up calls and measured allocation withThreadMXBean#getThreadAllocatedBytes.On loopback the handshake's CPU and allocation cost is about 110 KB per request. Against a real server you also save a network round trip or two for the handshake on every call.
Tests
reusesHttpsConnectionWhenChunkedBodyIsNotReadToTheEndandreusesHttpsConnectionWhenGzipBodyIsNotReadToTheEndassert that the second request reuses the connection (MockWebServer sequence number 1). Both fail on master.closingLargeUnreadChunkedBodyStillAllowsNextRequestcovers the drain limit.mvn verify -pl core -ampasses (794 tests, japicmp included).Fixes #2267