-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (55 loc) · 1.7 KB
/
index.js
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
import express from "express";
import TokenBucket from "./token-bucket.js";
import FixedWindowCounter from "./fixed-window-counter.js";
import SlidingWindowLog from "./sliding-window-log.js";
import SlidingWindowCounter from "./sliding-window-counter.js";
import { client } from "./redis.js";
const app = express();
const port = process.env.PORT || 3000;
const bucket = new TokenBucket(10, 1, 1);
const fixedWindowCounter = new FixedWindowCounter(60, 60);
const slidingWindowLog = new SlidingWindowLog(60, 60);
const slidingWindowCounter = new SlidingWindowCounter(60, 60, client);
app.listen(port, () => {
console.log(`Rate limiter listening on port ${port}`);
});
app.get("/", (req, res) => {
res.send("Health check OKK!");
});
app.get("/token-bucket", (req, res) => {
const ipAddress = req.headers["x-forwarded-for"] || req.socket.remoteAddress;
const result = bucket.handleRequest(ipAddress);
if (!result) {
res.status(429);
res.send("Limited, don't over use me!");
} else {
res.send("Success");
}
});
app.get("/fixed-window-counter", (req, res) => {
const result = fixedWindowCounter.handleRequest();
if (!result) {
res.status(429);
res.send("Limited, don't over use me!");
} else {
res.send("Success");
}
});
app.get("/sliding-window-log", (req, res) => {
const result = slidingWindowLog.handleRequest();
if (!result) {
res.status(429);
res.send("Limited, don't over use me!");
} else {
res.send("Success");
}
});
app.get("/sliding-window-counter", async (req, res) => {
const result = await slidingWindowCounter.handleRequest();
if (!result) {
res.status(429);
res.send("Limited, don't over use me!");
} else {
res.send("Success");
}
});