Class ThrottleQuota
- Namespace
- Cuemon.AspNetCore.Http.Throttling
- Assembly
- Cuemon.AspNetCore.dll
Specifies the allowed quota and window duration of HTTP requests.
public class ThrottleQuota
- Inheritance
-
ThrottleQuota
Examples
The following example demonstrates how to create ThrottleQuota instances using different time units and a TimeSpan directly. It then tracks request usage with ThrottleRequest, including incrementing and refreshing the window.
using System;
using Cuemon;
using Cuemon.AspNetCore.Http.Throttling;
namespace MyApp.Http.Throttling
{
public class ThrottleQuotaExample
{
public void Demonstrate()
{
// Allow 100 requests per 1 minute window
var quotaPerMinute = new ThrottleQuota(100, 1, TimeUnit.Minutes);
Console.WriteLine($"Rate limit: {quotaPerMinute.RateLimit}");
Console.WriteLine($"Window: {quotaPerMinute.Window.TotalMinutes} min");
// Allow 1000 requests per 1 hour window (using TimeSpan directly)
var quotaPerHour = new ThrottleQuota(1000, TimeSpan.FromHours(1));
Console.WriteLine($"Rate limit: {quotaPerHour.RateLimit}");
Console.WriteLine($"Window: {quotaPerHour.Window.TotalHours} h");
// Allow 10 requests per 15 seconds
var quotaPer15Sec = new ThrottleQuota(10, 15, TimeUnit.Seconds);
Console.WriteLine($"Rate limit: {quotaPer15Sec.RateLimit}");
Console.WriteLine($"Window: {quotaPer15Sec.Window.TotalSeconds} s");
// Use with ThrottleRequest to track usage
var request = new ThrottleRequest(quotaPerMinute);
Console.WriteLine($"Initial total: {request.Total}");
Console.WriteLine($"Expires at: {request.Expires:R}");
request.IncrementTotal();
Console.WriteLine($"After one request: {request.Total}");
request.Refresh(); // resets if window has expired
}}
}
Constructors
ThrottleQuota(int, double, TimeUnit)
Initializes a new instance of the ThrottleQuota class.
public ThrottleQuota(int rateLimit, double window, TimeUnit windowUnit)
Parameters
rateLimitintThe allowed rate from within a given
window.windowdoubleThe duration of the window.
windowUnitTimeUnitOne of the enumeration values that specifies the time unit of
window.
ThrottleQuota(int, TimeSpan)
Initializes a new instance of the ThrottleQuota class.
public ThrottleQuota(int rateLimit, TimeSpan window)
Parameters
rateLimitintThe allowed rate from within a given
window.windowTimeSpanThe duration of the window.
Properties
RateLimit
Gets the allowed rate before throttling.
public int RateLimit { get; }
Property Value
- int
The allowed rate before throttling.
Window
Gets the allowed window duration before throttling.
public TimeSpan Window { get; }
Property Value
- TimeSpan
The allowed window duration before throttling.