Update rate Limits at Runtime using Webapithrottle is not working

Asked

Viewed 55 times

1

I have a Webapi in ASP.NET MVC and I need to control the access limit, in addition, I need to change the limits values at runtime. I implemented as this example on the site Webapithrottle (section Update rate Limits at Runtime)

This is the code in my Webapiconfig:

//trace provider
var traceWriter = new SystemDiagnosticsTraceWriter()
{
    IsVerbose = true
};
config.Services.Replace(typeof(ITraceWriter), traceWriter);
config.EnableSystemDiagnosticsTracing();

//Web API throttling handler
config.MessageHandlers.Add(new ThrottlingHandler(
    policy: new ThrottlePolicy(perMinute: 3, perHour: 30, perDay: 35, perWeek: 3000)
    {
        //scope to IPs
        IpThrottling = true,
        //scope to clients
        ClientThrottling = true,
        ClientRules = new Dictionary<string, RateLimits>
        {
            { "client-key-1", new RateLimits { PerMinute = 1, PerHour = 60 } }
        },

        //scope to endpoints
        EndpointThrottling = true
    },

    //replace with PolicyMemoryCacheRepository for Owin self-host
    policyRepository: new PolicyCacheRepository(),

    //replace with MemoryCacheRepository for Owin self-host
    repository: new CacheRepository(),

    logger: new TracingThrottleLogger(traceWriter)));

I am setting three requests per minute as default and one request per minute for the customer with the "client-key-1" key. But when I test using Postman (I am passing the Authorization token with the client-key-1 value), I noticed that only the default configuration is being used because only after three requests I received the message:

inserir a descrição da imagem aqui

And even if I upgrade the rate limit using the function:

public void UpdateRateLimits()
{
    //init policy repo
    var policyRepository = new PolicyCacheRepository();

    //get policy object from cache
    var policy = policyRepository.FirstOrDefault(ThrottleManager.GetPolicyKey());

    //update client rate limits
    policy.ClientRules["client-key-1"] =
        new RateLimits { PerMinute = 20 };

    //apply policy updates
    ThrottleManager.UpdatePolicy(policy, policyRepository);

}

The message ""API calls quota exceeded! Maximum admitted 3 per Minute." continues to appear.

Someone’s had that problem before?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.