stefanprodan / WebApiThrottle

ASP.NET Web API rate limiter for IIS and Owin hosting
MIT License
1.28k stars 274 forks source link

Get policy object from cache is always null #127

Open vickyRathee opened 5 years ago

vickyRathee commented 5 years ago

The var policy = policyRepository.FirstOrDefault(ThrottleManager.GetPolicyKey()); is always null

Here is my code :

//Rate Limits

 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MessageHandlers.Add(new MyThrottlingHandler()
            {
                Policy = new ThrottlePolicy(perMinute: 2)
                {
                    ClientThrottling = true,
                    ClientRules = new Dictionary<string, RateLimits>
                    {
                        { "testkey", new RateLimits { PerMinute = 5 } },
                    },
                    ClientWhitelist = new List<string> { "anon", "anotherkeyhere" }
                },
                Repository = new CacheRepository(),
                PolicyRepository = new PolicyCacheRepository()
            });
}
}
public class MyThrottlingHandler: ThrottlingHandler
        {
            protected override RequestIdentity SetIdentity(HttpRequestMessage request)
            {
                string apiKey = Utility.ExtractApiKey(request);

                return new RequestIdentity()
                {                    
                    ClientKey = string.IsNullOrEmpty(apiKey) ? "anon" : apiKey,
                    ClientIp = GetClientIp(request).ToString(),
                    Endpoint = request.RequestUri.AbsolutePath.ToLowerInvariant()
                };
            }
        }

On the controller, I am simply checking if the Key is PAID customer update the rate limit to higher.

public static void UpdateRateLimits(string apiKey, RateLimits rateLimits)
        {
            //init policy repo
            var policyRepository = new PolicyCacheRepository();

            //get policy object from cache
            var policy = policyRepository.FirstOrDefault(ThrottleManager.GetPolicyKey());
            **// Error here - policy is always null**   

            if (policy.ClientRules.ContainsKey(apiKey))
            {
                //update client rate limits
                policy.ClientRules[apiKey] = rateLimits;
            }
            else
            {   
                //add new client rate limits
                policy.ClientRules.Add(apiKey, rateLimits);
            }
            //apply policy updates
            ThrottleManager.UpdatePolicy(policy, policyRepository);
        }
djmitchella commented 5 years ago

I just hit this -- you have to use the new constructor as in that bit of the docs, with code like:

config.MessageHandlers.Add(new CustomThrottlingHandler(
    policy: new ThrottlePolicy( /*perSecond: 10,...*/ )
    {
        IpThrottling = true,
        // ...
    },
    repository: new CacheRepository(),
    policyRepository: new PolicyCacheRepository(),
    // ...
));

rather than

config.MessageHandlers.Add(new CustomThrottlingHandler() 
{ 
    Policy = ..., 
    Repository = ...
 }