Skip to content

fix: replace per-call HttpClient with shared static instance to prevent socket exhaustion#46

Draft
DaraOladapo wants to merge 1 commit into
mainfrom
claude/relaxed-tesla-SsbKj
Draft

fix: replace per-call HttpClient with shared static instance to prevent socket exhaustion#46
DaraOladapo wants to merge 1 commit into
mainfrom
claude/relaxed-tesla-SsbKj

Conversation

@DaraOladapo

Copy link
Copy Markdown
Owner

Summary

  • Fixes the new HttpClient() per-call anti-pattern across all five verb files (Get.cs, Post.cs, Put.cs, Patch.cs, Delete.cs)
  • Introduces a single private static readonly HttpClient _httpClient on the partial class (declared in Get.cs, shared across all partials)
  • Methods with per-request headers or auth now use HttpRequestMessage + SendAsync so headers are scoped to the individual request and never mutate DefaultRequestHeaders on the shared instance — thread-safe under concurrent calls
  • Simple no-header overloads use the HttpClient convenience methods (GetAsync, DeleteAsync) directly
  • Zero breaking changes — all public signatures are identical

Closes #36

Why this matters

Disposing HttpClient inside a using block does not immediately release the underlying TCP socket — it enters TIME_WAIT. Under any meaningful request load this exhausts the local socket pool and causes SocketException. This is a well-documented .NET anti-pattern (Microsoft docs).

Test plan

  • All existing WireMock tests pass (dotnet test)
  • GetAsync_NoAuth_DoesNotSendAuthHeader confirms no header leakage between calls on the shared instance
  • GetAsync_WithToken_SendsBearerHeader confirms per-request auth is still correctly applied

https://claude.ai/code/session_01VwmzESpT2AXosoWQKJtuuQ


Generated by Claude Code

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace new HttpClient() per-call with a shared/reusable instance

2 participants