class DatabaseStore implements PrunableStore, Store

Traits

Constants

protected MAX_PRUNE_CHUNK_SIZE

Methods

calculateConsume(AdmissionPolicy $policy, int $now, int $value, int $secondaryValue, int $expiresAt)

Calculate an admission decision and update accepted state values.

array
calculateMany(array $policies, int $now, array $states)

Calculate ordered group decisions against staged state.

calculateInspection(AdmissionPolicy|Backoff|Cooldown $policy, int $now, int $value, int $secondaryValue, int $expiresAt)

Calculate a non-mutating policy inspection.

calculateCooldownBlock(int $durationMicroseconds, int $now, int $value, int $secondaryValue, int $expiresAt)

Calculate a cooldown extension and update its expiry.

calculateFailure(Backoff $backoff, int $now, int $value, int $secondaryValue, int $expiresAt)

Calculate a failure transition and update its state values.

int
currentTimeInMicroseconds()

Get the current epoch time in microseconds.

__construct(ConnectionResolverInterface $connections, string|null $connectionName, string $table)

Create a new database rate limiter store.

consume(string $key, AdmissionPolicy $policy)

Atomically consume capacity from an admission policy.

array
consumeMany(array $policies)

Consume all policies in one transaction, leaving capacity unchanged on denial.

array
consumeStates(ConnectionInterface $connection, array $policies, array $states)

Calculate a locked group and persist its accepted state.

block(string $key, int $durationMicroseconds)

Atomically extend a cooldown block.

inspect(string $key, AdmissionPolicy|Backoff|Cooldown $policy)

Inspect a policy without mutating its state.

recordFailure(string $key, Backoff $backoff)

Record a failure against a backoff policy.

bool
clear(string $key)

Clear the state for a physical limiter key.

int
pruneExpired(int $chunkSize = 1000)

Prune expired state in bounded batches.

mutateState(ConnectionInterface $connection, string $key, Closure $callback)

Mutate locked state, initializing a missing non-SQLite row in a new transaction.

array
emptyStateRow(string $key)

Return an empty state row for a physical limiter key.

void
insertStateRow(ConnectionInterface $connection, string $key)

Insert an empty state row if the key does not exist.

array
initializeStateRowForUpdate(ConnectionInterface $connection, string $key)

Initialize and lock state for a missing non-SQLite physical limiter key.

array|null
stateForUpdate(ConnectionInterface $connection, string $key)

Lock and read state, initializing SQLite state under its writer lock.

array|null
findStateForUpdate(ConnectionInterface $connection, string $key)

Find and lock numeric state for a physical limiter key.

array
stateFromRow(object $row)

Get and validate numeric state from a database row.

void
writeState(ConnectionInterface $connection, string $key, int $value, int $secondaryValue, int $expiresAt)

Write numeric state for a physical limiter key.

void
ensureCanMutate(ConnectionInterface $connection)

Ensure the connection can safely mutate limiter state.

Details

protected LimitResult calculateConsume(AdmissionPolicy $policy, int $now, int $value, int $secondaryValue, int $expiresAt)

Calculate an admission decision and update accepted state values.

Parameters

AdmissionPolicy $policy
int $now
int $value
int $secondaryValue
int $expiresAt

Return Value

LimitResult

protected array calculateMany(array $policies, int $now, array $states)

Calculate ordered group decisions against staged state.

Parameters

array $policies
int $now
array $states

Return Value

array

in CalculatesRateLimits at line 118
protected LimitResult|BackoffResult|CooldownResult calculateInspection(AdmissionPolicy|Backoff|Cooldown $policy, int $now, int $value, int $secondaryValue, int $expiresAt)

Calculate a non-mutating policy inspection.

Parameters

AdmissionPolicy|Backoff|Cooldown $policy
int $now
int $value
int $secondaryValue
int $expiresAt

Return Value

LimitResult|BackoffResult|CooldownResult

in CalculatesRateLimits at line 172
protected CooldownResult calculateCooldownBlock(int $durationMicroseconds, int $now, int $value, int $secondaryValue, int $expiresAt)

Calculate a cooldown extension and update its expiry.

Parameters

int $durationMicroseconds
int $now
int $value
int $secondaryValue
int $expiresAt

Return Value

CooldownResult

in CalculatesRateLimits at line 190
protected BackoffResult calculateFailure(Backoff $backoff, int $now, int $value, int $secondaryValue, int $expiresAt)

Calculate a failure transition and update its state values.

Parameters

Backoff $backoff
int $now
int $value
int $secondaryValue
int $expiresAt

Return Value

BackoffResult

in CalculatesRateLimits at line 638
protected int currentTimeInMicroseconds()

Get the current epoch time in microseconds.

Return Value

int

at line 26
__construct(ConnectionResolverInterface $connections, string|null $connectionName, string $table)

Create a new database rate limiter store.

Parameters

ConnectionResolverInterface $connections
string|null $connectionName
string $table

at line 36
LimitResult consume(string $key, AdmissionPolicy $policy)

Atomically consume capacity from an admission policy.

Parameters

string $key
AdmissionPolicy $policy

Return Value

LimitResult

at line 68
array consumeMany(array $policies)

Consume all policies in one transaction, leaving capacity unchanged on denial.

Parameters

array $policies

Return Value

array

at line 120
protected array consumeStates(ConnectionInterface $connection, array $policies, array $states)

Calculate a locked group and persist its accepted state.

Parameters

ConnectionInterface $connection
array $policies
array $states

Return Value

array

at line 136
CooldownResult block(string $key, int $durationMicroseconds)

Atomically extend a cooldown block.

Parameters

string $key
int $durationMicroseconds

Return Value

CooldownResult

at line 165
LimitResult|BackoffResult|CooldownResult inspect(string $key, AdmissionPolicy|Backoff|Cooldown $policy)

Inspect a policy without mutating its state.

Parameters

string $key
AdmissionPolicy|Backoff|Cooldown $policy

Return Value

LimitResult|BackoffResult|CooldownResult

at line 190
BackoffResult recordFailure(string $key, Backoff $backoff)

Record a failure against a backoff policy.

Parameters

string $key
Backoff $backoff

Return Value

BackoffResult

at line 217
bool clear(string $key)

Clear the state for a physical limiter key.

Parameters

string $key

Return Value

bool

at line 231
int pruneExpired(int $chunkSize = 1000)

Prune expired state in bounded batches.

Parameters

int $chunkSize

Return Value

int

at line 280
protected BackoffResult|CooldownResult|LimitResult mutateState(ConnectionInterface $connection, string $key, Closure $callback)

Mutate locked state, initializing a missing non-SQLite row in a new transaction.

Parameters

ConnectionInterface $connection
string $key
Closure $callback

Return Value

BackoffResult|CooldownResult|LimitResult

at line 308
protected array emptyStateRow(string $key)

Return an empty state row for a physical limiter key.

Parameters

string $key

Return Value

array

at line 321
protected void insertStateRow(ConnectionInterface $connection, string $key)

Insert an empty state row if the key does not exist.

Parameters

ConnectionInterface $connection
string $key

Return Value

void

at line 331
protected array initializeStateRowForUpdate(ConnectionInterface $connection, string $key)

Initialize and lock state for a missing non-SQLite physical limiter key.

Parameters

ConnectionInterface $connection
string $key

Return Value

array

at line 355
protected array|null stateForUpdate(ConnectionInterface $connection, string $key)

Lock and read state, initializing SQLite state under its writer lock.

Parameters

ConnectionInterface $connection
string $key

Return Value

array|null

at line 377
protected array|null findStateForUpdate(ConnectionInterface $connection, string $key)

Find and lock numeric state for a physical limiter key.

Parameters

ConnectionInterface $connection
string $key

Return Value

array|null

at line 396
protected array stateFromRow(object $row)

Get and validate numeric state from a database row.

Parameters

object $row

Return Value

array

at line 408
protected void writeState(ConnectionInterface $connection, string $key, int $value, int $secondaryValue, int $expiresAt)

Write numeric state for a physical limiter key.

Parameters

ConnectionInterface $connection
string $key
int $value
int $secondaryValue
int $expiresAt

Return Value

void

at line 427
protected void ensureCanMutate(ConnectionInterface $connection)

Ensure the connection can safely mutate limiter state.

Parameters

ConnectionInterface $connection

Return Value

void