Skip to content

Commit 3f9ad17

Browse files
authored
Merge pull request #78 from oslabs-beta/jd/slideTest
SlidingWindowCounter testing suite
2 parents 5fe7133 + 13fab86 commit 3f9ad17

File tree

5 files changed

+658
-6
lines changed

5 files changed

+658
-6
lines changed

src/@types/rateLimit.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface RedisBucket {
2626
export interface RedisWindow {
2727
currentTokens: number;
2828
previousTokens: number;
29-
fixedWindowStart: number;
29+
fixedWindowStart?: number;
3030
}
3131

3232
export type RedisLog = RedisBucket[];

src/rateLimiters/slidingWindowCounter.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class SlidingWindowCounter implements RateLimiter {
2525

2626
/**
2727
* Create a new instance of a TokenBucket rate limiter that can be connected to any database store
28-
* @param windowSize size of each window in milliseconds (fixed and rolling)
29-
* @param capacity max capacity of tokens allowed per fixed window
30-
* @param client redis client where rate limiter will cache information
28+
* @param windowSize - size of each window in milliseconds (fixed and rolling)
29+
* @param capacity - max capacity of tokens allowed per fixed window
30+
* @param client - redis client where rate limiter will cache information
3131
*/
3232
constructor(windowSize: number, capacity: number, client: Redis) {
3333
this.windowSize = windowSize;
@@ -38,7 +38,29 @@ class SlidingWindowCounter implements RateLimiter {
3838
}
3939

4040
/**
41+
* @function processRequest - current timestamp and number of tokens required for
42+
* the request to go through are passed in. We first check if a window exists in the redis
43+
* cache.
4144
*
45+
* If not, then fixedWindowStart is set as the current timestamp, and currentTokens
46+
* is checked against capacity. If we have enough capacity for the request, we return
47+
* success as true and tokens as how many tokens remain in the current fixed window.
48+
*
49+
* If a window does exist in the cache, we first check if the timestamp is greater than
50+
* the fixedWindowStart + windowSize.
51+
*
52+
* If it isn't then we check the number of tokens in the arguments as well as in the cache
53+
* against the capacity and return success or failure from there while updating the cache.
54+
*
55+
* If the timestamp is over the windowSize beyond the fixedWindowStart, then we update fixedWindowStart
56+
* to be fixedWindowStart + windowSize (to create a new fixed window) and
57+
* make previousTokens = currentTokens, and currentTokens equal to the number of tokens in args, if
58+
* not over capacity.
59+
*
60+
* Once previousTokens is not null, we then run functionality using the rolling window to compute
61+
* the formula this entire limiting algorithm is distinguished by:
62+
*
63+
* currentTokens + previousTokens * overlap % of rolling window over previous fixed window
4264
*
4365
* @param {string} uuid - unique identifer used to throttle requests
4466
* @param {number} timestamp - time the request was recieved

0 commit comments

Comments
 (0)