Honor server Keep-Alive timeout hints in the HTTP/1 connection pool - #1358
Open
quinnj wants to merge 1 commit into
Open
Honor server Keep-Alive timeout hints in the HTTP/1 connection pool#1358quinnj wants to merge 1 commit into
quinnj wants to merge 1 commit into
Conversation
Servers such as Apache, nginx and HAProxy advertise their idle keep-alive window on responses as `Keep-Alive: timeout=<seconds>`. The pool only knew its own `idle_timeout_ns` (default 90s), so a connection could be handed out again after the server had already closed it at its own (often 5s) deadline. Idempotent requests then burned a retry and everything else failed outright. Each `Conn` now records the advertised window (less a one-second safety margin, since the server's clock starts before the client finishes reading the response) and `_evict_expired_idle_locked!` applies it alongside `idle_timeout_ns`, whichever is tighter. A hint that leaves no headroom marks the connection non-reusable. `max=<n>` is ignored: a server that exhausts it answers with `Connection: close`, which is already honored. Fixes #1155 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1358 +/- ##
==========================================
+ Coverage 89.42% 89.45% +0.03%
==========================================
Files 31 31
Lines 12594 12627 +33
==========================================
+ Hits 11262 11296 +34
+ Misses 1332 1331 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Fixes #1155.
Problem
Servers such as Apache, nginx and HAProxy advertise their idle keep-alive window on responses as
Keep-Alive: timeout=<seconds>(Apache's default is 5 seconds). The HTTP/1 pool only knew its ownidle_timeout_ns(default 90 seconds), so a pooled connection could be handed out again well after the server had already closed it at its own deadline. An idempotent request then burned a retry (_retryable_reused_conn_error), and a non-idempotent one failed outright.Change
Conngains akeepalive_timeout_nsfield. After a reusable response is read,_note_keepalive_hint!parses the response'sKeep-Aliveheader and records the advertised window less a one-second safety margin. The margin exists because the server's clock starts when it finishes sending the response, before the client has finished reading it (this mirrors Node's undicikeepAliveTimeoutThreshold)._evict_expired_idle_locked!now expires an idle connection when it exceeds either the pool'sidle_timeout_nsor the connection's own server-advertised bound, whichever is tighter.timeout=1ortimeout=0) marks the connection non-reusable, so the pool never keeps a connection the server is about to drop.Timeout = 3), quoted values, and multiple header lines. Malformed or signed values are ignored (treated as no hint). Absurd values saturate instead of overflowing.max=<n>is intentionally ignored: a server that exhausts it answers withConnection: close, which the pool already honors.Transportdocstring and CHANGELOG updated.Tests
Added to
test/http_client_transport_tests.jl:Keep-Alive timeout hint parsing (#1155): 17 parser cases.HTTP client transport evicts an idle conn past its server keep-alive window (#1155): pool idle timeout disabled, only the per-connection bound evicts the stale peer and the fresh one is reused.HTTP client transport honors the server Keep-Alive timeout hint end to end (#1155): raw TCP server replies withtimeout=5, max=100(one connection serves both requests, pooled bound is 4s),max=100(reused, no bound), andtimeout=1(not pooled, second request dials a new connection).Locally
test/http_client_transport_tests.jlpasses in full (36 testsets, 0 failures) on Julia 1.12.6 with 4 threads.🤖 Generated with Claude Code