Skip to content

Introduce HTTP::Session for thread-safe request building#829

Merged
sferik merged 2 commits intomainfrom
session-builder
Mar 9, 2026
Merged

Introduce HTTP::Session for thread-safe request building#829
sferik merged 2 commits intomainfrom
session-builder

Conversation

@sferik
Copy link
Contributor

@sferik sferik commented Mar 9, 2026

This patch changes chainable option methods (headers, timeout, cookies, etc.) to return an HTTP::Session instead of an HTTP::Client.

Session is a lightweight, thread-safe builder that creates a fresh Client for each request, fixing the thread-safety issue where sharing a configured client across threads caused IOError due to shared mutable connection state.

The second commit changes HTTP.retriable to return an HTTP::Session. Retry is now a session-level option (like follow) that flows through HTTP::Options into Client#perform, eliminating the Retriable::Client class. (Thanks to @ixti for this suggestion.)

Note

HTTP.persistent still returns an HTTP::Client since persistent connections require mutable state.

Warning

This is a breaking change! Code that checks for is_a?(HTTP::Client) on the return value of chainable methods will need to be updated to check for HTTP::Session. Code that checks for is_a?(HTTP::Retriable::Client) will need to be refactored. I’m proposing it to be included in the upcoming v6.0.0 release.

@sferik sferik added this to the v6.0.0 milestone Mar 9, 2026
@tarcieri
Copy link
Member

tarcieri commented Mar 9, 2026

Yay! Once upon a time it worked like this, but it's been broken for more than a decade.

This potentially closes #306 and #558

sferik added 2 commits March 9, 2026 08:35
Chainable option methods (headers, timeout, cookies, etc.) now return an
HTTP::Session instead of an HTTP::Client. Session is a lightweight,
thread-safe builder that creates a fresh Client for each request, fixing
the thread-safety issue where sharing a configured client across threads
caused IOError due to shared mutable connection state.

HTTP.retriable now returns an HTTP::Retriable::Session.

HTTP.persistent still returns an HTTP::Client since persistent
connections require mutable state.

Closes #306.
Make retriable a first-class option on HTTP::Options (like `follow`),
eliminating the need for Retriable::Client and Retriable::Session.
Client#perform now checks options.retriable and delegates to
Retriable::Performer when set, keeping retry as a per-request concern
that flows naturally through the options system.
@sferik sferik force-pushed the session-builder branch from 3c90ad1 to b4fe926 Compare March 9, 2026 15:36
@sferik
Copy link
Contributor Author

sferik commented Mar 9, 2026

@ixti I'm interested to get your feedback on this but I’m going to merge it so I can start working on #536 and some other v6.0.0 issues that depend on it. But I’d be happy to incorporate any feedback you have before we ship it.

@sferik sferik merged commit 83f4bf5 into main Mar 9, 2026
8 checks passed
@sferik sferik deleted the session-builder branch March 9, 2026 15:41
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.

2 participants