Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Get policy object from cache is always null #127

Open
vickyRathee opened this issue Feb 10, 2019 · 1 comment
Open

Get policy object from cache is always null #127

vickyRathee opened this issue Feb 10, 2019 · 1 comment

Comments

@vickyRathee
Copy link

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
Copy link

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 = ...
 }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants