Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: different api rate limits for different paths

I need to setup different rate limits for different paths. Foe example:

On path /users I want to have rate limit of 60 requests per minute, while for path /stats I want to have rate limit of only 5 requests per minute.

I tried with next approach

Route::group(['middleware' => ['auth', 'throttle:60']], function(){
   Route::get('users', 'User@list');
});
Route::group(['middleware' => ['auth', 'throttle:5']], function(){
   Route::get('stats', 'User@stats');
});

Somehow, last rate limit is applied. However, when making requests on users path, X-Rate-Limit-Limit header is set to 60, but it throws "Too many requests" error when it reaches 6th request.

like image 901
Sufi Avatar asked Apr 29 '17 16:04

Sufi


People also ask

How do I change my API rate limit?

Click Edit Rate Limit. Select Unimited to allow unrestricted calls to the API, or select Custom Rate Limit to define the required rate limit; use the supplied fields to define the maximum number of calls allowed in a specified time period; for example, 100 calls per 1 minute. Click Submit when done.

What is Laravel API rate limit?

Laravel API rate limiting 100 requests per minute.

How does Laravel rate limit work?

Laravel's rate limiting middleware stores the client's IP address alongside with the amount of requests in a given time period and performs a check on every request.


1 Answers

You may want to try commenting out the default rate on line 40 of the Kernel.php since you are specifying it in each middleware group to avoid conflict.

You may also want to change the middleware to include the second parameter of how long the waiting period is until the next request can come in. (e.g. throttle:60,1)

like image 140
jeremykenedy Avatar answered Oct 22 '22 00:10

jeremykenedy