How to throttle requests in a Web Api?

后端 未结 7 487
深忆病人
深忆病人 2020-11-30 17:45

I\'m trying to implement request throttling via the following:

Best way to implement request throttling in ASP.NET MVC?

I\'ve pulled that code into my solut

相关标签:
7条回答
  • 2020-11-30 18:45

    WebApiThrottle is quite the champ now in this area.

    It's super easy to integrate. Just add the following to App_Start\WebApiConfig.cs:

    config.MessageHandlers.Add(new ThrottlingHandler()
    {
        // Generic rate limit applied to ALL APIs
        Policy = new ThrottlePolicy(perSecond: 1, perMinute: 20, perHour: 200)
        {
            IpThrottling = true,
            ClientThrottling = true,
            EndpointThrottling = true,
            EndpointRules = new Dictionary<string, RateLimits>
            { 
                 //Fine tune throttling per specific API here
                { "api/search", new RateLimits { PerSecond = 10, PerMinute = 100, PerHour = 1000 } }
            }
        },
        Repository = new CacheRepository()
    });
    

    It's available as a nuget too with the same name.

    0 讨论(0)
提交回复
热议问题