fix: use persistent httpx.Client to eliminate per-call TCP/TLS overhead#36
Open
sameer-ahmed wants to merge 2 commits into
Open
fix: use persistent httpx.Client to eliminate per-call TCP/TLS overhead#36sameer-ahmed wants to merge 2 commits into
sameer-ahmed wants to merge 2 commits into
Conversation
Using httpx.request() (the top-level convenience function) creates a brand new TCP connection and TLS handshake on every single API call. On internal or corporate Redmine servers this can add ~2 minutes of overhead per request, making bulk operations extremely slow. Replace with a module-level httpx.Client instance that reuses the underlying connection across calls, reducing latency to milliseconds after the first connection is established.
f39694d to
4adf9df
Compare
httpx default keepalive_expiry is 5s, causing a full ~600ms TLS reconnect on every call after a short pause. Set keepalive_expiry=120s and max_keepalive_connections=5 to reuse connections across calls.
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.
Problem
httpx.request()(the top-level convenience function) creates a brand new TCP connection and TLS handshake on every single API call. On internal/corporate Redmine servers this adds ~2 minutes of overhead per request, making bulk operations extremely slow.We measured this empirically:
Fix
Replace
httpx.request()with a module-levelhttpx.Clientinstance that reuses the underlying TCP/TLS connection across calls. After the first connection is established, subsequent calls reuse it — reducing latency to milliseconds.Why this happens
httpx.request()is a convenience wrapper that internally does:The
withblock closes the client (and its connection pool) after every call. The persistent client avoids this entirely.This is consistent with the httpx docs recommendation — use a
Clientinstance for multiple requests rather than the top-level API.Impact