I'm trying to figure out if there is a better way to load client policies w/o loading all of them on startup. I can load all via IThrottlePolicyProvider, but I think as the number of clients grows it would be bit on the heavy side.
So far I've tried creating a middleware to read api key from Authorization-Token header and do a lookup on it to find the client and their rate limits stored in a database. I then store client's rate limits in OWIN context so it can be accessed later on.
I then implemented IPolicyRepository.FirstOrDefault which appears to be getting called for all requests. Here, I'm basically returning ThrottlePolicy with the rate limits for the current client in OWIN context only.
public ThrottlePolicy FirstOrDefault(string id)
{
var token = HttpContext.Current.GetOwinContext().Get<string>("client.AuthorizationToken");
var rateLimit = HttpContext.Current.GetOwinContext().Get<RateLimits>("client.RateLimits");
var throttlePolicy = new ThrottlePolicy(perSecond: 0, perMinute: 0, perHour: 0, perDay: 0, perWeek: 0)
{
ClientThrottling = true
};
if (rateLimit != null)
{
throttlePolicy.ClientRules.Add(token, rateLimit);
}
return throttlePolicy;
}
I'm not showing all of the code, but in general this works ok. Howevert, a little cumbersome and I feel like I may be missing a feature in the library. Any suggestions?
I too am interested in this. I'd like to have things data-driven so as to be able to change things dynamically without having to re-deploy. Any suggestions for best practice on this would be appreciated.
I'm trying to figure out if there is a better way to load client policies w/o loading all of them on startup. I can load all via
IThrottlePolicyProvider
, but I think as the number of clients grows it would be bit on the heavy side.So far I've tried creating a middleware to read api key from Authorization-Token header and do a lookup on it to find the client and their rate limits stored in a database. I then store client's rate limits in OWIN context so it can be accessed later on.
I then implemented IPolicyRepository.FirstOrDefault which appears to be getting called for all requests. Here, I'm basically returning ThrottlePolicy with the rate limits for the current client in OWIN context only.
I'm not showing all of the code, but in general this works ok. Howevert, a little cumbersome and I feel like I may be missing a feature in the library. Any suggestions?