|
| 1 | +import Redis from 'ioredis'; |
| 2 | +import { RateLimiter, RateLimiterResponse } from '../@types/rateLimit'; |
| 3 | + |
| 4 | +/** |
| 5 | + * The SlidingWindowLog instance of a RateLimiter limits requests based on a unique user ID. |
| 6 | + * With the FixedWindow algorithm, users are able to send more requests to go through at the |
| 7 | + * edges of a window. The SlidingWindowLog algorithm addresses this issue by tracking request |
| 8 | + * timestamps in a log then removing these requests from the log once they fall outside of the window. |
| 9 | + * If a request is received and there are more than capacity requests in the log then the request is dropped |
| 10 | + * |
| 11 | + * Whenever a user makes a request the following steps are performed: |
| 12 | + * 1. The user's log is obtained from redis. |
| 13 | + * 2. Any requests that are older than window size are dropped from the log. |
| 14 | + * 3. The complexity of the current request is added to the complexity of all requests in the log. |
| 15 | + * 4. If the request exceeds the specified capacity it is dropped. |
| 16 | + * 5. Otherwise the request is allowed and ther current request is added to the log. |
| 17 | + */ |
| 18 | +class SlidingWindowLog implements RateLimiter { |
| 19 | + private windowSize: number; |
| 20 | + |
| 21 | + private capacity: number; |
| 22 | + |
| 23 | + private client: Redis; |
| 24 | + |
| 25 | + /** |
| 26 | + * Create a new instance of a SlidingWindowLog rate limiter that can be connected to any redis store |
| 27 | + * @param windowSize size of window in milliseconds |
| 28 | + * @param capacity max number of tokens allowed in each window |
| 29 | + * @param client redis client where rate limiter will cache information |
| 30 | + */ |
| 31 | + constructor(windowSize: number, capacity: number, client: Redis) { |
| 32 | + this.windowSize = windowSize; |
| 33 | + this.capacity = capacity; |
| 34 | + this.client = client; |
| 35 | + if (windowSize <= 0 || capacity <= 0) |
| 36 | + throw SyntaxError('SlidingWindowLog windowSize and capacity must be positive'); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param {string} uuid - unique identifer used to throttle requests |
| 41 | + * @param {number} timestamp - time the request was recieved |
| 42 | + * @param {number} [tokens=1] - complexity of the query for throttling requests |
| 43 | + * @return {*} {Promise<RateLimiterResponse>} |
| 44 | + * @memberof SlidingWindowLog |
| 45 | + */ |
| 46 | + async processRequest( |
| 47 | + uuid: string, |
| 48 | + timestamp: number, |
| 49 | + tokens = 1 |
| 50 | + ): Promise<RateLimiterResponse> { |
| 51 | + // set the expiry of key-value pairs in the cache to 24 hours |
| 52 | + const keyExpiry = 86400000; // TODO: Make this a global for consistency across each algo. |
| 53 | + if (tokens > this.capacity) return { success: false, tokens: this.capacity }; |
| 54 | + |
| 55 | + throw new Error('SlidingWindowLog.processRequest not implemented'); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Resets the rate limiter to the intial state by clearing the redis store. |
| 60 | + */ |
| 61 | + public reset(): void { |
| 62 | + this.client.flushall(); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +export default SlidingWindowLog; |
0 commit comments