Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stanislav Pakhomov #273

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Stas13199505
Copy link

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:

  1. Identifiers – Represent the entities whose rate of requests is being limited (e.g., IP address, token).
  2. Rules – Define the conditions under which requests are allowed or denied, including FixedWindow, Timespan, and logical combinations like And and Or.
  3. History – Keeps track of requests and allows checking whether a request exceeds the limits within a specific timeframe.

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
var history = new InMemoryFixedWindowHistory();
var fixedWindowRule = new FixedWindow(history, maxCount: 5, window: 10); // Max 5 requests in 10 seconds

var ipAddress = new IpAddress("192.168.1.1");

for (int i = 0; i < 6; i++)
{
    bool isAllowed = fixedWindowRule.Check(ipAddress);
    Console.WriteLine(isAllowed ? "Request allowed" : "Request denied");

    // Simulate a delay between requests
    Task.Delay(1000).Wait(); // 1 second delay
}

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
var history = new InMemoryTimespanHistory();
var timespanRule = new Timespan(history, timespan: 5); // Allow one request every 5 seconds

var token = new Token("abcdef12345");

for (int i = 0; i < 6; i++)
{
    bool isAllowed = timespanRule.Check(token);
    Console.WriteLine(isAllowed ? "Request allowed" : "Request denied");

    // Simulate a delay between requests
    Task.Delay(2000).Wait(); // 2 second delay
}

Logical Rules (And, Or)

You can combine multiple rules using And (all rules must pass) or Or (any rule must pass).

And Rule

This rule checks if all the given rules pass.

Usage Example: And Rule
var fixedWindowHistory = new InMemoryFixedWindowHistory();
var timespanHistory = new InMemoryTimespanHistory();

var fixedWindowRule = new FixedWindow(fixedWindowHistory, maxCount: 3, window: 10);
var timespanRule = new Timespan(timespanHistory, timespan: 5);

var andRule = new And(new IRule[] { fixedWindowRule, timespanRule });

var ipAddress = new IpAddress("192.168.1.1");

for (int i = 0; i < 5; i++)
{
    bool isAllowed = andRule.Check(ipAddress);
    Console.WriteLine(isAllowed ? "Request allowed" : "Request denied");

    // Simulate a delay between requests
    Task.Delay(1000).Wait(); // 1 second delay
}

Or Rule

This rule checks if any of the given rules pass.

Usage Example: Or Rule
var fixedWindowHistory = new InMemoryFixedWindowHistory();
var timespanHistory = new InMemoryTimespanHistory();

var fixedWindowRule = new FixedWindow(fixedWindowHistory, maxCount: 2, window: 5); // Max 2 requests in 5 seconds
var timespanRule = new Timespan(timespanHistory, timespan: 10); // Allow one request every 10 seconds

var orRule = new Or(new IRule[] { fixedWindowRule, timespanRule });

var ipAddress = new IpAddress("192.168.1.1");

for (int i = 0; i < 5; i++)
{
    bool isAllowed = orRule.Check(ipAddress);
    Console.WriteLine(isAllowed ? "Request allowed" : "Request denied");

    // Simulate a delay between requests
    Task.Delay(1000).Wait(); // 1 second delay
}

Conclusion

The RateLimiter library is a powerful tool for controlling request rates based on different conditions. By combining various rules (such as FixedWindow, Timespan, and logical combinations like And and Or), 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant