class ThrottleRequests (View source)

Traits

Properties

protected RateLimiter $limiter

The rate limiter instance.

Methods

int
secondsUntil(DateTimeInterface|DateInterval|int $delay)

Get the number of seconds until the given DateTime.

int
availableAt(DateTimeInterface|DateInterval|int $delay = 0)

Get the "available at" UNIX timestamp.

parseDateInterval(DateTimeInterface|DateInterval|int $delay)

If the given value is an interval, convert it to a DateTime instance.

int
currentTime()

Get the current system time as a UNIX timestamp.

void
__construct(RateLimiter $limiter)

Create a new request throttler.

Response
handle(Request $request, Closure $next, int|string $maxAttempts = 60, float|int $decayMinutes = 1, string $prefix = '')

Handle an incoming request.

Response
handleRequestUsingNamedLimiter(Request $request, Closure $next, string $limiterName, Closure $limiter)

Handle an incoming request.

Response
handleRequest(Request $request, Closure $next, array $limits)

Handle an incoming request.

int
resolveMaxAttempts(Request $request, int|string $maxAttempts)

Resolve the number of attempts if the user is authenticated or not.

string
resolveRequestSignature(Request $request)

Resolve request signature.

buildException(Request $request, string $key, int $maxAttempts, callable|null $responseCallback = null)

Create a 'too many attempts' exception.

int
getTimeUntilNextRetry(string $key)

Get the number of seconds until the next retry.

Response
addHeaders(Response $response, int $maxAttempts, int $remainingAttempts, int|null $retryAfter = null)

Add the limit header information to the given response.

array
getHeaders(int $maxAttempts, int $remainingAttempts, int|null $retryAfter = null, Response|null $response = null)

Get the limit headers information.

int
calculateRemainingAttempts(string $key, int $maxAttempts, int|null $retryAfter = null)

Calculate the number of remaining attempts.

Details

protected int secondsUntil(DateTimeInterface|DateInterval|int $delay)

Get the number of seconds until the given DateTime.

Parameters

DateTimeInterface|DateInterval|int $delay

Return Value

int

protected int availableAt(DateTimeInterface|DateInterval|int $delay = 0)

Get the "available at" UNIX timestamp.

Parameters

DateTimeInterface|DateInterval|int $delay

Return Value

int

protected DateTimeInterface|int parseDateInterval(DateTimeInterface|DateInterval|int $delay)

If the given value is an interval, convert it to a DateTime instance.

Parameters

DateTimeInterface|DateInterval|int $delay

Return Value

DateTimeInterface|int

protected int currentTime()

Get the current system time as a UNIX timestamp.

Return Value

int

void __construct(RateLimiter $limiter)

Create a new request throttler.

Parameters

RateLimiter $limiter

Return Value

void

Response handle(Request $request, Closure $next, int|string $maxAttempts = 60, float|int $decayMinutes = 1, string $prefix = '')

Handle an incoming request.

Parameters

Request $request
Closure $next
int|string $maxAttempts
float|int $decayMinutes
string $prefix

Return Value

Response

Exceptions

ThrottleRequestsException

protected Response handleRequestUsingNamedLimiter(Request $request, Closure $next, string $limiterName, Closure $limiter)

Handle an incoming request.

Parameters

Request $request
Closure $next
string $limiterName
Closure $limiter

Return Value

Response

Exceptions

ThrottleRequestsException

protected Response handleRequest(Request $request, Closure $next, array $limits)

Handle an incoming request.

Parameters

Request $request
Closure $next
array $limits

Return Value

Response

Exceptions

ThrottleRequestsException

protected int resolveMaxAttempts(Request $request, int|string $maxAttempts)

Resolve the number of attempts if the user is authenticated or not.

Parameters

Request $request
int|string $maxAttempts

Return Value

int

protected string resolveRequestSignature(Request $request)

Resolve request signature.

Parameters

Request $request

Return Value

string

Exceptions

RuntimeException

protected ThrottleRequestsException buildException(Request $request, string $key, int $maxAttempts, callable|null $responseCallback = null)

Create a 'too many attempts' exception.

Parameters

Request $request
string $key
int $maxAttempts
callable|null $responseCallback

Return Value

ThrottleRequestsException

protected int getTimeUntilNextRetry(string $key)

Get the number of seconds until the next retry.

Parameters

string $key

Return Value

int

protected Response addHeaders(Response $response, int $maxAttempts, int $remainingAttempts, int|null $retryAfter = null)

Add the limit header information to the given response.

Parameters

Response $response
int $maxAttempts
int $remainingAttempts
int|null $retryAfter

Return Value

Response

protected array getHeaders(int $maxAttempts, int $remainingAttempts, int|null $retryAfter = null, Response|null $response = null)

Get the limit headers information.

Parameters

int $maxAttempts
int $remainingAttempts
int|null $retryAfter
Response|null $response

Return Value

array

protected int calculateRemainingAttempts(string $key, int $maxAttempts, int|null $retryAfter = null)

Calculate the number of remaining attempts.

Parameters

string $key
int $maxAttempts
int|null $retryAfter

Return Value

int