nikolaposa / rate-limit

🚔 General purpose rate limiter implementation.
MIT License
271 stars 47 forks source link

Rate as configuration #52

Closed nikolaposa closed 3 years ago

nikolaposa commented 3 years ago

Remove Rate parameter from RateLimiter and SilentRateLimiter interfaces.

This decision was prompted by the most common Rate Limiter use scenarios, where unlike identifier parameter, Rate parameter is static, it never changes once rate limiting clause is put in place. This is nicely illustrated in the example of a RateLimitMiddleware, which in addition to the RateLimiter itself needs to be constructed with a specific Rate for which it should run.

Rate really is a configuration, not a dynamic parameter to be passed in runtime. In practice, this library is used in a way that multiplle rate limiting strategies are registered with the Dependency Injection Container of the project, for example:

$container->set('rate_limiter.web', new RedisRateLimiter(Rate::perMinute(100), $container->get('redis'));
$container->set('rate_limiter.api', new RedisRateLimiter(Rate::perSecond(10), $container->get('redis'));