Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rate-limiting a function call in rails per user

Anyone have any idea how I might go about this? Having a pretty hard time finding information online. Best I found is the curbit it gem but I can only think of how to implement that application-wise.

like image 910
Chris Bolton Avatar asked Jul 22 '11 01:07

Chris Bolton


People also ask

Where do you implement rate limiting?

Often rate-limiting is applied at a reverse proxy, API gateway, or load balancer before the request reaches the API, so that it can be applied to all requests arriving at a cluster of servers. By handling this at a proxy server, you also avoid excess load being generated on your application servers.

Why do we need to introduce API rate limiting?

Those limits were put in place to ensure public safety. APIs use a similar criterion, called a "rate limit," to ensure the safety of the API's consumers and the API itself. They can protect you against slow performance and denial-of-service (DoS) attacks, allow for scalability, and improve the overall user experience.


1 Answers

It can be handled by: 1) webserver 2) rack-application. All depend on what you need. We use built-in nginx functionality to limit API requests:

     limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;
     limit_req zone=one burst=2;

The another solution is rack-throttle.

This is Rack middleware that provides logic for rate-limiting incoming HTTP requests to Rack applications. You can use Rack::Throttle with any Ruby web framework based on Rack, including with Ruby on Rails 3.0 and with Sinatra.

like image 181
Anatoly Avatar answered Sep 24 '22 06:09

Anatoly