Limiter
Another integration pattern is limiting the number of calls that can be made during a given period of time. This is important when rate limiting or billing may be an issue and is a perfect stop gap for a critical failure causing an erroneous number of API calls or running up an unwanted bill!
The Limiter auto resets it's count on the defined TimeSpan
, and will not allow additional executions past the defined limit.
using Perigee;
using Perigee.Extensions;
PerigeeApplication.ApplicationNoInit("Limiter", (c) =>
{
c.AddRecurring("Limiter", (ct, l) => {
using var APILimit = new RateLimiter("RateLimiter.json", 2, TimeSpan.FromMinutes(1), ct);
if (APILimit.CanExecute())
{
//Perform action
l.LogInformation("Calling API {n}/{x} executions", APILimit.GetCount(), APILimit.GetLimit());
}
else
{
l.LogInformation("Out of executions...");
}
});
});
/*
[16:35:07 INF] Calling API 1/2 executions
[16:35:13 INF] Calling API 2/2 executions
[16:35:18 INF] Out of executions...
*/
Last updated