Skip to content

client: HttpClientBuilder with connect_timeout - #117

Merged
azdagron merged 2 commits into
mainfrom
andrewh/http-client-connect-timeout
May 19, 2026
Merged

azdagron merged 2 commits into
mainfrom
andrewh/http-client-connect-timeout

Conversation

@azdagron

Copy link
Copy Markdown
Collaborator

Motivation

HttpClient::plaintext() / plaintext_http2_only() / with_tls() each construct an HttpConnector inline with no way to set connect_timeout. The TCP connect phase is therefore bounded only by the kernel's tcp_syn_retries (~130s on Linux defaults). Callers on network paths that can silently drop SYNs — NAT gateways under churn are the motivating case — have no way to fail fast short of a per-request timeout, which conflates the connect phase with TLS handshake and request body.

What

Adds HttpClientBuilder, obtained via HttpClient::builder(), with a connect_timeout(Duration) setter that flows through to HttpConnector::set_connect_timeout for all three transport flavours. The three existing associated constructors now delegate to the builder with no timeout set, so behaviour is unchanged for existing callers and the connector setup lives in one place.

The setter bounds only the TCP connect(2) call (per resolved address — hyper divides the budget across the address set). It does not cover DNS resolution or, for with_tls, the TLS handshake. The doc comment points callers at CallOptions::with_timeout for an end-to-end bound; connect_timeout is for the case where you want to retry connect quickly without aborting an in-flight request body.

Not in this PR

Http2Connection / MakeSendRequest in client/http2.rs build their own HttpConnector instances and are left untouched. Happy to follow up with the same builder shape there if there's demand; the immediate consumer uses HttpClient.

Testing

http_client_connect_timeout_bounds_tcp_connect sends to a TEST-NET-1 address (RFC 5737, guaranteed unroutable) with a 100 ms connect timeout and asserts the failure surfaces within 2 s — well under the kernel SYN-retry floor an unbounded connect would hit. Existing 324 lib tests pass unchanged.

HttpClient::plaintext()/plaintext_http2_only()/with_tls() each construct an
HttpConnector inline with no way to set connect_timeout. That leaves the TCP
connect phase bounded only by kernel tcp_syn_retries (~130s on Linux defaults).
Callers on paths that can silently drop SYNs (e.g. NAT gateways under churn)
have no way to fail fast short of a per-request timeout that conflates connect
with the request body.

This adds HttpClientBuilder, obtained via HttpClient::builder(), with a
connect_timeout(Duration) setter that flows through to
HttpConnector::set_connect_timeout for all three transport flavours. The
existing constructors delegate to the builder with no timeout set, preserving
behaviour. Http2Connection/MakeSendRequest are left untouched for now —
follow-up if there is demand.

The setter bounds only the TCP connect(2) call (per resolved address; hyper
divides the budget across the address set). DNS resolution and the TLS
handshake are not covered; the doc comment points callers at
CallOptions::with_timeout for an end-to-end bound.
@github-actions

github-actions Bot commented May 19, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@azdagron

Copy link
Copy Markdown
Collaborator Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request May 19, 2026
@azdagron
azdagron requested a review from iainmcgin May 19, 2026 14:14
@azdagron
azdagron enabled auto-merge (squash) May 19, 2026 16:22
@azdagron
azdagron merged commit cdb0caa into main May 19, 2026
12 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators May 19, 2026
@azdagron
azdagron deleted the andrewh/http-client-connect-timeout branch May 19, 2026 17:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants