Skip to content

Instantly share code, notes, and snippets.

@mitchdenny
Last active November 13, 2022 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchdenny/786505969ae8c984e54ac7c21353dc0b to your computer and use it in GitHub Desktop.
Save mitchdenny/786505969ae8c984e54ac7c21353dc0b to your computer and use it in GitHub Desktop.
PartitionedRateLimiter does not honour AutoReplenishment = false
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