Laravel: different api rate limits for different paths

旧时模样 提交于 2020-01-13 09:44:08

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/43698098/laravel-different-api-rate-limits-for-different-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!