Appearance
Throttle Class
Definition
Provides a way to limit the rate of execution of some code by specifying a maximum number of requests within a time span.
- Namespace:
JASE - Assembly:
JASE.Concurrency.dll
Constructors
| Name | Description |
|---|---|
Throttle(int maxRequests, TimeSpan timeSpan) | Initializes a new instance of the Throttle class with the specified maximum requests and time span. |
Properties
| Name | Description |
|---|---|
ActiveCount | Gets the number of admissions currently within the active sliding window. |
MaxRequests | Gets the maximum number of requests allowed within Window. |
Window | Gets the sliding time window for admitted requests. |
Methods
| Name | Description |
|---|---|
bool CanExecute() | Determines whether a new request can likely be executed without waiting. |
ValueTask ConfigureAsync(TimeSpan timeout, int maxRequests, TimeSpan timeSpan, CancellationToken cancellationToken = null) | Updates the admission limits, waiting at most timeout when a drain is required. |
ValueTask ConfigureAsync(int maxRequests, TimeSpan timeSpan, CancellationToken cancellationToken = null) | Updates the admission limits. When maxRequests or timeSpan is reduced, admissions are paused until the active window complies with the new limits. |
ValueTask DrainAsync(CancellationToken cancellationToken = null) | Asynchronously waits until all admitted requests have expired from the active window. |
ValueTask DrainAsync(TimeSpan timeout, CancellationToken cancellationToken = null) | Asynchronously waits until all admitted requests have expired from the active window or until timeout elapses. |
void EnqueueRequest() | Enqueues a new request, blocking until the number of requests is less than the specified maximum. |
ValueTask EnqueueRequestAsync(CancellationToken cancellationToken = null) | Enqueues a new request, asynchronously waiting until the number of requests is less than the specified maximum. |
ValueTask EnqueueRequestAsync(TimeSpan timeout, CancellationToken cancellationToken = null) | Enqueues a new request, asynchronously waiting until the number of requests is less than the specified maximum or until timeout elapses. |
ThrottleStats GetStats() | Returns a consistent snapshot of throttle configuration, window state, and cumulative counters. |
void Reset() | Clears all admitted requests from the active window without waiting for natural expiry. |