本站资源收集于互联网,不提供软件存储服务,每天免费更新优质的软件以及学习资源!

Laravel中间件`throttle:api`究竟是如何限制API请求频率的?

网络教程 app 1℃

Laravel中间件`throttle:api`究竟是如何限制API请求频率的

新手请教 laravel 中间件

在 laravel 路由配置文件中,我们经常看到如下配置:

‘api’ => [ // LaravelSanctumHttpMiddlewareEnsureFrontendRequestsAreStateful::class, ‘throttle:api’, IlluminateRoutingMiddlewareSubstituteBindings::class,],

其中 ‘throttle:api’, 引发了疑问。

‘throttle:api’ 的含义

‘throttle:api’ 是一个速率限制中间件,用于限制 api 请求的频率。后面的 api 参数是一个 ratelimiter 的名称。

ratelimiter

ratelimiter 是一个类,用于限制请求速率。laravel 默认提供了几个 ratelimiter 实例,其中一个是 api。

中间件参数格式

中间件参数以冒号开始,多个参数用逗号分隔。对于 ‘throttle:api’, 参数格式如下:

maxattempts: 允许的请求最大数量。默认值为 60。decayminutes: 限制窗口的分钟数。默认值为 1。limiter: ratelimiter 的名称。默认值为 api。

示例

‘throttle:api’, 表示使用名为 api 的 ratelimiter 限制 api 请求的频率。其中:

maxattempts 为 api。decayminutes 为 1。

php 中的参数类型和个数的混用

需要注意的是,php 是一种弱类型语言,因此参数类型和参数个数经常混着用。

‘throttle:60,1′, 表示 maxattempts 为 60,decayminutes 为 1。’throttle:api’, 表示 maxattempts 为 api(默认),decayminutes 为 1(默认)。

以上就是Laravel 中间件`throttle:api`究竟是如何限制API请求频率的?的详细内容,更多请关注范的资源库其它相关文章!

转载请注明:范的资源库 » Laravel中间件`throttle:api`究竟是如何限制API请求频率的?

喜欢 (0)