class ThrottleRequestsWithRedis extends ThrottleRequests

Traits

Properties

protected RateLimiter $limiter

The rate limiter instance.

from  ThrottleRequests
static protected bool $shouldHashKeys

Indicates if the rate limiter keys should be hashed.

from  ThrottleRequests
protected Factory $redis

The Redis factory implementation.

array $decaysAt

The timestamp of the end of the current duration by key.

array $remaining

The number of remaining slots by key.

Methods

int
secondsUntil(DateInterval|DateTimeInterface|int $delay)

Get the number of seconds until the given DateTime.

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

Get the "available at" UNIX timestamp.

parseDateInterval(DateInterval|DateTimeInterface|int|null $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.

string
runTimeForHumans(float $startTime, float|null $endTime = null)

Given a start time, format the total run time for human readability.

__construct(RateLimiter $limiter, Factory $redis)

Create a new request throttler.

static string
using(UnitEnum|string $name)

Specify the named rate limiter to use for the middleware.

static string
with(int $maxAttempts = 60, int $decayMinutes = 1, string $prefix = '')

Specify the rate limiter configuration for the middleware.

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

Handle an incoming request.

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

Handle an incoming request using a named limiter.

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.

static void
shouldHashKeys(bool $shouldHashKeys = true)

Specify whether rate limiter keys should be hashed.

static void
flushState()

Flush all static state.

bool
tooManyAttempts(string $key, int $maxAttempts, int $decaySeconds)

Determine if the given key has been "accessed" too many times.

getRedisConnection()

Get the Redis connection that should be used for throttling.

Details

in InteractsWithTime at line 17
protected int secondsUntil(DateInterval|DateTimeInterface|int $delay)

Get the number of seconds until the given DateTime.

Parameters

DateInterval|DateTimeInterface|int $delay

Return Value

int

in InteractsWithTime at line 29
protected int availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)

Get the "available at" UNIX timestamp.

Parameters

DateInterval|DateTimeInterface|int|null $delay

Return Value

int

in InteractsWithTime at line 41
protected DateTimeInterface|int parseDateInterval(DateInterval|DateTimeInterface|int|null $delay)

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

Parameters

DateInterval|DateTimeInterface|int|null $delay

Return Value

DateTimeInterface|int

in InteractsWithTime at line 57
protected int currentTime()

Get the current system time as a UNIX timestamp.

Return Value

int

in InteractsWithTime at line 65
protected string runTimeForHumans(float $startTime, float|null $endTime = null)

Given a start time, format the total run time for human readability.

Parameters

float $startTime
float|null $endTime

Return Value

string

at line 35
__construct(RateLimiter $limiter, Factory $redis)

Create a new request throttler.

Parameters

RateLimiter $limiter
Factory $redis

in ThrottleRequests at line 47
static string using(UnitEnum|string $name)

Specify the named rate limiter to use for the middleware.

Parameters

UnitEnum|string $name

Return Value

string

in ThrottleRequests at line 57
static string with(int $maxAttempts = 60, int $decayMinutes = 1, string $prefix = '')

Specify the rate limiter configuration for the middleware.

Parameters

int $maxAttempts
int $decayMinutes
string $prefix

Return Value

string

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

Handle an incoming request.

Parameters

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

Return Value

Response

Exceptions

ThrottleRequestsException
MissingRateLimiterException

in ThrottleRequests at line 96
protected Response handleRequestUsingNamedLimiter(Request $request, Closure $next, string $limiterName, Closure $limiter)

Handle an incoming request using a named limiter.

Parameters

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

Return Value

Response

Exceptions

ThrottleRequestsException

at line 47
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

in ThrottleRequests at line 167
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

Exceptions

MissingRateLimiterException

in ThrottleRequests at line 194
protected string resolveRequestSignature(Request $request)

Resolve request signature.

Parameters

Request $request

Return Value

string

Exceptions

RuntimeException

in ThrottleRequests at line 209
protected ThrottleRequestsException|HttpResponseException 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|HttpResponseException

at line 98
protected int getTimeUntilNextRetry(string $key)

Get the number of seconds until the next retry.

Parameters

string $key

Return Value

int

in ThrottleRequests at line 235
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

in ThrottleRequests at line 247
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

at line 90
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

in ThrottleRequests at line 290
static void shouldHashKeys(bool $shouldHashKeys = true)

Specify whether rate limiter keys should be hashed.

Boot-only. The flag persists in a static property for the worker lifetime and applies to every subsequent request.

Parameters

bool $shouldHashKeys

Return Value

void

in ThrottleRequests at line 298
static void flushState()

Flush all static state.

Return Value

void

at line 71
protected bool tooManyAttempts(string $key, int $maxAttempts, int $decaySeconds)

Determine if the given key has been "accessed" too many times.

Parameters

string $key
int $maxAttempts
int $decaySeconds

Return Value

bool

at line 106
protected RedisProxy getRedisConnection()

Get the Redis connection that should be used for throttling.

Return Value

RedisProxy