Skip to content

Reuse idle connections across requests - #48

Open
toots wants to merge 2 commits into
andersfugmann:mainfrom
toots:keep-alive-connections
Open

Reuse idle connections across requests#48
toots wants to merge 2 commits into
andersfugmann:mainfrom
toots:keep-alive-connections

Conversation

@toots

@toots toots commented Aug 10, 2026

Copy link
Copy Markdown

Every request opened its own socket and paid a TCP and TLS handshake for it, although S3 and the S3-compatible endpoints all speak HTTP/1.1 keep-alive, so idle connections are now pooled per (scheme, host, port) and handed back out; a connection is only pooled when the response was framed well enough for its body to have been fully consumed and did not ask for Connection: close.

Peers reap idle connections on their own schedule -- Backblaze B2 aggressively so -- and handing out one the peer has already closed costs a failed request, so an entry idle for longer than AWS_S3_POOL_MAX_IDLE_S (20s by default) is dropped instead, and a bodyless request that fails on a reused connection is retried once on a fresh one.

The first commit is an independent lwt fix that reuse made visible: a write raising EPIPE on a peer-closed socket escaped through Lwt.async and left the in-flight request waiting on a response that could never arrive, and a TLS shutdown over a dead socket did the same while leaving the fd in CLOSE-WAIT.

toots added 2 commits August 10, 2026 07:45
A write raising EPIPE on a peer-closed socket escaped through Lwt.async, leaving the in-flight request waiting on a response that could never arrive; the read side is now closed so the pending read errors out instead.

Closing the channels is best-effort for the same reason, as a TLS shutdown over a dead socket raises and left the fd in CLOSE-WAIT.
Every request opened its own socket, paying a TCP and TLS handshake each time, although S3 and the S3-compatible endpoints all speak HTTP/1.1 keep-alive; idle connections are now pooled per (scheme, host, port) and handed back out, and a connection is only pooled when the response was framed well enough for the body to have been fully consumed.

Peers reap idle connections on their own schedule, so an entry older than AWS_S3_POOL_MAX_IDLE_S (20s by default) is dropped rather than handed out, and a bodyless request that fails on a reused connection is retried once on a fresh one.
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.

1 participant