/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param int $limit * @param int $time * * @throws \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException * * @return mixed */ public function handle($request, Closure $next, $limit = 10, $time = 60) { if (!$this->throttle->attempt($request, $limit, $time)) { throw new TooManyRequestsHttpException($time * 60, 'Rate limit exceeded.'); } return $next($request); }
/** * Setup the filters. * * @param \Illuminate\Routing\Router $router * @param \GrahamCampbell\Throttle\Throttle $throttle * * @return void */ protected function setupFilters(Router $router, Throttle $throttle) { $router->filter('throttle', function ($route, $request, $limit = 10, $time = 60) use($throttle) { if (!$throttle->attempt($request, $limit, $time)) { throw new TooManyRequestsHttpException($time * 60, 'Rate limit exceed.'); } }); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * * @throws \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException * * @return mixed */ public function handle($request, Closure $next) { if ($this->isEnabledFor($request)) { $limit = 25; // request limit $time = 10; // ban time if (false === $this->throttle->attempt($request, $limit, $time)) { throw new TooManyRequestsHttpException($time * 60, 'Rate limit exceed.'); } } return $next($request); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param int $maxRequests * @param int $minutes * * @return mixed */ public function handle($request, Closure $next, $maxRequests = 30, $minutes = 1) { // dd($this->timeToUnlock($this->throttle->getFactory())); if (!$this->throttle->attempt($request, $maxRequests, $minutes)) { // throw new TooManyRequestsHttpException($minutes * 60, 'Rate limit exceeded.'); $response = response('Rate limit exceeded.', 429); $response->header('Retry-After', $minutes * 60); $this->throttle->clear($request); // TODO: call appropriately } else { $response = $next($request); } $remainingHits = $maxRequests - $this->throttle->count($request); if ($remainingHits < 0) { $remainingHits = 0; } $response->header('X-Rate-Limit-Limit', $maxRequests); $response->header('X-Rate-Limit-Remaining', $remainingHits); return $response; }