Diagnosing Intermittent Connection Pool Exhaustion in Xamarin HttpClient
29.3K reputation · 02 Dec 2022, 14:16 UTC
Problem Description
Xamarin’s HttpClient relies on platform‑specific handlers (OkHttp on Android, CFNetwork on iOS). The default connection pool is opaque; no public API exposes its size or timeout. When many concurrent requests are issued, the pool may silently drop connections after a timeout, leading to intermittent failures.
Unresolved Decision
For projects targeting .NET Standard 2.0 or earlier, HttpClientFactory is unavailable, leaving developers to choose between creating a new HttpClient per request or reusing a single shared instance. The documentation does not specify the exact pool size, timeout thresholds, or how the underlying platform limits (e.g., OkHttp’s 5 max per host) interact with Xamarin’s pool, creating uncertainty around how to avoid exhaustion in high‑traffic scenarios.
What is the default connection pool size for Xamarin’s HttpClient on Android and iOS? How do platform limits affect the pool when many simultaneous requests are made? Does reusing a single HttpClient reliably prevent exhaustion, or are there hidden side effects such as stale DNS or cookie data?
1 answer
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
29,270 reputation · 02 Dec 2022, 22:21 UTC
On iOS, the httpMaximumConnectionsPerHost = 6 limit applies to HTTP/1.1 connections; when the server negotiates HTTP/2, NSURLSession multiplexes multiple streams over a single connection, so the effective concurrent request count can exceed six without creating additional sockets.
On Android, OkHttp’s connection pool defaults to a maximum of five idle connections total (not just per host) and a keep‑alive of five minutes. The Xamarin AndroidClientHandler ignores HttpClientHandler.MaxConnectionsPerServer; to change the pool size you must supply a custom OkHttpClient via a handler constructor.