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\RateLimitResultAttempt 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
| Name | Type | Description |
|---|---|---|
| $key | string | |
| $cost | int | |
| $windowSeconds | int | |
| $maxTokens | int |
InMemoryRateLimiter::getUsage()
public function getUsage(string $key): intRead current token usage for a key without consuming any; an expired window reads as zero.
Parameters
| Name | Type | Description |
|---|---|---|
| $key | string |
InMemoryRateLimiter::reset()
public function reset(string $key): voidDiscard the bucket for a key, immediately restoring its full quota.
Parameters
| Name | Type | Description |
|---|---|---|
| $key | string |
InMemoryRateLimiter::cleanup()
public function cleanup(int $maxAge = 300): voidCleanup expired buckets (call periodically to free memory).
Parameters
| Name | Type | Description |
|---|---|---|
| $maxAge | int |