Skip to content

Commit d243365

Browse files
committed
updated rate limiter options. sliding window log setup
1 parent 00cbfc9 commit d243365

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/@types/rateLimit.d.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,22 @@ export type RateLimiterSelection =
4444
* @type {number} refillRate - Rate at which tokens are added to the bucket in seconds
4545
*/
4646
export interface TokenBucketOptions {
47+
typename: 'bucket';
4748
bucketSize: number;
4849
refillRate: number;
4950
}
5051

52+
/**
53+
* @type {number} windowSize - size of the window in milliseconds
54+
* @type {number} capacity - max number of tokens that can be used in the bucket
55+
*/
56+
export interface WindowOptions {
57+
typename: 'window';
58+
windowSize: number;
59+
capacity: number;
60+
}
61+
5162
// TODO: This will be a union type where we can specify Option types for other Rate Limiters
52-
// Record<string, never> represents the empty object for alogorithms that don't require settings
63+
// Record<string, never> represents the empty object for algorithms that don't require settings
5364
// and might be able to be removed in the future.
54-
export type RateLimiterOptions = TokenBucketOptions | Record<string, never>;
65+
export type RateLimiterOptions = TokenBucketOptions | WindowSize | Record<string, never>;

src/middleware/rateLimiterSetup.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Redis from 'ioredis';
22
import { RateLimiterOptions, RateLimiterSelection } from '../@types/rateLimit';
3+
import SlidingWindowLog from '../rateLimiters/slidingWindowLog';
34
import TokenBucket from '../rateLimiters/tokenBucket';
45

56
/**
@@ -18,24 +19,23 @@ export default function setupRateLimiter(
1819
) {
1920
switch (selection) {
2021
case 'TOKEN_BUCKET':
21-
// todo validate options
22-
return new TokenBucket(options.bucketSize, options.refillRate, client);
23-
break;
22+
if (options.typename === 'bucket') {
23+
return new TokenBucket(options.bucketSize, options.refillRate, client);
24+
}
25+
throw new Error('Invalid options for token bucket');
2426
case 'LEAKY_BUCKET':
2527
throw new Error('Leaky Bucket algonithm has not be implemented.');
26-
break;
2728
case 'FIXED_WINDOW':
2829
throw new Error('Fixed Window algonithm has not be implemented.');
29-
break;
3030
case 'SLIDING_WINDOW_LOG':
31-
throw new Error('Sliding Window Log has not be implemented.');
32-
break;
31+
if (options.typename === 'window') {
32+
return new SlidingWindowLog(options.windowSize, options.capacity, client);
33+
}
34+
throw new Error('Invalid options for sliding window log');
3335
case 'SLIDING_WINDOW_COUNTER':
3436
throw new Error('Sliding Window Counter algonithm has not be implemented.');
35-
break;
3637
default:
3738
// typescript should never let us invoke this function with anything other than the options above
3839
throw new Error('Selected rate limiting algorithm is not suppported');
39-
break;
4040
}
4141
}

0 commit comments

Comments
 (0)