mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
improve throttle middleware
This commit is contained in:
parent
8ccf7a3011
commit
3e311808a8
11
.env.dist
11
.env.dist
@ -4,13 +4,6 @@ APP_KEY=
|
||||
APP_TIMEZONE=UTC
|
||||
APP_URL=http://localhost
|
||||
|
||||
DB_CONNECTION=mysql
|
||||
DB_HOST=127.0.0.1
|
||||
DB_PORT=3306
|
||||
DB_DATABASE=homestead
|
||||
DB_USERNAME=homestead
|
||||
DB_PASSWORD=secret
|
||||
|
||||
CACHE_DRIVER=file
|
||||
QUEUE_CONNECTION=redis
|
||||
|
||||
@ -25,8 +18,8 @@ QUEUE_DELAY_PER_JOB=5
|
||||
|
||||
THROTTLE=false
|
||||
THROTTLE_DECAY_MINUTES=1
|
||||
THROTTLE_MAX_PER_DECAY_MINUTES=30
|
||||
THROTTLE_MAX_PER_CONCURRENCY=2
|
||||
THROTTLE_MAX_REQUESTS_PER_DECAY_MINUTES=60
|
||||
THROTTLE_MAX_REQUESTS_PER_SECOND=2
|
||||
|
||||
SLAVE_INSTANCE=false
|
||||
SLAVE_KEY=
|
||||
|
@ -15,27 +15,18 @@ class Throttle
|
||||
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
// don't throttle meta requests
|
||||
if (\in_array('meta', $request->segments())) {
|
||||
if (!env('THROTTLE', false)) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
if (env('THROTTLE') === false) {
|
||||
// don't throttle base requests
|
||||
if ($request->is('/')) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
|
||||
if (!is_null(env('THROTTLE_DECAY_MINUTES'))) {
|
||||
$this->decayMinutes = (int) env('THROTTLE_DECAY_MINUTES');
|
||||
}
|
||||
|
||||
if (!is_null(env('THROTTLE_MAX_PER_DECAY_MINUTES'))) {
|
||||
$this->maxAttemptsPerDecayMinutes = (int) env('THROTTLE_MAX_PER_DECAY_MINUTES');
|
||||
}
|
||||
|
||||
if (!is_null(env('THROTTLE_MAX_PER_CONCURRENCY'))) {
|
||||
$this->maxAttemptsPerConcurrency = (int) env('THROTTLE_MAX_PER_CONCURRENCY');
|
||||
}
|
||||
$this->decayMinutes = (int) env('THROTTLE_DECAY_MINUTES', 1);
|
||||
$this->maxAttemptsPerDecayMinutes = (int) env('THROTTLE_MAX_REQUESTS_PER_DECAY_MINUTES', 60);
|
||||
$this->maxAttemptsPerConcurrency = (int) env('THROTTLE_MAX_REQUESTS_PER_SECOND', 2);
|
||||
|
||||
$signature = $this->resolveRequestSignature($request);
|
||||
$key = "user:{$signature}:" . time();
|
||||
|
Loading…
x
Reference in New Issue
Block a user