Last active
November 13, 2022 06:33
-
-
Save mitchdenny/786505969ae8c984e54ac7c21353dc0b to your computer and use it in GitHub Desktop.
PartitionedRateLimiter does not honour AutoReplenishment = false
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.RateLimiting; | |
var concurrency = 1; | |
var partitionedRateLimiter = PartitionedRateLimiter.Create<Guid, Guid>((r) => | |
{ | |
return RateLimitPartition.Get(r, (key) => | |
{ | |
var rateLimiter = new TokenBucketRateLimiter(new() | |
{ | |
AutoReplenishment = false, | |
QueueLimit = 0, | |
TokenLimit = 10, | |
TokensPerPeriod = 1, | |
ReplenishmentPeriod = TimeSpan.FromSeconds(7) | |
}); | |
return rateLimiter; | |
}); | |
}); | |
var tasks = new List<Task>(); | |
for (var taskIndex = 0; taskIndex < concurrency; taskIndex++) | |
{ | |
Task.Run(async () => | |
{ | |
var taskId = Guid.NewGuid(); | |
var iterationCount = 0; | |
var timer = new PeriodicTimer(TimeSpan.FromSeconds(1)); | |
while (await timer.WaitForNextTickAsync()) | |
{ | |
using var permit = await partitionedRateLimiter.AcquireAsync(taskId); | |
Console.WriteLine($"{taskId} ({iterationCount++}) > Permit acquired? {permit.IsAcquired}"); | |
} | |
}); | |
} | |
Console.WriteLine("Press ENTER to terminate"); | |
Console.ReadLine(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment