-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import asyncHandler from "express-async-handler"; | ||
import Redis from "ioredis"; | ||
|
||
const redisUrl = process.env.REDIS_URL; | ||
if (!redisUrl) { | ||
throw new Error("REDIS_URL is not defined"); | ||
} | ||
const redis = new Redis(redisUrl); | ||
const MAX_REQUESTS = 100; | ||
const WINDOW_SIZE_IN_SECONDS = 60; | ||
|
||
const rateLimiter = asyncHandler( | ||
//@ts-ignore | ||
async (req: Request, res: Response, next: NextFunction) => { | ||
//@ts-ignore | ||
const clientIp = req.user.user_id; | ||
// console.log(clientIp); | ||
const key = `rate-limit:${clientIp}`; | ||
const requestCount = await redis.get(key); | ||
|
||
if (requestCount && parseInt(requestCount) >= MAX_REQUESTS) { | ||
return res | ||
.status(429) | ||
.json({ message: "Too many requests, please try again later." }); | ||
} | ||
|
||
const ttl = await redis.ttl(key); | ||
|
||
if (ttl < 0) { | ||
await redis.set(key, 1, "EX", WINDOW_SIZE_IN_SECONDS); | ||
} else { | ||
await redis.incr(key); | ||
} | ||
|
||
next(); | ||
} | ||
); | ||
|
||
export default rateLimiter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters