| | | 1 | | namespace Intentum.Runtime.RateLimiting; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Options for rate limit check (key, limit, window). |
| | | 5 | | /// </summary> |
| | | 6 | | public sealed record RateLimitOptions(string Key, int Limit, TimeSpan Window); |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Result of a rate limit check. |
| | | 10 | | /// </summary> |
| | 19 | 11 | | public sealed record RateLimitResult( |
| | 10 | 12 | | bool Allowed, |
| | 8 | 13 | | int CurrentCount, |
| | 1 | 14 | | int Limit, |
| | 1 | 15 | | TimeSpan? RetryAfter = null |
| | 19 | 16 | | ); |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Interface for rate limiting. Used when policy decision is <see cref="Policy.PolicyDecision.RateLimit"/>. |
| | | 20 | | /// </summary> |
| | | 21 | | public interface IRateLimiter |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Tries to acquire a slot for the given key within the limit for the time window. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="key">Scope key (e.g. user id, session id, IP).</param> |
| | | 27 | | /// <param name="limit">Maximum number of requests allowed in the window.</param> |
| | | 28 | | /// <param name="window">Time window (e.g. 1 minute).</param> |
| | | 29 | | /// <param name="cancellationToken">Cancellation token.</param> |
| | | 30 | | /// <returns>Result indicating whether the request is allowed and optional retry-after.</returns> |
| | | 31 | | ValueTask<RateLimitResult> TryAcquireAsync( |
| | | 32 | | string key, |
| | | 33 | | int limit, |
| | | 34 | | TimeSpan window, |
| | | 35 | | CancellationToken cancellationToken = default); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Resets the counter for the given key (e.g. after successful auth or admin override). |
| | | 39 | | /// </summary> |
| | | 40 | | void Reset(string key); |
| | | 41 | | } |