-
Couldn't load subscription status.
- Fork 11
Open
Description
BASIC INFO
version used: "redis-token-bucket-ratelimiter": "^0.5.1"
EXPECTED
When I await limiter.use(), I expected to get RollingLimiterResult object. As documented on readme page example
Example below uses `limit.rejected' to tell if a request was rejected
async function rateLimitMiddleware(req, res, next) {
const id = getUserId(req);
const limit = await requestLimiter.use(id);
// Your max tokens
res.set('X-RateLimit-Limit', String(limit.limit));
// Remaining tokens; this continually refills
res.set('X-RateLimit-Remaining', String(limit.remaining));
// The time at which it's valid to do the same request again; this is almost always now()
const retrySec = Math.ceil(limit.retryDelta / 1000);
res.set(
'X-RateLimit-Reset',
String(Math.ceil(Date.now() / 1000) + retrySec)
);
if (limit.rejected) {
res.set('Retry-After', String(retrySec));
res.status(429).json({
error: {
message: `Rate limit exceeded, retry in ${retrySec} seconds.`,
name: 'RateLimitError',
},
});
return;
}
next();
}
ACTUAL
When use() amount exceeds limit, the function throws error instead
As shown in code here: https://github.com/BitMEX/node-redis-token-bucket-ratelimiter/blob/master/rollingLimit.js#L53
if (amount > this.limit) throw new Error(`amount must be < limit (${this.limit})`);Which one is the correct behavior. Thanks
Metadata
Metadata
Assignees
Labels
No labels