HttpClient
Timeout
.NET
C#
Networking

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.

Practice system design

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:

  1. Resource Management: Creating a new HttpClient for 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.
  2. 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 HttpClient instance is shared, thus benefiting from connection pooling.
  • CancellationTokenSource: For each request, a CancellationTokenSource is created with its timeout configured using the CancelAfter method. This method allows you to simulate diverse timeout conditions on a per-request basis.
  • Request Execution: Pass the token from CancellationTokenSource into the request. If the operation exceeds the allotted timeout, an OperationCanceledException is thrown.
  • Timeout Policy: Wraps the request execution logic with a TimeoutAsync policy, 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.