Skip to content

Commit

Permalink
#172 Merge pull request #173 from MiniKraken-Team/fix-negative-remain…
Browse files Browse the repository at this point in the history
…ing-response-value

fix for negative remaining values
  • Loading branch information
animir authored Jul 29, 2022
2 parents 76b94c0 + 1ef2438 commit b271676
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/RateLimiterMemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RateLimiterMemory extends RateLimiterAbstract {
return new Promise((resolve) => {
const secDuration = this._getKeySecDuration(options);
const res = this._memoryStorage.incrby(rlKey, points, secDuration);
res.remainingPoints = this.points - res.consumedPoints;
res.remainingPoints = Math.max(this.points - res.consumedPoints, 0);
resolve(res);
});
}
Expand All @@ -58,7 +58,7 @@ class RateLimiterMemory extends RateLimiterAbstract {
return new Promise((resolve) => {
const secDuration = this._getKeySecDuration(options);
const res = this._memoryStorage.incrby(rlKey, -points, secDuration);
res.remainingPoints = this.points - res.consumedPoints;
res.remainingPoints = Math.max(this.points - res.consumedPoints, 0);
resolve(res);
});
}
Expand Down Expand Up @@ -91,7 +91,7 @@ class RateLimiterMemory extends RateLimiterAbstract {
get(key) {
const res = this._memoryStorage.get(this.getKey(key));
if (res !== null) {
res.remainingPoints = this.points - res.consumedPoints;
res.remainingPoints = Math.max(this.points - res.consumedPoints, 0);
}

return Promise.resolve(res);
Expand Down
1 change: 1 addition & 0 deletions test/RateLimiterMemory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ describe('RateLimiterMemory with fixed window', function RateLimiterMemoryTest()
rateLimiter.get(testKey)
.then((res) => {
expect(res.consumedPoints).to.equal(12);
expect(res.remainingPoints).to.equal(0);
done();
});
})
Expand Down

0 comments on commit b271676

Please sign in to comment.