Repository navigation
Reuse keep-alive HTTP connections after a fully read response - #587
Merged
Merged
Conversation
The driver dropped the keep-alive connection after every request. Before sending a new request, and when closing a cursor, it checked the previous response stream with `in->fail() || !in->eof()`. AmortizedIStreamReader reads ahead in blocks of at least 8 KB, so the read that reaches the end of the body always stops short and sets both eofbit and failbit. That is the normal end-of-response state, but the check treated it as an error and reset the session, so every query opened a new TCP connection (and a new TLS handshake over HTTPS). Replace the check with Statement::resetConnectionIfNeeded(), which resets the session only when the stream is bad, the body has unread bytes, or the session recorded a socket exception. closeCursor() now destroys the result reader before inspecting the stream: the reader's destructor puts back pre-read bytes, which clears eofbit and can set badbit, so checking first could reuse a connection that still has unread rows on the wire. Test server: serve multiple requests per connection so keep-alive can actually be exercised, bind to an ephemeral loopback port instead of the hard-coded 8124, count accepted connections, and guard the response data with a mutex. Fix the ANSI networking test binary, which loaded the Unicode driver, and size the zstd output buffer with ZSTD_compressBound so compressing an empty body no longer produces a zero-length buffer. Add tests asserting the connection count across repeated selects and inserts (chunked, Content-Length, compressed), and that the driver reconnects when the server closes, when a cursor is closed with unread data, and after an incomplete response.
chernser
approved these changes
Sep 24, 2026
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.
The driver dropped the keep-alive connection after every request. Before sending a new request, and when closing a cursor, it checked the previous response stream with
in->fail() || !in->eof().AmortizedIStreamReaderreads ahead in blocks of at least 8 KB, so the read that reaches the end of the body always stops short and sets botheofbitandfailbit. That is the normal end-of-response state, but the check treated it as an error and reset the session, so every query opened a new TCP connection (and a new TLS handshake over HTTPS).