Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
11835d3
beginning test framework
jondeweydev Jul 8, 2022
ddddcbd
initial test framework updates
jondeweydev Jul 9, 2022
e3f3cac
merged
jondeweydev Jul 9, 2022
e13de2a
testing pseudocode complete
jondeweydev Jul 10, 2022
606823f
pseudocode converted to completed test skeleton
jondeweydev Jul 10, 2022
6ff5e8c
test updates
jondeweydev Jul 14, 2022
b4877a6
blocked request tests complete
jondeweydev Jul 14, 2022
41e718e
moved types folder & slidingWindowCounter tests are complete
jondeweydev Jul 14, 2022
d612dab
put types back in src
jondeweydev Jul 14, 2022
18255c8
skipping all tests to pass travis PR
jondeweydev Jul 14, 2022
581307c
Merge branch 'dev' of https://github.com/oslabs-beta/GraphQL-Gate int…
jondeweydev Jul 15, 2022
a950262
addressed comments in PR
jondeweydev Jul 15, 2022
9537d0f
Merge branch 'dev' into jd/slideTest
jondeweydev Jul 20, 2022
1dbd7e0
removed null as a type of previousTokens
jondeweydev Jul 20, 2022
505da80
further test updates
jondeweydev Jul 20, 2022
9c87146
updated to reflect proportion flooring
jondeweydev Jul 20, 2022
ad54fda
added edge tests for proportions
jondeweydev Jul 20, 2022
3f11c8e
Merge branch 'dev' into jd/slideTest
jondeweydev Jul 20, 2022
9e4d7a2
added test for skipped fixed window
jondeweydev Jul 20, 2022
33c116e
Merge branch 'dev' of https://github.com/oslabs-beta/GraphQL-Gate int…
jondeweydev Jul 20, 2022
3e23f96
Merge branch 'jd/slideTest' of https://github.com/oslabs-beta/GraphQL…
jondeweydev Jul 20, 2022
13fab86
updated name
jondeweydev Jul 20, 2022
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
2 changes: 1 addition & 1 deletion src/@types/rateLimit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface RedisBucket {
export interface RedisWindow {
currentTokens: number;
previousTokens: number;
fixedWindowStart: number;
fixedWindowStart?: number;
}

export type RedisLog = RedisBucket[];
Expand Down
28 changes: 25 additions & 3 deletions src/rateLimiters/slidingWindowCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class SlidingWindowCounter implements RateLimiter {

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

/**
* @function processRequest - current timestamp and number of tokens required for
* the request to go through are passed in. We first check if a window exists in the redis
* cache.
*
* If not, then fixedWindowStart is set as the current timestamp, and currentTokens
* is checked against capacity. If we have enough capacity for the request, we return
* success as true and tokens as how many tokens remain in the current fixed window.
*
* If a window does exist in the cache, we first check if the timestamp is greater than
* the fixedWindowStart + windowSize.
*
* If it isn't then we check the number of tokens in the arguments as well as in the cache
* against the capacity and return success or failure from there while updating the cache.
*
* If the timestamp is over the windowSize beyond the fixedWindowStart, then we update fixedWindowStart
* to be fixedWindowStart + windowSize (to create a new fixed window) and
* make previousTokens = currentTokens, and currentTokens equal to the number of tokens in args, if
* not over capacity.
*
* Once previousTokens is not null, we then run functionality using the rolling window to compute
* the formula this entire limiting algorithm is distinguished by:
*
* currentTokens + previousTokens * overlap % of rolling window over previous fixed window
*
* @param {string} uuid - unique identifer used to throttle requests
* @param {number} timestamp - time the request was recieved
Expand Down
Loading