Skip to content

Reuse persistent HTTP client connections - #665

Merged
bsergean merged 1 commit into
masterfrom
feature/http-connection-reuse
Nov 18, 2026
Merged

bsergean merged 1 commit into
masterfrom
feature/http-connection-reuse

Conversation

@bsergean

Copy link
Copy Markdown
Collaborator

Repeated requests through one HttpClient currently tear down and recreate TCP/TLS connections. Retain one fully consumed HTTP/1.1 connection and reuse it for sequential requests to the same origin, including queued asynchronous requests and same-origin redirects.

  • Move the active socket into local ownership so errors, cancellation, and exceptions close it automatically. Cache it only after consuming the complete framed response, including chunked trailers. TLS/proxy setters invalidate the cache; configuration changes inside a callback cannot return an old connection to it.
  • Honor case-insensitive Connection: close tokens in either direction. Preserve repeated framing/connection fields rather than silently overwriting them; WebSocket handshake validation accepts the resulting Connection token lists. Ambiguous framing is never cached.
  • Probe idle sockets through the existing plain/TLS abstraction. If closure races the probe, retry a bodyless GET/HEAD at most once, before any response bytes have arrived. Never automatically replay an upload or other method after transport failure.
  • Drain same-origin redirect bodies without exposing intermediate bytes to download callbacks. Preserve upload replay on 307 and discard the upload source when a 303 switches to GET.
  • Have the built-in one-request HttpServer advertise Connection: close during serialization, without mutating application-owned response objects. Update one-shot test fixtures likewise.

No new client settings are needed. This retains one idle connection rather than adding a connection pool or pipelining. HTTP-to-WebSocket ownership transfer remains separate (#434).

Validation:

  • Full OpenSSL suite: 36/36 test executables pass.
  • Focused HTTP/header suites pass with Network.framework (-Werror=deprecated-declarations), mbedTLS 3 under ThreadSanitizer, and TLS/zlib disabled.
  • 13 new reuse cases / 277 assertions cover HTTP/HTTPS, chunked/streamed/gzip responses, redirects/uploads, async reuse, cancellation, trust/proxy changes, malformed framing, and deterministic stale-socket races. Cases run in milliseconds, without sleeps.
  • Local Use of common socket between ix::WebSocket and ix::HttpClient  #434 probe: two HTTP requests followed by a WebSocket upgrade now use two TCP connections instead of three; the HTTP requests share one connection.
  • Production C++11 with -pedantic-errors, formatting and whitespace checks pass.

Fixes #484.

@bsergean
bsergean merged commit b294309 into master Nov 18, 2026
14 checks passed
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.

Support HTTP client connection reuse

1 participant