Skip to content

Commit

Permalink
feat: add configurable rate limiting (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
hughcrt authored Sep 26, 2024
1 parent a8d7b29 commit dc699cb
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/backend/src/utils/ratelimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import ratelimit from "koa-ratelimit";

const db = new Map();

const MAX_REQUESTS_PER_MINUTE = parseInt(
process.env.MAX_REQUESTS_PER_MINUTE || "150",
10,
);

export default ratelimit({
driver: "memory",
db: db,
Expand All @@ -13,17 +18,13 @@ export default ratelimit({
reset: "Rate-Limit-Reset",
total: "Rate-Limit-Total",
},
max: 150, // limit unlogged IPs to 50 requests per minute
max: MAX_REQUESTS_PER_MINUTE,
disableHeader: false,
whitelist: (ctx) => {
// don't limit logged in users
if (ctx.state.userId) return true;
return false;
// some logic that returns a boolean
},
// blacklist: (ctx) => {
// // some logic that returns a boolean
// },
});

export const aggressiveRatelimit = ratelimit({
Expand Down

0 comments on commit dc699cb

Please sign in to comment.