freyr / message-broker-ratelimit-redis
Redis rate limit store for freyr/message-broker: the token buckets of the webhook lanes in Lua scripts, with a phpredis or a Predis client
Package info
github.com/freyr/message-broker-ratelimit-redis
pkg:composer/freyr/message-broker-ratelimit-redis
Requires
- php: >=8.4
- freyr/message-broker: ^0.15
Requires (Dev)
- ext-redis: *
- infection/infection: ^0.35.11
- phpunit/phpunit: ^13.4
- predis/predis: ^3.2
Suggests
- ext-redis: A \Redis, \RedisCluster or Relay client for PhpRedisScriptRunner
- phpunit/phpunit: Required for Freyr\MessageBroker\RateLimit\Redis\Testing\*, the Redis test set-up
- predis/predis: A Predis client for PredisScriptRunner
Provides
Conflicts
None
Replaces
None
README
This package is the Redis rate limit store of the Freyr Message Broker library,
namespace Freyr\MessageBroker\RateLimit\Redis\.
The repository
freyr/message-broker-ratelimit-redisis a read-only copy of the folderratelimit/redis/of freyr/message-broker. Development, issues and pull requests are in freyr/message-broker. A release has the tagratelimit-redis-v<version>in freyr/message-broker and the tagv<version>infreyr/message-broker-ratelimit-redis.
The adapter holds RedisRateLimiter, which implements the port
Webhook\RateLimiter of the core in Redis. Each call is one atomic Lua
script with the clock of the Redis primary. Each token bucket is one HASH
with a time to live, and all keys of one deployment have one hash tag, so
the store also works in Redis Cluster. The application gives the Redis
client; the package opens no connection.
Requirements
- PHP 8.4 or later
freyr/message-broker^0.15 (the core)- Redis 7.0 or later, or Valkey 7.2 or later
- One Redis client:
ext-redis(a\Redis,\RedisClusteror Relay client) orpredis/predis
Install
composer require freyr/message-broker-ratelimit-redis
Add a Redis client yourself: ext-redis or predis/predis.
Minimal wiring
use Freyr\MessageBroker\RateLimit\Redis\PhpRedisScriptRunner; use Freyr\MessageBroker\RateLimit\Redis\RedisRateLimitConfig; use Freyr\MessageBroker\RateLimit\Redis\RedisRateLimiter; $redis = new \Redis(); $redis->connect('redis', 6379); $rateLimiter = new RedisRateLimiter(new PhpRedisScriptRunner($redis), new RedisRateLimitConfig('shop.prod')); // Give $rateLimiter to Webhook\DeliveryLoop of the core.
With Predis, use new PredisScriptRunner($client).
Documents
- Redis rate limit store: the client bridges, the key prefix and the hash tag, the server versions,
maxmemory-policy, the script cache, the tests - Webhooks: the rate limit rules of the core
- Webhook delivery storage: the PostgreSQL store of the rate limit and the circuit breaker
Tests
The tests need Redis and no PostgreSQL; see Tests.