Implement per-team API rate limiting with Redis (#164)
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
import TTLCache from "@isaacs/ttlcache";
|
||||
import { Context } from "hono";
|
||||
import { db } from "../db";
|
||||
import { UnsendApiError } from "./api-error";
|
||||
import { env } from "~/env";
|
||||
import { getTeamAndApiKey } from "../service/api-service";
|
||||
|
||||
const rateLimitCache = new TTLCache({
|
||||
ttl: 1000, // 1 second
|
||||
max: 10000,
|
||||
});
|
||||
import { isSelfHosted } from "~/utils/common";
|
||||
|
||||
/**
|
||||
* Gets the team from the token. Also will check if the token is valid.
|
||||
@@ -32,8 +26,6 @@ export const getTeamFromToken = async (c: Context) => {
|
||||
});
|
||||
}
|
||||
|
||||
checkRateLimit(token);
|
||||
|
||||
const teamAndApiKey = await getTeamAndApiKey(token);
|
||||
|
||||
if (!teamAndApiKey) {
|
||||
@@ -66,18 +58,3 @@ export const getTeamFromToken = async (c: Context) => {
|
||||
|
||||
return { ...team, apiKeyId: apiKey.id };
|
||||
};
|
||||
|
||||
const checkRateLimit = (token: string) => {
|
||||
let rateLimit = rateLimitCache.get<number>(token);
|
||||
|
||||
rateLimit = rateLimit ?? 0;
|
||||
|
||||
if (rateLimit >= env.API_RATE_LIMIT) {
|
||||
throw new UnsendApiError({
|
||||
code: "RATE_LIMITED",
|
||||
message: `Rate limit exceeded, ${env.API_RATE_LIMIT} requests per second`,
|
||||
});
|
||||
}
|
||||
|
||||
rateLimitCache.set(token, rateLimit + 1);
|
||||
};
|
||||
|
Reference in New Issue
Block a user