Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 2 bugs for redis: ttl -1 and X-RateLimit-Reset #39

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix 2 bug:
1,"dateEnd===-1" may happen just after ttl expires then "await this.client.incrBy(key, weight)" will set ttl to -1; we count this low probability requests as the previous period and check "dateEnd===-1" for the next request

2,add "dateEnd = Date.now() + dateEnd * 1000;" the unit of Date.now() is millisecond
  • Loading branch information
codeHui committed Jan 6, 2022
commit 5c11267c6c5d2750b755acd58b7c9e8198e5f139
7 changes: 5 additions & 2 deletions src/RedisStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,18 @@ class RedisStore extends Store {
async _hit(key, options, weight) {

let [counter, dateEnd] = await this.client.multi().get(key).ttl(key).exec();

if(counter === null) {

//check "dateEnd===-1", the reason is below
if(counter === null || dateEnd===-1) {
counter = weight;
dateEnd = Date.now() + options.interval;

const seconds = Math.ceil(options.interval / 1000);
await this.client.setEx(key, seconds.toString(), counter.toString());
}else {
//"dateEnd===-1" may happen just after ttl expires then "await this.client.incrBy(key, weight)" will set ttl to -1; we count this low probability requests as the previous period and check "dateEnd===-1" for the next request
counter = await this.client.incrBy(key, weight);
dateEnd = Date.now() + dateEnd * 1000;
}

return {
Expand Down