我正在使用一個名為 Artillery 的負載測驗工具來模擬我的 Laravel 應用程式上的請求。默認情況下,Laravel 應用程式使用 IP 來檢測它是否應該限制速率,因此在生產中不同的 IP 地址不會成為問題,但對于 Artillery,這是一個問題,因為請求回傳 429 錯誤。
我嘗試configureRateLimiting在生產中禁用,但仍然出現 429 錯誤。
Artillery 每秒至少向我的 api 端點發送至少 3 個請求,持續至少 2 分鐘,然后在 15 分鐘內加速到大約每秒 30 個請求。
在這里禁用速率限制我缺少什么?
<?php
namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* @var string
*/
public const HOME = '/home';
/**
* The controller namespace for the application.
*
* When present, controller route declarations will automatically be prefixed with this namespace.
*
* @var string|null
*/
// protected $namespace = 'App\\Http\\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
if (!config('artillery.enabled')) {
$this->configureRateLimiting();
}
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting()
{
if (config('artillery.enabled')) {
return;
}
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(300)->by(optional($request->user())->id ?: $request->ip());
});
}
}
我的網站通過 Cliudflare,我正在向我的生產端點發送請求,因為這是最現實的測驗。
uj5u.com熱心網友回復:
在app/Http/Kernel.php 中,Laravel 對所有 api 路由都有一個默認的油門限制。
protected $middlewareGroups = [
...
'api' => [
'throttle:60,1', //comment means it would be no limit
],
];
評論或增加它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/416994.html
標籤:
下一篇:在陣列緊縮之后我得到陣列喜歡這個[[10,9,7],[6,5,4],[3,1]],但我想為陣列的所有元素進行foreach回圈
