class RateLimiter (View source)

Traits

Properties

protected Repository $cache

The cache store implementation.

protected array $limiters

The configured limit object resolvers.

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.

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

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

void
__construct(Repository $cache)

Create a new rate limiter instance.

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

Register a named limiter configuration.

Closure|null
limiter(BackedEnum|UnitEnum|string $name)

Get the given named rate limiter.

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

Attempts 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, int $decaySeconds = 60)

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

int
increment(string $key, 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, 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.

mixed
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.

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

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

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

Parameters

float $startTime
float $endTime

Return Value

string

void __construct(Repository $cache)

Create a new rate limiter instance.

Parameters

Repository $cache

Return Value

void

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

Register a named limiter configuration.

Parameters

BackedEnum|UnitEnum|string $name
Closure $callback

Return Value

$this

Closure|null limiter(BackedEnum|UnitEnum|string $name)

Get the given named rate limiter.

Parameters

BackedEnum|UnitEnum|string $name

Return Value

Closure|null

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

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

Parameters

string $key
int $maxAttempts
Closure $callback
int $decaySeconds

Return Value

mixed

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

int hit(string $key, int $decaySeconds = 60)

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

Parameters

string $key
int $decaySeconds

Return Value

int

int increment(string $key, 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
int $decaySeconds
int $amount

Return Value

int

int decrement(string $key, 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
int $decaySeconds
int $amount

Return Value

int

mixed attempts(string $key)

Get the number of attempts for the given key.

Parameters

string $key

Return Value

mixed

mixed resetAttempts(string $key)

Reset the number of attempts for the given key.

Parameters

string $key

Return Value

mixed

int remaining(string $key, int $maxAttempts)

Get the number of retries left for the given key.

Parameters

string $key
int $maxAttempts

Return Value

int

int retriesLeft(string $key, int $maxAttempts)

Get the number of retries left for the given key.

Parameters

string $key
int $maxAttempts

Return Value

int

void clear(string $key)

Clear the hits and lockout timer for the given key.

Parameters

string $key

Return Value

void

int availableIn(string $key)

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

Parameters

string $key

Return Value

int

string cleanRateLimiterKey(string $key)

Clean the rate limiter key from unicode characters.

Parameters

string $key

Return Value

string