-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.ts
80 lines (73 loc) · 2.19 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { Argument, program } from 'commander';
import { RateLimiterType } from './enums';
import { createRateLimiterServer } from './server';
import {
FixedWindowCounterArgs,
RateLimiterArgs,
RedisSlidingWindowCounterArgs,
SlidingWindowCounterArgs,
SlidingWindowLogArgs,
TokenBucketArgs
} from './types';
import Redlock from 'redlock';
import Client from 'ioredis';
program.addArgument(
new Argument(
'<algorithm>',
'The algorithm to use for the rate limiter'
).choices(Object.values(RateLimiterType))
);
program.option('-p, --port <port>', 'Port on which server will start', '8080');
program.option('--debug', 'Enable debugging');
program.parse();
const options = program.opts();
const rateLimiterType = program.args[0] as RateLimiterType;
const PORT = parseInt(options.port);
const DEBUG = options.debug;
// Change the following default data to use different configuration.
async function getRateLimiterArgs(
rateLimiterType: RateLimiterType
): Promise<RateLimiterArgs> {
switch (rateLimiterType) {
case RateLimiterType.TOKEN_BUCKET: {
const arg: TokenBucketArgs = { capacity: 10, timePeriodInMs: 1000 };
return arg;
}
case RateLimiterType.FIXED_WINDOW_COUNTER: {
const arg: FixedWindowCounterArgs = { threshold: 1 };
return arg;
}
case RateLimiterType.SLIDING_WINDOW_LOG: {
const arg: SlidingWindowLogArgs = { logThreshold: 1 };
return arg;
}
case RateLimiterType.SLIDING_WINDOW_COUNTER: {
const arg: SlidingWindowCounterArgs = { threshold: 1 };
return arg;
}
case RateLimiterType.REDIS_SLIDING_WINDOW_COUNTER: {
const client = new Client();
client.on('error', (err) => {
if (DEBUG) {
console.error(err);
}
});
const redlock = new Redlock([client], {
driftFactor: 0.01,
retryCount: 10,
retryDelay: 200,
retryJitter: 200
});
const args: RedisSlidingWindowCounterArgs = {
threshold: 1,
client,
redlock
};
return args;
}
}
}
(async () => {
const args: RateLimiterArgs = await getRateLimiterArgs(rateLimiterType);
createRateLimiterServer(rateLimiterType, args, PORT, DEBUG);
})();