fix: replace per-call HttpClient with shared static instance to prevent socket exhaustion#46
Draft
DaraOladapo wants to merge 1 commit into
Draft
fix: replace per-call HttpClient with shared static instance to prevent socket exhaustion#46DaraOladapo wants to merge 1 commit into
DaraOladapo wants to merge 1 commit into
Conversation
…nt socket exhaustion Every public method was creating and immediately disposing a new HttpClient inside a using block. Disposing HttpClient does not release the underlying socket immediately, causing socket exhaustion under load. Fix: introduce a single private static readonly HttpClient _httpClient on the partial class (declared in Get.cs). All methods that need per-request headers or auth now use HttpRequestMessage plus SendAsync so headers are scoped to the individual request and never mutate the shared client DefaultRequestHeaders. Simple no-header overloads use the HttpClient convenience methods directly. Closes #36 https://claude.ai/code/session_01VwmzESpT2AXosoWQKJtuuQ
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.
Summary
new HttpClient()per-call anti-pattern across all five verb files (Get.cs,Post.cs,Put.cs,Patch.cs,Delete.cs)private static readonly HttpClient _httpClienton the partial class (declared inGet.cs, shared across all partials)HttpRequestMessage+SendAsyncso headers are scoped to the individual request and never mutateDefaultRequestHeaderson the shared instance — thread-safe under concurrent callsHttpClientconvenience methods (GetAsync,DeleteAsync) directlyCloses #36
Why this matters
Disposing
HttpClientinside ausingblock does not immediately release the underlying TCP socket — it entersTIME_WAIT. Under any meaningful request load this exhausts the local socket pool and causesSocketException. This is a well-documented .NET anti-pattern (Microsoft docs).Test plan
dotnet test)GetAsync_NoAuth_DoesNotSendAuthHeaderconfirms no header leakage between calls on the shared instanceGetAsync_WithToken_SendsBearerHeaderconfirms per-request auth is still correctly appliedhttps://claude.ai/code/session_01VwmzESpT2AXosoWQKJtuuQ
Generated by Claude Code