When it comes to open API to external users, we need to consider the number of requests we allowed. Most of API-as-a-service platforms have different payment plans. Due to different levels of payment, it can make sure that our service resource can be used fairly - advanced plan users can make more requests than normal users.
In order to achieve this goal, we need to find a way recording the number of requests. Intuitively, we can creata a
request_record table, before controller process the request, by comparing the number in database and client’s request limitation, we can either allow or deny the requests.
However, we don’t need to build like this way.
Thanks to Ruby gems, you are able to find some libraries can do the trick. In this article,
rack-throttle to help me count the requests. The reasons I prefer
Read on →
- To limit by defining a maximum number of allowed HTTP requests per a given time period
- Compatible with any Rack application and any Rack-based framework.
- Compatible with the memcached, memcache-client, memcache and redis gems.
- Well documented and the most important thing is that codes are easy to read!