improve throttle middleware

This commit is contained in:
Irfan 2019-11-15 08:20:39 -08:00
parent 8ccf7a3011
commit 3e311808a8
2 changed files with 8 additions and 24 deletions

View File

@ -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=

View File

@ -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();