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

RateLimiter #121

Open
Tracked by #82
WeihanLi opened this issue Jul 28, 2021 · 2 comments
Open
Tracked by #82

RateLimiter #121

WeihanLi opened this issue Jul 28, 2021 · 2 comments
Labels

Comments

@WeihanLi
Copy link
Owner

No description provided.

@WeihanLi WeihanLi mentioned this issue Jul 28, 2021
11 tasks
@stale stale bot added the inactive label Mar 13, 2022
@stale stale bot removed the inactive label Mar 14, 2022
Repository owner deleted a comment from stale bot Oct 25, 2022
@EdiWang
Copy link

EdiWang commented Jan 22, 2024

I would hope to have something similar to https://github.com/stefanprodan/AspNetCoreRateLimit, that package was not under active development since .NET got it's own rate limiting middleware. However, their new implementation requires Redis, which would be a over-kill for a simple single server application.

@WeihanLi
Copy link
Owner Author

WeihanLi commented Nov 4, 2024

API Proposal:

public interface IRateLimiter
{
    Task<RateLimitLease> AcquireAsync(int permitCount = 1, CancellationToken cancellationToken = default);
}

public class RateLimiterOptions
{
    public required int Limit { get; set; }
}

internal abstract class RateLimiter<TOptions>
  where TOptions : RateLimiterOptions
{
    protected RateLimiter(TOptions options)
    {
        Options = options;
    }
    protected TOptions Options { get; }
}

public interface RateLimitLease
{
    bool IsAcquired { get; }
    Timespan? RetryAfter { get; }
    bool TryGetMetadata<T>(string metadataName, [MaybeNull] out T metadata);
}

public class RateLimitLease : RateLimitLease
{
    bool IsAcquired { get; }
    Timespan? RetryAfter { get; }
    bool TryGetMetadata<T>(string metadataName, [MaybeNull] out T metadata)
}

public class ConcurrencyRateLimiter : IRateLimiter
{
}

public class ConcurrencyRateLimiterOptions : RateLimiterOptions {}

public class TokenBucketRateLimiter : IRateLimiter
{
}

public class TokenBucketRateLimiterOptions : RateLimiterOptions
{
    public TimeSpan FillPeriod { get; set; }
    public int TokensPerPeriod { get; set; }
    public bool AutoFill { get; set; }
    public int QueueLimit { get; set; }
}

public class FixedWindowsRateLimiter : IRateLimiter
{
    
}

public class SlidingWindowsRateLimiter : IRateLimiter
{
}

possible Redis-based implementation

public class RedisConcurrencyRateLimiter : ConcurrencyRateLimiter
{
}

public class RedisTokenBucketRateLimiter : TokenBucketRateLimiter
{
}

// public required TimeSpan TimeWindow { get; init; }

public class RedisFixedWindowsRateLimiter : FixedWindowsRateLimiter
{
}

public class RedisSlidingWindowsRateLimiter : SlidingIRateLimiter
{
}

Usage example:

var rateLimiter = new TokenBucketRateLimiter(new TokenBucketRateLimterOptions
{
    Limit = 10    
});

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

No branches or pull requests

2 participants