ASP. NET Web API control Request Frequency

Source: Internet
Author: User

Reference Address: http://www.cnblogs.com/shanyou/p/3194802.html

Installing the NuGet package: Webapicontrib

Many APIs, such as the GitHub ' s API, have traffic control practices. Use rate limiting to prevent the client from making too many requests to your API in a short amount of time. For example, we can limit the anonymous API client to a maximum of 60 requests per hour, and we can make more requests from more authenticated clients. So how does ASP. NET Webapi implement such a function? There is an implementation above the project Webapicontrib: https://github.com/WebApiContrib/WebAPIContrib/blob/master/src/WebApiContrib/ Messagehandlers/throttlinghandler.cs, with good scalability.

The simplest approach is to use Throttlinghandler to register with simple parameters, such as controlling 60 requests per user every hour:

Config. Messagehandlers.add ( new    Throttlinghandler,new  Inmemorythrottlestore (),            timespan.fromhours (1)));
  Ithrottlestore  interface uses ID + current number of requests. Inmemorythrottlestore has only one in-memory storage, but you can easily extend the implementation as a distributed cache or database. You can also easily customize the behavior of the Throttlinghandler, for example, we have better control over an IP address. 
 CONFIG.               Messagehandlers.add (new   Throttlinghandler (  new   Inmemorythrottlestore (), ID /span>=> if  ( id = =  10.0.0.1   "   return  5000                    return  60  ; }, Timespan.fromhours ( 1 ))); 

ASP. NET Web API control Request Frequency

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.