class RateLimiter

Traits

Properties

protected Repository $cache

The cache store implementation.

protected array $limiters

The configured limit object resolvers.

protected Closure|null $keyScopeResolver

The callback used to resolve the scope for named limiter keys.

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(Repository $cache)

Create a new rate limiter instance.

for(UnitEnum|string $name, Closure $callback)

Register a named limiter configuration.

void
resolveKeyScopeUsing(Closure|null $resolver)

Register the named limiter key scope resolver.

Closure|null
limiter(UnitEnum|string $name)

Get the given named rate limiter.

string
resolveNamedLimiterKey(string $limiterName, Limit $limit, bool $shouldHashKeys = true)

Resolve the storage key for a named rate limit.

mixed
attempt(string $key, int $maxAttempts, Closure $callback, DateInterval|DateTimeInterface|int $decaySeconds = 60)

Attempt to execute a callback if it's not limited.

bool
tooManyAttempts(string $key, int $maxAttempts)

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

int
hit(string $key, DateInterval|DateTimeInterface|int $decaySeconds = 60)

Increment (by 1) the counter for a given key for a given decay time.

int
increment(string $key, DateInterval|DateTimeInterface|int $decaySeconds = 60, int $amount = 1)

Increment the counter for a given key for a given decay time by a given amount.

int
decrement(string $key, DateInterval|DateTimeInterface|int $decaySeconds = 60, int $amount = 1)

Decrement the counter for a given key for a given decay time by a given amount.

mixed
attempts(string $key)

Get the number of attempts for the given key.

bool
resetAttempts(string $key)

Reset the number of attempts for the given key.

int
remaining(string $key, int $maxAttempts)

Get the number of retries left for the given key.

int
retriesLeft(string $key, int $maxAttempts)

Get the number of retries left for the given key.

void
clear(string $key)

Clear the hits and lockout timer for the given key.

int
availableIn(string $key)

Get the number of seconds until the "key" is accessible again.

string
cleanRateLimiterKey(string $key)

Clean the rate limiter key from unicode characters.

mixed
withoutSerializationOrCompression(callable $callback)

Execute the given callback without serialization or compression when applicable.

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 41
__construct(Repository $cache)

Create a new rate limiter instance.

Parameters

Repository $cache

at line 53
RateLimiter for(UnitEnum|string $name, Closure $callback)

Register a named limiter configuration.

Boot-only. The callback persists on the singleton rate limiter for the worker lifetime and applies to every subsequent limiter() lookup; per-request use races across coroutines.

Parameters

UnitEnum|string $name
Closure $callback

Return Value

RateLimiter

at line 68
void resolveKeyScopeUsing(Closure|null $resolver)

Register the named limiter key scope resolver.

Boot-only. The callback persists on the singleton rate limiter for the worker lifetime and applies to subsequent named limits across coroutines.

Parameters

Closure|null $resolver

Return Value

void

at line 76
Closure|null limiter(UnitEnum|string $name)

Get the given named rate limiter.

Parameters

UnitEnum|string $name

Return Value

Closure|null

at line 112
string resolveNamedLimiterKey(string $limiterName, Limit $limit, bool $shouldHashKeys = true)

Resolve the storage key for a named rate limit.

Parameters

string $limiterName
Limit $limit
bool $shouldHashKeys

Return Value

string

at line 135
mixed attempt(string $key, int $maxAttempts, Closure $callback, DateInterval|DateTimeInterface|int $decaySeconds = 60)

Attempt to execute a callback if it's not limited.

Parameters

string $key
int $maxAttempts
Closure $callback
DateInterval|DateTimeInterface|int $decaySeconds

Return Value

mixed

at line 153
bool tooManyAttempts(string $key, int $maxAttempts)

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

Parameters

string $key
int $maxAttempts

Return Value

bool

at line 169
int hit(string $key, DateInterval|DateTimeInterface|int $decaySeconds = 60)

Increment (by 1) the counter for a given key for a given decay time.

Parameters

string $key
DateInterval|DateTimeInterface|int $decaySeconds

Return Value

int

at line 177
int increment(string $key, DateInterval|DateTimeInterface|int $decaySeconds = 60, int $amount = 1)

Increment the counter for a given key for a given decay time by a given amount.

Parameters

string $key
DateInterval|DateTimeInterface|int $decaySeconds
int $amount

Return Value

int

at line 205
int decrement(string $key, DateInterval|DateTimeInterface|int $decaySeconds = 60, int $amount = 1)

Decrement the counter for a given key for a given decay time by a given amount.

Parameters

string $key
DateInterval|DateTimeInterface|int $decaySeconds
int $amount

Return Value

int

at line 213
mixed attempts(string $key)

Get the number of attempts for the given key.

Parameters

string $key

Return Value

mixed

at line 223
bool resetAttempts(string $key)

Reset the number of attempts for the given key.

Parameters

string $key

Return Value

bool

at line 233
int remaining(string $key, int $maxAttempts)

Get the number of retries left for the given key.

Parameters

string $key
int $maxAttempts

Return Value

int

at line 245
int retriesLeft(string $key, int $maxAttempts)

Get the number of retries left for the given key.

Parameters

string $key
int $maxAttempts

Return Value

int

at line 253
void clear(string $key)

Clear the hits and lockout timer for the given key.

Parameters

string $key

Return Value

void

at line 265
int availableIn(string $key)

Get the number of seconds until the "key" is accessible again.

Parameters

string $key

Return Value

int

at line 275
string cleanRateLimiterKey(string $key)

Clean the rate limiter key from unicode characters.

Parameters

string $key

Return Value

string

at line 283
protected mixed withoutSerializationOrCompression(callable $callback)

Execute the given callback without serialization or compression when applicable.

Parameters

callable $callback

Return Value

mixed