HttpLimitsCollection Class
Represents a collection of HTTP limits for a specific connection group.
public sealed class HttpLimitsCollection
Namespace
Remarks
The HttpLimitsCollection class is used to manage various HTTP-related limits such as the maximum number of concurrent connections and the maximum number of requests per connection. These limits can be configured to optimize the performance and behavior of HTTP clients.
This class is typically used internally by .NET's HTTP client implementation but can be accessed for advanced scenarios requiring fine-grained control over connection pooling and request management.
Methods
-
GetLimit
(string limitName)
Retrieves the value of a specified HTTP limit.
Parameters:- limitName: The name of the limit to retrieve (e.g., "MaxConcurrentConnections").
nullif the limit is not found. -
SetLimit
(string limitName, object limitValue)
Sets the value of a specified HTTP limit.
Parameters:- limitName: The name of the limit to set.
- limitValue: The new value for the limit.
Properties
-
MaxConcurrentConnections
int
Gets or sets the maximum number of concurrent connections allowed for this connection group.
-
MaxRequestsPerConnection
int
Gets or sets the maximum number of requests allowed on a single connection before it is closed.
-
ConnectionLeaseTimeout
TimeSpan
Gets or sets the time after which an idle connection is considered stale and will be closed.
Example
The following example demonstrates how to retrieve and modify HTTP limits using the HttpLimitsCollection.
using System;
using System.Net.Http;
using System.Net.Http.Headers;
public class HttpLimitsExample
{
public static void Main(string[] args)
{
var httpClient = new HttpClient();
var connectionGroup = new ConnectionGroup("my-api-group"); // Assuming a hypothetical ConnectionGroup class
// Access the HttpLimitsCollection for the connection group
HttpLimitsCollection limits = connectionGroup.HttpLimits;
// Get current limits
int maxConnections = (int)limits.GetLimit("MaxConcurrentConnections");
Console.WriteLine($"Current MaxConcurrentConnections: {maxConnections}");
TimeSpan leaseTimeout = (TimeSpan)limits.GetLimit("ConnectionLeaseTimeout");
Console.WriteLine($"Current ConnectionLeaseTimeout: {leaseTimeout.TotalSeconds} seconds");
// Set new limits
limits.SetLimit("MaxConcurrentConnections", 20);
limits.SetLimit("MaxRequestsPerConnection", 100);
limits.ConnectionLeaseTimeout = TimeSpan.FromMinutes(5);
Console.WriteLine($"Updated MaxConcurrentConnections: {limits.MaxConcurrentConnections}");
Console.WriteLine($"Updated ConnectionLeaseTimeout: {limits.ConnectionLeaseTimeout.TotalMinutes} minutes");
// In a real scenario, these limits would influence how HttpClient manages connections.
}
}
// Hypothetical class for demonstration purposes
public class ConnectionGroup
{
public HttpLimitsCollection HttpLimits { get; }
public ConnectionGroup(string name)
{
HttpLimits = new HttpLimitsCollection();
// Initialize with default values or load from configuration
HttpLimits.SetLimit("MaxConcurrentConnections", 10);
HttpLimits.SetLimit("MaxRequestsPerConnection", 50);
HttpLimits.ConnectionLeaseTimeout = TimeSpan.FromSeconds(30);
}
}