-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfixed-window-counter.ts
67 lines (55 loc) · 1.64 KB
/
fixed-window-counter.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
import { NextFunction, Request, Response } from 'express';
import { FixedWindowCounterArgs, RateLimiter } from '../types';
interface Counter {
/**
* The start of the window.
*
* @type {number}
*/
window: number;
/**
* Number of requests received so far in the current window.
*
* @type {number}
*/
count: number;
}
export class FixedWindowCounterRateLimiter implements RateLimiter {
counters: Map<string, Counter>;
threshold: number;
constructor({ threshold }: FixedWindowCounterArgs) {
this.threshold = threshold;
this.counters = new Map<string, Counter>();
}
handleRequest(req: Request, res: Response, next: NextFunction) {
const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
// Make sure a valid IP is present in the request.
if (typeof ip !== 'string') {
res
.status(400)
.send('Invalid x-forwarded-for header or remote address\n');
return;
}
const counter = this.counters.get(ip);
const currentWindow = Math.floor(new Date().getTime() / 1000);
// If this is the first request from the given IP, or the window is changed
if (counter === undefined || counter.window != currentWindow) {
this.counters.set(ip, {
count: 1,
window: currentWindow
});
next();
return;
}
// Discard the request if the counter exceeds the threshold
if (counter.count >= this.threshold) {
res.status(429).send('Too many requests. Please try later!\n');
return;
}
// Otherwise increase the counter.
counter.count++;
this.counters.set(ip, counter);
next();
}
cleanup(): void {}
}