fix: replace per-call new HttpClient() with shared static instance#47
Draft
DaraOladapo wants to merge 1 commit into
Draft
fix: replace per-call new HttpClient() with shared static instance#47DaraOladapo wants to merge 1 commit into
DaraOladapo wants to merge 1 commit into
Conversation
Every method was constructing and disposing an HttpClient inside a using block. Under load this causes socket exhaustion (TIME_WAIT) because the OS cannot release underlying sockets immediately after Dispose. Fixes #36. Introduced a single private static readonly HttpClient _httpClient in the partial class (Get.cs). All methods now build a per-request HttpRequestMessage so per-call headers (Authorization, custom) are isolated and thread-safe — DefaultRequestHeaders on the shared instance is never mutated.
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
Closes #36.
Every public method in the library was creating and disposing a fresh
HttpClientinside ausingblock:This is the well-documented .NET anti-pattern described in Microsoft's HttpClient guidelines. Each construction opens a new socket;
Dispose()does not immediately release it — the OS holds the connection inTIME_WAIT. Under any meaningful request volume this exhausts the available socket pool and causesSocketException.Fix
Introduced a single
private static readonly HttpClient _httpClientinGet.cs(shared across all partial-class files). All active methods now build a per-requestHttpRequestMessageand pass it to_httpClient.SendAsync(request):Using
HttpRequestMessageis required here: per-call headers (Authorization, custom) are set on the message, not onDefaultRequestHeaders, so concurrent calls cannot race on shared state.Files changed
Get.cs_httpClientstatic field; all 8 active methods useHttpRequestMessagePost.csHttpRequestMessagePut.csHttpRequestMessagePatch.csHttpRequestMessageDelete.csHttpRequestMessageNon-breaking
Public API signatures are unchanged. The
#region TODOcommented-out blocks are preserved (tracked separately in #37).Test plan
dotnet build)dotnet test)SocketExceptionunder concurrent load (manual or load test)Generated by Claude Code