Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Here's an extended version of the documentation with usage examples for all the rules (FixedWindow, Timespan, And, and Or):
Rate Limiter Library Documentation
Overview
The
RateLimiter
library provides a flexible and extensible way to control the rate of requests for specific identifiers, such as IP addresses or tokens. The library supports several rules for rate limiting, including fixed windows, timespan-based limits, and composite rules (AND, OR). This allows for the application of complex rate-limiting strategies to prevent abuse and ensure fairness.The library consists of several components:
FixedWindow
,Timespan
, and logical combinations likeAnd
andOr
.Key Components
1. Identifiers
An identifier is an entity whose rate is being limited. For example, you can limit the number of requests per IP address or token.
2. Rules
Rules define the conditions for limiting requests.
Fixed Window Rule (
FixedWindow
)This rule limits the number of requests that can be made within a fixed window of time (e.g., 10 requests per 60 seconds).
Usage Example: Fixed Window Rule
Timespan Rule (
Timespan
)This rule checks the time passed since the last request for an identifier. It allows a request only if a specified timespan has passed since the last request.
Usage Example: Timespan Rule
Logical Rules (
And
,Or
)You can combine multiple rules using
And
(all rules must pass) orOr
(any rule must pass).And
RuleThis rule checks if all the given rules pass.
Usage Example:
And
RuleOr
RuleThis rule checks if any of the given rules pass.
Usage Example:
Or
RuleConclusion
The
RateLimiter
library is a powerful tool for controlling request rates based on different conditions. By combining various rules (such asFixedWindow
,Timespan
, and logical combinations likeAnd
andOr
), you can build sophisticated rate-limiting strategies tailored to your application’s needs.This flexibility makes it easy to protect your application from abuse while ensuring fair access for legitimate users.