lsky-org / lsky-pro

☁️兰空图床(Lsky Pro) - Your photo album on the cloud.
https://www.lsky.pro
GNU General Public License v3.0
4.07k stars 600 forks source link

请问在哪里可以设置接口的请求配额,或者如何解决短时间多次调用api会返回429状态码的问题 #554

Open Galaxy-ht opened 1 year ago

Galaxy-ht commented 1 year ago

请问在哪里可以设置接口的请求配额,或者如何解决短时间多次调用api会返回429状态码的问题

wisp-x commented 1 year ago

https://github.com/lsky-org/lsky-pro/blob/bb37a11bfbd5e072d7e0bec234e9220d434396c0/app/Providers/RouteServiceProvider.php#L60 具体如何配置可以查阅 laravel 官方文档。

gztchan commented 2 months ago

@Galaxy-ht

Change perMinute in app/Providers/RouteServiceProvider.php. This affects all api:

protected function configureRateLimiting()
{
    RateLimiter::for('api', function (Request $request) {
        return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
    });
}

Especially for lsky-pro, change throttle:3 to meet your need. The middleware limits how many tokens you can create in 1 minute:

Route::group([
    'prefix' => 'v1',
    'middleware' => CheckIsEnableApi::class,
], function () {
    Route::get('strategies', [StrategyController::class, 'index']);
    Route::post('upload', [ImageController::class, 'upload']);
    Route::post('tokens', [TokenController::class, 'store'])->middleware('throttle:3');

    Route::group([
        'middleware' => 'auth:sanctum',
    ], function () {
        Route::get('images', [ImageController::class, 'images']);
        Route::delete('images/{key}', [ImageController::class, 'destroy']);
        Route::get('albums', [AlbumController::class, 'index']);
        Route::delete('albums/{id}', [AlbumController::class, 'destroy']);
        Route::delete('tokens', [TokenController::class, 'clear']);
        Route::get('profile', [UserController::class, 'index']);
    });
});