Class ThrottlingRateLimiter
A rate limiter that controls the rate of operations by limiting concurrency and applying delays when a specified threshold of concurrency usage is reached.
The delay algorithm checks the current available permits from the concurrency limiter. If the available permits are greater than or equal to the throttling threshold, no delay is applied. Otherwise, it calculates a delay based on the percentage of permits used, scaling it up to a maximum of 1000 milliseconds.
public class ThrottlingRateLimiter : RateLimiter, IAsyncDisposable, IDisposable
- Inheritance
-
ThrottlingRateLimiter
- Implements
- Inherited Members
Constructors
ThrottlingRateLimiter(int, int?)
Initializes a new instance of the ThrottlingRateLimiter class with the specified maximum number of concurrent calls and an optional throttling percentage.
public ThrottlingRateLimiter(int maxConcurrentCalls, int? throttlingPercentage = 50)
Parameters
maxConcurrentCalls
intThe maximum number of concurrent operations allowed.
throttlingPercentage
int?The percentage of
maxConcurrentCalls
at which throttling is triggered. Defaults to 50% if not specified.
Fields
DefaultThrottlingPercentage
The default throttling percentage, which defines the threshold for applying throttling, set to 50%.
public const int DefaultThrottlingPercentage = 50
Field Value
Properties
IdleDuration
Specifies how long the RateLimiter has had all permits available. Used by RateLimiter managers that may want to clean up unused RateLimiters.
public override TimeSpan? IdleDuration { get; }
Property Value
Remarks
Returns null when the RateLimiter is in use or is not ready to be idle.
Methods
AcquireAsyncCore(int, CancellationToken)
Method that RateLimiter implementations implement for AcquireAsync(int, CancellationToken).
protected override ValueTask<RateLimitLease> AcquireAsyncCore(int permitCount, CancellationToken cancellationToken)
Parameters
permitCount
intNumber of permits to try and acquire.
cancellationToken
CancellationTokenOptional token to allow canceling a queued request for permits.
Returns
- ValueTask<RateLimitLease>
A task that completes when the requested permits are acquired or when the requested permits are denied.
AttemptAcquireCore(int)
Method that RateLimiter implementations implement for AttemptAcquire(int).
protected override RateLimitLease AttemptAcquireCore(int permitCount)
Parameters
permitCount
intNumber of permits to try and acquire.
Returns
Dispose(bool)
Dispose method for implementations to write.
protected override void Dispose(bool disposing)
Parameters
disposing
bool
GetStatistics()
Gets a snapshot of the RateLimiter statistics if available.
public override RateLimiterStatistics? GetStatistics()
Returns
- RateLimiterStatistics
An instance of RateLimiterStatistics containing a snapshot of the RateLimiter statistics.