using CryptoExchange.Net.Objects; using Microsoft.Extensions.Logging; using System; using System.Threading; using System.Threading.Tasks; namespace CryptoExchange.Net.RateLimiting.Interfaces { /// /// Rate limit gate /// public interface IRateLimitGate { /// /// Event when the rate limit is triggered /// event Action RateLimitTriggered; /// /// Event when the rate limit is updated. Note that it's only updated when a request is send, so there are no specific updates when the current usage is decaying. /// event Action? RateLimitUpdated; /// /// Add a rate limit guard /// /// Guard to add /// IRateLimitGate AddGuard(IRateLimitGuard guard); /// /// Set a RetryAfter guard, can be used when a server rate limit is hit and a RetryAfter header is specified /// /// The time after which requests can be send again /// RateLimitType /// Task SetRetryAfterGuardAsync(DateTime retryAfter, RateLimitItemType type = RateLimitItemType.Request); /// /// Returns the 'retry after' timestamp if set /// /// Task GetRetryAfterTime(); /// /// Process a request. Enforces the configured rate limits. When a rate limit is hit will wait for the rate limit to pass if RateLimitingBehaviour is Wait, or return an error if it is set to Fail /// /// Logger /// Id of the item to check /// The rate limit item type /// The request definition /// The API key /// Request weight /// Behaviour when rate limit is hit /// An additional optional suffix for the key selector. Can be used to make rate limiting work based on parameters. /// Cancelation token /// Error if RateLimitingBehaviour is Fail and rate limit is hit ValueTask ProcessAsync(ILogger logger, int itemId, RateLimitItemType type, RequestDefinition definition, string? apiKey, int requestWeight, RateLimitingBehaviour behaviour, string? keySuffix, CancellationToken ct); /// /// Enforces the rate limit as defined in the request definition. When a rate limit is hit will wait for the rate limit to pass if RateLimitingBehaviour is Wait, or return an error if it is set to Fail /// /// Logger /// Id of the item to check /// The guard /// The rate limit item type /// The request definition /// The API key /// Behaviour when rate limit is hit /// The weight to apply to the limit guard /// An additional optional suffix for the key selector. Can be used to make rate limiting work based on parameters. /// Cancelation token /// Error if RateLimitingBehaviour is Fail and rate limit is hit ValueTask ProcessSingleAsync(ILogger logger, int itemId, IRateLimitGuard guard, RateLimitItemType type, RequestDefinition definition, string? apiKey, int requestWeight, RateLimitingBehaviour behaviour, string? keySuffix, CancellationToken ct); /// /// Reset the limit for the specified parameters /// /// The rate limit item type /// The request definition /// The API key /// An additional optional suffix for the key selector. Can be used to make rate limiting work based on parameters. /// Amount in weight to reset by, or null to set used rate limit to 0 /// Cancelation token Task ResetAsync( RateLimitItemType type, RequestDefinition definition, string? apiKey, string? keySuffix, int? amount, CancellationToken ct); } }