RediBox Throttle
Provides throttle
and pThrottle
lua scripts for RediBox redis clients.
Installation
npm i redibox-hook-throttle --save
No configuration required.
API
client.throttle(key, limit, seconds)
Args:
- key - key name - ip, user id or some unique key to throttle X by
- limit - limit amount, e.g. '20' requests
- seconds - ttl in secondsof this limit, i.e, '60' - 20 requests per 60 seconds.
Returns:
- ARRAY
- [0] throttled: -> 1 if you should throttle/reject this request, 0 if still within limit
- [1] remaining: -> how many times left until throttled, i.e. 11 requests left
- [2] ttl: -> seconds remaining until limit resets
Example:
In this example we have an express api server and we want to limit each user to no more than 10 requests per minute. We can use throttle in a middleware to achieve this.
const app = ; app;
client.pthrottle(key, limit, milliseconds)
pThrottle
is exactly the same as throttle
but uses millisecond precision instead of seconds.