bitmex / node-redis-token-bucket-ratelimiter Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
We have some endpoints that are rate limited but we always want to the requests to succeed even if that sends the token balance negative. Currently we'd need to instantiate two RollingLimit
classes with different options and manage which instance was applied to the different endpoints.
Would you be open to adding a new parameter to the use() signature to specify the value for force? e.g. use(id, cost, force)
Is there a way to get the retryDelta without "using" the ratelimiter?
When you use amount = 0 you will get the amount of token left which is nice but getting the correct retryDelta is what I want.
Anyway the package is a nice implementation of the concept.
I am trying to understand the implementation of token bucket in lua script. I cam across following expression and it's comment from rollinglimit.lua
-- tokens that should have been added by now
-- note math.max in case this ends up negative (clock skew?)
-- now that we call 'TIME' this is less likely to happen
local addTokens = math.max(((nowMS - lastUpdateMS) / intervalMS) * limit, 0)
comment mentions the use of math.max in cases where expression ((nowMS - lastUpdateMS) / intervalMS) * limit
can be negative.
I believe expression can only be negative when lastUpdateMs is in future but that is unlikely happen without external change of value.
I want to understand and make sure the robustness of implementation. Can you specify In which cases expression can end up negative?
I searched about clock skew but it seems to be a low level circuit concept than a programming concpet.
Currently I have one redis instance. May be I am unware of sitation where expression can end up in negative.
Thank you for your time and effort.
version used: "redis-token-bucket-ratelimiter": "^0.5.1"
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();
}
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.