InMemoryRateLimiter
In-memory rate limiter using sliding window algorithm. Note: This implementation is per-process. For production with multiple workers, use RedisRateLimiter instead.
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 |