Skip to content

Reuse DefaultClient keep-alive connections when chunked bodies are not read to the end - #3673

Merged
velo merged 2 commits into
masterfrom
fix/2267-default-client-allocations
Nov 10, 2026
Merged

velo merged 2 commits into
masterfrom
fix/2267-default-client-allocations

Conversation

@velo

@velo velo commented Nov 9, 2026

Copy link
Copy Markdown
Member

What the profiler showed

The allocation hotspots in #2267 were Client$Default.convertAndSend (304 GB, mostly HashMap$Node) and Client$Default.convertResponse (186 GB, mostly byte[]). In the flame graphs, almost all of that sits under HttpURLConnection.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

HttpURLConnection only puts a connection back in its keep-alive cache once the response body has been read to the end. With a Content-Length body, the JDK drains the rest on close. With a chunked body, ChunkedInputStream.close() only drains bytes that are already available(). Over TLS that is usually 0, because the terminating 0\r\n\r\n chunk hasn't been decrypted yet. When that happens the JDK closes the socket.

Jackson-based decoders, including the Spring SpringDecoder used 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 to feign-httpclient made the problem go away.

Fix

When the response length is unknown (chunked or close-delimited), DefaultClient wraps the connection stream in a private DrainOnCloseInputStream. On close() 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 single read() returning -1 and 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 own KeepAliveStream handling. 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 Jackson ObjectMapper.readValue(InputStream). I ran 300 measured calls after 50 warm-up calls and measured allocation with ThreadMXBean#getThreadAllocatedBytes.

scenario before after
HTTPS, chunked, Jackson decoder 168,676 B/call, 350/350 new connections 58,757 B/call, 1/350 new connections
HTTPS, Content-Length, Jackson decoder 54,173 B/call, 1/350 54,173 B/call, 1/350
HTTPS, chunked, body fully read 62,139 B/call, 1/350 62,181 B/call, 1/350
plain HTTP, chunked, Jackson decoder 54,764 B/call, 1/350 54,813 B/call, 1/350

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

  • reusesHttpsConnectionWhenChunkedBodyIsNotReadToTheEnd and reusesHttpsConnectionWhenGzipBodyIsNotReadToTheEnd assert that the second request reuses the connection (MockWebServer sequence number 1). Both fail on master.
  • closingLargeUnreadChunkedBodyStillAllowsNextRequest covers the drain limit.

mvn verify -pl core -am passes (794 tests, japicmp included).

Fixes #2267

velo added 2 commits November 9, 2026 17:59
… keep-alive connections

Signed-off-by: Marvin Froeder <velo.br@gmail.com>
Signed-off-by: Marvin Froeder <velo.br@gmail.com>
@velo
velo enabled auto-merge (squash) November 10, 2026 10:50
@velo
velo merged commit a4a978e into master Nov 10, 2026
7 checks passed
@velo
velo deleted the fix/2267-default-client-allocations branch November 10, 2026 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance discussion about default openfeign client

1 participant