Skip to content

fix: Share reqwest HTTP clients#2152

Open
tmathern wants to merge 13 commits into
mainfrom
mathern/pool-reqwest
Open

fix: Share reqwest HTTP clients#2152
tmathern wants to merge 13 commits into
mainfrom
mathern/pool-reqwest

Conversation

@tmathern
Copy link
Copy Markdown
Contributor

@tmathern tmathern commented May 15, 2026

Changes in this pull request

  • Share reqwest clients (avoid client recreation and related memory consumption).

Checklist

  • This PR represents a single feature, fix, or change.
  • All applicable changes have been documented.
  • Any TO DO items (or similar) have been entered as GitHub issues and the link to that issue has been included in a comment.

@tmathern tmathern self-assigned this May 15, 2026
@tmathern tmathern added safe to test check-release Add this label to any PR to invoke a larger suite of tests. labels May 15, 2026
@tmathern tmathern requested review from gpeacock and ok-nick May 15, 2026 14:44
Comment thread sdk/src/assertions/uuid_assertion.rs
@tmathern tmathern marked this pull request as ready for review May 15, 2026 14:47
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 15, 2026

Merging this PR will not alter performance

✅ 30 untouched benchmarks
⏩ 64 skipped benchmarks1


Comparing mathern/pool-reqwest (98ddb7d) with main (350e138)

Open in CodSpeed

Footnotes

  1. 64 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Comment thread sdk/src/http/reqwest.rs Outdated
@tmathern tmathern requested a review from gpeacock May 15, 2026 17:13
Comment thread sdk/src/http/reqwest.rs
Comment on lines +31 to +32
static SYNC_CLIENT: OnceLock<reqwest::blocking::Client> = OnceLock::new();
static SYNC_CLIENT_REDIRECTS: OnceLock<reqwest::blocking::Client> = OnceLock::new();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO I prefer to not by default store global state like this. I'd rather the caller of the library chooses to store the HTTP client as global state if they'd like. I'm not sure if it has meaningful impact on memory/perf. I defer to @gpeacock on the final decision.

Copy link
Copy Markdown
Contributor Author

@tmathern tmathern May 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally, the client itself is stateless (it is not state that changes behavior). You are reusing HTTP plumbing, so you do not recreate it, with all the underlying plumbing and needed memory, on the flight, on each call you need it.

A new reqwest client goes through reqwest::new, which spawns a tokio runtime current thread on this thread, with its conenction pool.

Now imagine this in a server under load. N concurrent requests, each on its own worker thread, each going through this (and a few fetches), rebuilding on each call. Without sharing, every worker thread spawns its own runtime thread, and undergoes init again. (I don't think it impacts perf yet, but memory: threads do not come for free).

The caller could, choose, yes, but we also should have our default part reuse this for better defaults. The two statics only govern the default built-in resolver, where the library owns the lifecycle, not the caller. And the OnceLock is lazy, so if the network path is never touched, nothing is allocated.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is ok, but I'm nervous about it after the problems we had with settings that were held in a thread local

@tmathern tmathern requested a review from ok-nick May 20, 2026 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

check-release Add this label to any PR to invoke a larger suite of tests. safe to test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants