Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 - Override API 'throttle:60,1'

Tags:

I'm writing a lot of API to get and store data.
I like the default throttle option:

protected $middlewareGroups = [     'api' => [         'throttle:60,1',         'bindings',     ], ]; 

to limit the request to 60 per minute; but for some route (es: POST), I'd like to increase this value.

I tried to set 'throttle:500,1' on route Middleware like below:

Route::group(function () {         Route::get('semaphore/1',        ['uses' => 'App\Api\V1\DBs\SemaphoreController@index']);         Route::post('semaphore/1',       ['uses' => 'App\Api\V1\DBs\SemaphoreController@store',        'middleware' => 'WriteToDatabaseMiddleware', 'throttle:500,1']); }); 

but it does not work.

Any idea?

Thank you.

UPDATE:
I noticed that the 'throttle:500,1' used in the api.php route will be set AFTER the default 'throttle:60,1' specified into Kernel.php file; then, It doesn't work.

Logging the process execution, the first call is:

Illuminate\Routing\Middleware\ThrottleRequests -> handle 

from Kernel.php has maxAttempts=60.

Then, the second call is:

Illuminate\Routing\Middleware\ThrottleRequests -> handle 

from api.php has maxAttempts=500.

In others words, the throttle:500,1 in the api.php file do not override the throttle:60,1 in the Kernel.php file.

like image 583
vlauciani Avatar asked May 26 '17 12:05

vlauciani


People also ask

How do I disable rate limiting in Laravel?

A: For disabling the rate limiter in Laravel, first go to the app/Http/Kernel. php. There you will find the default throttle limit defined by Laravel for all api routes. Just comment out that code to disable it completely.

How do I fix too many requests from Laravel?

Open routes/web. php file and add groups around the routes which you want to limit. That's it now every request for these routes will limited to 10 requests per ip address.

What is the default rate limiter value in Laravel?

Laravel has a fixed decay rate limiter. With default settings of throttle:60,1 this means that a client could make 60 requests with 1 minute of decay before hitting a 1 minute forced decay timeout. The client could make 60 requests in 1 second or distributed over 60 seconds at a rate of 1 request per second ( 1 r/s ).

What is Ratelimiter in Laravel?

Laravel includes a simple to use rate limiting abstraction which, in conjunction with your application's cache, provides an easy way to limit any action during a specified window of time. If you are interested in rate limiting incoming HTTP requests, please consult the rate limiter middleware documentation.


2 Answers

Current answer

According to this GitHub issue, the throttle middleware should not be used "twice" (like you want to do that). There are only two ways how to deal with your current problem "correctly":

  1. Write an own throttling middleware

or

  1. Define the throttle middleware separately for each route (group)

Old answer

You set the middleware key wrong! When declaring multiple middleware to use, create a new array for them

['middleware' => ['WriteToDatabaseMiddleware','throttle:500,1']] 

EDIT: Because of the middleware order, you should set your kernel throttle to the highest value you want to use, and all other routes that should have a lower throttle value to the corresponding ones.

like image 199
manniL Avatar answered Sep 22 '22 14:09

manniL


In laravel 6 you can use prefix for prevent with global throttle. use 'throttle:5,1,prefix'

Route::group(['prefix' => 'contact-us', 'middleware' => 'throttle:5,1,contact-form',], function () {     Route::post('/', 'ContactUsController@store'); }); 

Allow multiple throttles by naming

like image 26
Ali Yousefi Avatar answered Sep 24 '22 14:09

Ali Yousefi