-
Notifications
You must be signed in to change notification settings - Fork 86
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
Labels
Comments
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. |
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
No description provided.
The text was updated successfully, but these errors were encountered: