php - Laravel 5.4 - 覆盖 API 'throttle:60,1'

标签 php laravel-5.4 throttling

我正在编写大量 API 来获取和存储数据。
我喜欢默认的 throttle选项:

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

将请求限制为每分钟 60 个;但是对于某些路线(es:POST),我想增加这个值。

我尝试设置 'throttle:500,1'在路由中间件如下:

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']);
});

但它不起作用。

有什么想法吗?

谢谢。

更新:
我注意到 'throttle:500,1'用于 api.php路由将在默认 'throttle:60,1' 之后设置指定为 Kernel.php文件;然后,它不起作用。

记录流程执行,第一个调用是:

Illuminate\Routing\Middleware\ThrottleRequests -> handle

来自 Kernel.phpmaxAttempts=60 .

那么,第二个调用是:

Illuminate\Routing\Middleware\ThrottleRequests -> handle

来自 api.phpmaxAttempts=500 .

换句话说,throttle:500,1api.php文件不要覆盖 throttle:60,1Kernel.php文件。

最佳答案

当前答案

根据this GitHub issue , throttle 中间件不应该被“两次”使用(就像你想那样做)。如何“正确”处理你现在的问题,只有两种方式:

  1. 编写自己的 throttle 中间件

  1. 为每个路由(组)单独定义 throttle 中间件

旧答案

您设置的中间件 key 错误!声明使用多个中间件时,为它们创建一个新数组

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

编辑:由于中间件顺序,您应该将内核限制设置为您要使用的最高值,并且所有其他路由都应该有一个降低 throttle 值到相应的值。

关于php - Laravel 5.4 - 覆盖 API 'throttle:60,1',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44202247/

相关文章:

php - 在类外访问 protected 成员变量

php - 如何使用函数连接到数据库以及如何使用查询?

php - 分组的 Laravel 路由会被缓存吗?它们缓存在哪里?

python - Bokeh 应用程序中的 throttle

node.js - 由于每秒上限而限制和排队 API 请求

php - 如何查看从单行表中爆炸检索的多个图像

php - 我的 MYSQL 数据库使用哪种搜索方法

php - Laravel 中如何聚合相关表字段的查询?

sql-server - SQLSTATE[IMSSP] : Tried to bind parameter number 2101. SQL Server 支持最多 2100 个参数

javascript - 如何防止默认浏览器滚动 touchmove 与 throttle 功能相结合