remove discontinued throttle middleware

This commit is contained in:
Irfan 2022-12-03 02:12:01 +05:00
parent 8df86920ba
commit b0e4c4ce66
3 changed files with 0 additions and 92 deletions

View File

@ -122,16 +122,6 @@ QUEUE_DELAY_PER_JOB=5
# cloud id (optional)
# ELASTICSEARCH_CLOUD_ID=cloudid
###
# Throttling
# Rate limiting requests
###
THROTTLE=false
THROTTLE_DECAY_MINUTES=1
THROTTLE_MAX_REQUESTS_PER_DECAY_MINUTES=60
THROTTLE_MAX_REQUESTS_PER_SECOND=2
###
# GitHub generate report URL on fatal errors
###

View File

@ -1,78 +0,0 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class Throttle
{
public $maxAttemptsPerDecayMinutes = 30;
public $maxAttemptsPerConcurrency = 2;
public $decayMinutes = 1;
private $userRequests = [];
public function handle(Request $request, Closure $next)
{
if ($request->header('auth') === env('APP_KEY')) {
return $next($request);
}
if (!env('THROTTLE', false)) {
return $next($request);
}
// don't throttle base requests
if ($request->is('/')) {
return $next($request);
}
$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();
$this->hit($key);
$data = app('redis')->keys("user:{$signature}:*");
foreach ($data as $user) {
$this->userRequests[$user] = (int) app('redis')->get($user);
}
// throttle concurrent requests
if (array_sum($this->userRequests) > $this->maxAttemptsPerDecayMinutes) {
return response()->json([
'error' => 'You are being rate limited [MAX: '.$this->maxAttemptsPerDecayMinutes.' requests/'.$this->decayMinutes.' minute(s)]'
], 429);
}
// requests per DECAY_MINUTES
$requestsThisSecond = (int) app('redis')->get($key);
if ($requestsThisSecond > $this->maxAttemptsPerConcurrency) {
return response()->json([
'error' => 'You are being rate limited [MAX: '.$this->maxAttemptsPerConcurrency.' requests/second]'
], 429);
}
return $next($request);
}
protected function resolveRequestSignature(Request $request)
{
return sha1(
$request->getHost() . '|' . $request->ip()
);
}
protected function hit(string $key)
{
if (!app('redis')->exists($key)) {
app('redis')->set($key, 0);
app('redis')->expire($key, $this->decayMinutes*60);
}
app('redis')->incr($key);
}
}

View File

@ -85,9 +85,6 @@ if (env('INSIGHTS', false)) {
$app->middleware($globalMiddleware);
$app->routeMiddleware([
// 'meta' => App\Http\Middleware\Meta::class,
// 'cache-resolver' => App\Http\Middleware\CacheResolver::class,
// 'throttle' => App\Http\Middleware\Throttle::class,
'microcaching' => \App\Http\Middleware\MicroCaching::class,
'source-health-monitor' => SourceHeartbeatMonitor::class,
]);
@ -176,7 +173,6 @@ if (env("SCOUT_DRIVER") === "Matchish\ScoutElasticSearch\Engines\ElasticSearchEn
*/
$commonMiddleware = [
// 'throttle'
'source-health-monitor',
'microcaching',
];