@@ -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