Download the PHP package phpsoftbox/rate-limiter without Composer
On this page you can find all versions of the php package phpsoftbox/rate-limiter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phpsoftbox/rate-limiter
More information about phpsoftbox/rate-limiter
Files in phpsoftbox/rate-limiter
Package rate-limiter
Short Description Rate limiter component for the PhpSoftBox framework
License MIT
Informations about the package rate-limiter
RateLimiter
Компонент реализует fixed-window rate limiting.
API
RateLimiterInterface:
hit(string $key, int $maxAttempts, int $decaySeconds): RateLimitResult— регистрирует попытку; окно длиной$decaySecondsоткрывается первой попыткой;attempts(string $key): int— число попыток в текущем окне (0, если окна нет или оно истекло);reset(string $key): void— сбрасывает счётчик, например после успешного входа.
Время
Все лимитеры принимают необязательный PSR-20 Psr\Clock\ClockInterface (например, PhpSoftBox\Clock\FrozenClock
в тестах). Без него используется системное время time().
В RedisRateLimiter часы влияют только на resetAt: длину окна отсчитывает TTL в Redis.
Production
Для нескольких workers используйте атомарный storage:
RedisRateLimiter выполняет increment и установку TTL одним Lua script.
Поддерживается клиент PhpRedis (ext-redis): Redis или RedisCluster — тип проверяется в конструкторе.
Predis и другие клиенты (у Predis иная сигнатура eval()) подключаются собственной реализацией
AtomicRateLimitStoreInterface (increment, attempts, reset) и AtomicStoreRateLimiter.
SimpleCacheRateLimiter использует обычный PSR-16 get/set, поэтому подходит
только для development, тестов или гарантированно single-process окружения.
RateLimitResult содержит:
limit;remaining;retryAfterSeconds— относительное ожидание;resetAt— абсолютный Unix timestamp.
Ключи
HashRateLimitKeyNormalizer формирует PSR-16-safe ключ из namespace и SHA-256:
Raw credentials нельзя включать даже в исходный ключ. Используйте Node ID, credential selector или необратимый hash безопасного идентификатора.