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
ThrottlingRateLimiter
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
int
The maximum number of concurrent operations allowed.
throttlingPercentage
int?
The percentage of maxConcurrentCalls
at which throttling is triggered. Defaults to 50% if not specified.
The default throttling percentage, which defines the threshold for applying throttling, set to 50%.
public const int DefaultThrottlingPercentage = 50
Field Value
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
Method that RateLimiter implementations implement for AcquireAsync(int, CancellationToken).
protected override ValueTask<RateLimitLease> AcquireAsyncCore(int permitCount, CancellationToken cancellationToken)
Parameters
permitCount
int
Number of permits to try and acquire.
cancellationToken
CancellationToken
Optional token to allow canceling a queued request for permits.
A task that completes when the requested permits are acquired or when the requested permits are denied.
Method that RateLimiter implementations implement for AttemptAcquire(int).
protected override RateLimitLease AttemptAcquireCore(int permitCount)
Parameters
permitCount
int
Number of permits to try and acquire.
Dispose method for implementations to write.
protected override void Dispose(bool disposing)
Parameters
disposing
bool
Gets a snapshot of the RateLimiter statistics if available.
public override RateLimiterStatistics? GetStatistics()
Returns
An instance of RateLimiterStatistics containing a snapshot of the RateLimiter statistics.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4