Description
I was exploring the recently-announced System.Threading.Ratelimiting APIs, when I noticed some peculiar behavior that I haven't noticed with other rate limiting frameworks.
When rate-limiting a function/request, it stands to reason that the average time taken for a batch of requests should approximate the rate limit.
When I set a rate limit with the intent of permitting one request per second, I notice that the rate is approximately 1.5 seconds per request. If I try to permit one request per half second, I notice that the rate is approximately 0.75 seconds per request.
Reproduction Steps
using System.Threading.RateLimiting;
RateLimiter limiter = new FixedWindowRateLimiter(
new FixedWindowRateLimiterOptions(permitLimit: 1, queueProcessingOrder: QueueProcessingOrder.OldestFirst, queueLimit: 100, window: TimeSpan.FromSeconds(1), autoReplenishment: true)
);
async Task printLimited(int message) {
await limiter.WaitAsync(1);
Console.WriteLine(message);
}
int[] nums = new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
DateTime start = System.DateTime.Now;
var tasks = nums.Select(x => printLimited(x));
await Task.WhenAll(tasks);
DateTime end = System.DateTime.Now;
TimeSpan delta = end - start;
Console.WriteLine("Time elapsed: " + delta.TotalSeconds.ToString());
Expected behavior
The above should complete in approximately n seconds, where n is the number of elements in nums.
Actual behavior
The above completes in approximately 1.5n seconds, where n is the number of elements in nums.
Regression?
No response
Known Workarounds
No response
Configuration
Dotnet 6.0.200
MacOS Monterey 12.5
ARM64
Other information
No response
Description
I was exploring the recently-announced
System.Threading.RatelimitingAPIs, when I noticed some peculiar behavior that I haven't noticed with other rate limiting frameworks.When rate-limiting a function/request, it stands to reason that the average time taken for a batch of requests should approximate the rate limit.
When I set a rate limit with the intent of permitting one request per second, I notice that the rate is approximately 1.5 seconds per request. If I try to permit one request per half second, I notice that the rate is approximately 0.75 seconds per request.
Reproduction Steps
Expected behavior
The above should complete in approximately
nseconds, wherenis the number of elements innums.Actual behavior
The above completes in approximately
1.5nseconds, wherenis the number of elements innums.Regression?
No response
Known Workarounds
No response
Configuration
Dotnet 6.0.200MacOS Monterey 12.5ARM64Other information
No response