You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 17, 2023. It is now read-only.
Example Connect middleware implementation limiting against a user._id:
constlimiter=newLimiter()limiter.connect(redisClient)// connect to a thunk-redis instancelimiter.get(req.user._id).then(function(limit){response.set('X-RateLimit-Limit',limit.total)response.set('X-RateLimit-Remaining',limit.remaining)response.set('X-RateLimit-Reset',Math.ceil(limit.reset/1000))// all gooddebug('remaining %s/%s %s',limit.remaining,limit.total,id)if(limit.remaining>=0)return// not goodletafter=Math.ceil((limit.reset-Date.now())/1000)response.set('Retry-After',after)response.end(429,'Rate limit exceeded, retry in '+after+' seconds')})
API
new Limiter(options)
Return a limiter instance.
constlimiter=newLimiter()
options.max: Optional, Type: Number, max requests within duration, default to 2500.
options.duration: Optional, Type: Number, of limit in milliseconds, should greater than 100 ms, default to 3600000.
options.prefix: Optional, Type: String, redis key namespace, default to LIMIT.
Limiter.prototype.connect([host, options]) => this
Limiter.prototype.connect(redisClient) => this
Connect to redis. Arguments are the same as thunk-redis's createClient, or give a thunk-redis instance.
Return a promise that guarantee a limiter result. it support more max and duration pairs ad limit policy. The first pairs will be used as default. If some trigger limit, then the limiter will apply the next pair policy.