Skip to content
docsv0.19.1

InMemoryRateLimiter

In-memory rate limiter using a sliding window. PER-PROCESS, and that is the whole caveat: several workers each keep their own buckets, so a limit of ten is ten PER WORKER. Whoever needs one shared count implements {@see RateLimiterInterface} over a store they run. This docblock used to end «use RedisRateLimiter instead». **There is no RedisRateLimiter** — not in this package, not anywhere in the framework. Advice pointing at a class nobody wrote reads as a shipped alternative and costs a reader the search before they find out (greenhouse decisions/0213 row 7, decisions/0215 F5). Naming the contract instead is the honest version: the seam is real, the implementation is yours.

InMemoryRateLimiter::consume()

public function consume(string $key, int $cost = 1, int $windowSeconds = 60, int $maxTokens = 100): Milpa\ToolRuntime\RateLimiting\RateLimitResult

Attempt to consume tokens from this key's in-memory sliding-window bucket. Lazily resets the bucket once its window has elapsed, then denies the call when consuming would push usage past $maxTokens within the window; otherwise deducts $cost immediately. Buckets live in a process-local array, so limits are not shared across workers/processes.

Parameters

Parameters of consume()
NameTypeDescription
$keystring
$costint
$windowSecondsint
$maxTokensint

InMemoryRateLimiter::getUsage()

public function getUsage(string $key): int

Read current token usage for a key without consuming any; an expired window reads as zero.

Parameters

Parameters of getUsage()
NameTypeDescription
$keystring

InMemoryRateLimiter::reset()

public function reset(string $key): void

Discard the bucket for a key, immediately restoring its full quota.

Parameters

Parameters of reset()
NameTypeDescription
$keystring

InMemoryRateLimiter::cleanup()

public function cleanup(int $maxAge = 300): void

Cleanup expired buckets (call periodically to free memory).

Parameters

Parameters of cleanup()
NameTypeDescription
$maxAgeint