diff --git a/src/RedisRateLimiting/Concurrency/RedisConcurrencyRateLimiter.cs b/src/RedisRateLimiting/Concurrency/RedisConcurrencyRateLimiter.cs index e2b162c..20871d8 100644 --- a/src/RedisRateLimiting/Concurrency/RedisConcurrencyRateLimiter.cs +++ b/src/RedisRateLimiting/Concurrency/RedisConcurrencyRateLimiter.cs @@ -76,27 +76,7 @@ protected override ValueTask AcquireAsyncCore(int permitCount, C protected override RateLimitLease AttemptAcquireCore(int permitCount) { - if (permitCount > _options.PermitLimit) - { - throw new ArgumentOutOfRangeException(nameof(permitCount), permitCount, string.Format("{0} permit(s) exceeds the permit limit of {1}.", permitCount, _options.PermitLimit)); - } - - var leaseContext = new ConcurencyLeaseContext - { - Limit = _options.PermitLimit, - RequestId = Guid.NewGuid().ToString(), - }; - - var response = _redisManager.TryAcquireLease(leaseContext.RequestId); - - leaseContext.Count = response.Count; - - if (response.Allowed) - { - return new ConcurrencyLease(isAcquired: true, this, leaseContext); - } - - return new ConcurrencyLease(isAcquired: false, this, leaseContext); + return FailedLease; } private async ValueTask AcquireAsyncCoreInternal(CancellationToken cancellationToken) @@ -148,7 +128,7 @@ private void Release(ConcurencyLeaseContext leaseContext) { if (leaseContext.RequestId is null) return; - _redisManager.ReleaseLease(leaseContext.RequestId); + _ = _redisManager.ReleaseLeaseAsync(leaseContext.RequestId); } private async Task StartDequeueTimerAsync(PeriodicTimer periodicTimer) diff --git a/src/RedisRateLimiting/FixedWindow/RedisFixedWindowRateLimiter.cs b/src/RedisRateLimiting/FixedWindow/RedisFixedWindowRateLimiter.cs index 45734b6..2c1cdcf 100644 --- a/src/RedisRateLimiting/FixedWindow/RedisFixedWindowRateLimiter.cs +++ b/src/RedisRateLimiting/FixedWindow/RedisFixedWindowRateLimiter.cs @@ -12,6 +12,8 @@ public class RedisFixedWindowRateLimiter : RateLimiter private readonly RedisFixedWindowManager _redisManager; private readonly RedisFixedWindowRateLimiterOptions _options; + private readonly FixedWindowLease FailedLease = new(isAcquired: false, null); + public override TimeSpan? IdleDuration => TimeSpan.Zero; public RedisFixedWindowRateLimiter(TKey partitionKey, RedisFixedWindowRateLimiterOptions options) @@ -60,29 +62,7 @@ protected override ValueTask AcquireAsyncCore(int permitCount, C protected override RateLimitLease AttemptAcquireCore(int permitCount) { - if (permitCount > _options.PermitLimit) - { - throw new ArgumentOutOfRangeException(nameof(permitCount), permitCount, string.Format("{0} permit(s) exceeds the permit limit of {1}.", permitCount, _options.PermitLimit)); - } - - var leaseContext = new FixedWindowLeaseContext - { - Limit = _options.PermitLimit, - Window = _options.Window, - }; - - var response = _redisManager.TryAcquireLease(); - - leaseContext.Count = response.Count; - leaseContext.RetryAfter = response.RetryAfter; - leaseContext.ExpiresAt = DateTimeOffset.FromUnixTimeSeconds(response.ExpiresAt); - - if (leaseContext.Count > _options.PermitLimit) - { - return new FixedWindowLease(isAcquired: false, leaseContext); - } - - return new FixedWindowLease(isAcquired: true, leaseContext); + return FailedLease; } private async ValueTask AcquireAsyncCoreInternal() diff --git a/src/RedisRateLimiting/SlidingWindow/RedisSlidingWindowRateLimiter.cs b/src/RedisRateLimiting/SlidingWindow/RedisSlidingWindowRateLimiter.cs index 5d3eae3..8bbd4b6 100644 --- a/src/RedisRateLimiting/SlidingWindow/RedisSlidingWindowRateLimiter.cs +++ b/src/RedisRateLimiting/SlidingWindow/RedisSlidingWindowRateLimiter.cs @@ -11,7 +11,9 @@ public class RedisSlidingWindowRateLimiter : RateLimiter { private readonly RedisSlidingWindowManager _redisManager; private readonly RedisSlidingWindowRateLimiterOptions _options; - + + private readonly SlidingWindowLease FailedLease = new(isAcquired: false, null); + public override TimeSpan? IdleDuration => TimeSpan.Zero; public RedisSlidingWindowRateLimiter(TKey partitionKey, RedisSlidingWindowRateLimiterOptions options) @@ -60,29 +62,7 @@ protected override ValueTask AcquireAsyncCore(int permitCount, C protected override RateLimitLease AttemptAcquireCore(int permitCount) { - if (permitCount > _options.PermitLimit) - { - throw new ArgumentOutOfRangeException(nameof(permitCount), permitCount, string.Format("{0} permit(s) exceeds the permit limit of {1}.", permitCount, _options.PermitLimit)); - } - - var leaseContext = new SlidingWindowLeaseContext - { - Limit = _options.PermitLimit, - Window = _options.Window, - RequestId = Guid.NewGuid().ToString(), - }; - - var response = _redisManager.TryAcquireLease(leaseContext.RequestId); - - leaseContext.Count = response.Count; - leaseContext.Allowed = response.Allowed; - - if (leaseContext.Allowed) - { - return new SlidingWindowLease(isAcquired: true, leaseContext); - } - - return new SlidingWindowLease(isAcquired: false, leaseContext); + return FailedLease; } private async ValueTask AcquireAsyncCoreInternal() diff --git a/src/RedisRateLimiting/TokenBucket/RedisTokenBucketRateLimiter.cs b/src/RedisRateLimiting/TokenBucket/RedisTokenBucketRateLimiter.cs index d3341f1..42ae207 100644 --- a/src/RedisRateLimiting/TokenBucket/RedisTokenBucketRateLimiter.cs +++ b/src/RedisRateLimiting/TokenBucket/RedisTokenBucketRateLimiter.cs @@ -12,6 +12,8 @@ public class RedisTokenBucketRateLimiter : RateLimiter private readonly RedisTokenBucketManager _redisManager; private readonly RedisTokenBucketRateLimiterOptions _options; + private readonly TokenBucketLease FailedLease = new(isAcquired: false, null); + public override TimeSpan? IdleDuration => TimeSpan.Zero; public RedisTokenBucketRateLimiter(TKey partitionKey, RedisTokenBucketRateLimiterOptions options) @@ -65,28 +67,7 @@ protected override ValueTask AcquireAsyncCore(int permitCount, C protected override RateLimitLease AttemptAcquireCore(int permitCount) { - if (permitCount > _options.TokenLimit) - { - throw new ArgumentOutOfRangeException(nameof(permitCount), permitCount, string.Format("{0} permit(s) exceeds the permit limit of {1}.", permitCount, _options.TokenLimit)); - } - - var leaseContext = new TokenBucketLeaseContext - { - Limit = _options.TokenLimit, - }; - - var response = _redisManager.TryAcquireLease(); - - leaseContext.Allowed = response.Allowed; - leaseContext.Count = response.Count; - leaseContext.RetryAfter = response.RetryAfter; - - if (leaseContext.Allowed) - { - return new TokenBucketLease(isAcquired: true, leaseContext); - } - - return new TokenBucketLease(isAcquired: false, leaseContext); + return FailedLease; } private async ValueTask AcquireAsyncCoreInternal()