Re-using HttpClient but with a different Timeout setting per request?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
The HttpClient in .NET is a class that is notoriously misunderstood when it comes to its proper usage and lifecycle management. One of the key aspects that developers must grasp is reusability: how you can maintain efficiency by reusing the same instance of HttpClient
. However, a common scenario arises when you need different timeout settings for different HTTP requests. Let's explore how you can achieve this without compromising on effectiveness or resource consumption.
Why Reuse HttpClient?
First, let's touch upon why reusing HttpClient
is recommended:
- Resource Management: Creating a new
HttpClientfor each request can lead to resource exhaustion. It uses native sockets, which, when discarded improperly, can cause socket exhaustion—a state where your application can’t open new sockets due to resource leaks. - Connection Pooling: A single instance will make use of an internal Connection Pool, reusing existing TCP connections for subsequent requests to the same host. This reuse results in reduced latency and improved application performance.
Reusing HttpClient
is crucial for both performance and resource use, yet dynamically changing timeout settings for different requests adds complexity.
Setting Timeout Flexibly
By default, the HttpClient
's Timeout
property is applied at the instance level. If you change it, it affects all subsequent requests. However, there is a workaround to set different timeouts by employing CancellationTokenSource
. Here is how you can achieve that:
Code Example Using CancellationTokenSource
- Static HttpClient: A static
HttpClientinstance is shared, thus benefiting from connection pooling. - CancellationTokenSource: For each request, a
CancellationTokenSourceis created with its timeout configured using theCancelAftermethod. This method allows you to simulate diverse timeout conditions on a per-request basis. - Request Execution: Pass the token from
CancellationTokenSourceinto the request. If the operation exceeds the allotted timeout, anOperationCanceledExceptionis thrown. - Timeout Policy: Wraps the request execution logic with a
TimeoutAsyncpolicy, allowing you to keep timeout settings modular and reusable. - Exception Handling: The timeout exception thrown is specific to Polly, allowing more granular exception handling.
Related reading
- React - Fetch from external API function on button click
- Read asynchronously data from NetworkStream with huge amount of packets
- read kafka message starting from a specific offset using high level API
- Reading streaming http response with Python requests library
- Read-only list or unmodifiable list in .NET 4.0
- Read connection string from web.config
- Read/Write String from/to a File in Android
- (Re)attaching to an App Insights Operation from another machine/process (not using HTTP)

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.