Introduce HTTP::Session for thread-safe request building#829
Merged
Conversation
Member
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.
Contributor
Author
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.
This patch changes chainable option methods (
headers,timeout,cookies, etc.) to return anHTTP::Sessioninstead of anHTTP::Client.Sessionis a lightweight, thread-safe builder that creates a freshClientfor each request, fixing the thread-safety issue where sharing a configured client across threads causedIOErrordue to shared mutable connection state.The second commit changes
HTTP.retriableto return anHTTP::Session. Retry is now a session-level option (likefollow) that flows throughHTTP::OptionsintoClient#perform, eliminating theRetriable::Clientclass. (Thanks to @ixti for this suggestion.)Note
HTTP.persistentstill returns anHTTP::Clientsince 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 forHTTP::Session. Code that checks foris_a?(HTTP::Retriable::Client)will need to be refactored. I’m proposing it to be included in the upcoming v6.0.0 release.