Files
firecrawl/apps/api/src/services/rate-limiter.ts
T

78 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-04-15 17:01:47 -04:00
import { RateLimiterRedis } from "rate-limiter-flexible";
2025-04-10 18:49:23 +02:00
import { RateLimiterMode } from "../types";
2024-07-16 22:41:13 -04:00
import Redis from "ioredis";
2025-04-10 18:49:23 +02:00
import type { AuthCreditUsageChunk } from "../controllers/v1/types";
2024-04-20 14:02:22 -07:00
2024-07-16 22:41:13 -04:00
export const redisRateLimitClient = new Redis(
2024-12-11 19:51:08 -03:00
process.env.REDIS_RATE_LIMIT_URL!,
2024-12-11 19:46:11 -03:00
);
2024-04-15 17:01:47 -04:00
2024-06-06 11:23:10 -07:00
const createRateLimiter = (keyPrefix, points) =>
new RateLimiterRedis({
2024-07-16 22:41:13 -04:00
storeClient: redisRateLimitClient,
2024-06-06 11:23:10 -07:00
keyPrefix,
points,
2024-12-11 19:51:08 -03:00
duration: 60, // Duration in seconds
2024-06-06 11:23:10 -07:00
});
2024-04-15 17:01:47 -04:00
2024-05-08 15:14:39 -07:00
export const testSuiteRateLimiter = new RateLimiterRedis({
2024-07-16 22:41:13 -04:00
storeClient: redisRateLimitClient,
2024-05-14 14:26:42 -07:00
keyPrefix: "test-suite",
2024-05-19 12:23:34 -07:00
points: 10000,
2024-12-11 19:51:08 -03:00
duration: 60, // Duration in seconds
2024-05-08 15:14:39 -07:00
});
2024-04-15 17:01:47 -04:00
2025-04-10 18:49:23 +02:00
// TODO: PUT OVERRIDES FOR THESE INTO THE DB - mogery
2024-10-08 15:11:08 -03:00
const testSuiteTokens = [
"a01ccae",
"6254cf9",
"0f96e673",
"23befa1b",
"69141c4",
"48f9a97",
"5dc70ad",
"e5e60e5",
"65181ba",
"77c85b7",
"8567275",
"6c46abb",
"cb0ff78",
"fd769b2",
2025-02-20 22:31:43 -03:00
// "4c2638d",
2024-10-30 17:19:37 -03:00
"cbb3462", // don't remove (s-ai)
2024-12-11 19:51:08 -03:00
"824abcd", // don't remove (s-ai)
2024-12-30 21:42:01 -03:00
"0966288",
2025-02-20 22:31:43 -03:00
"226556f",
"0a18c9e", // gh
2024-10-08 15:11:08 -03:00
];
2024-09-11 14:03:34 -04:00
2025-04-10 18:49:23 +02:00
// TODO: PUT OVERRIDES FOR THESE INTO THE DB - mogery
// const manual_growth = ["22a07b64-cbfe-4924-9273-e3f01709cdf2"];
// const manual = ["69be9e74-7624-4990-b20d-08e0acc70cf6", "9661a311-3d75-45d2-bb70-71004d995873"];
// const manual_etier2c = ["77545e01-9cec-4fa9-8356-883fc66ac13e", "778c62c4-306f-4039-b372-eb20174760c0"];
const fallbackRateLimits: AuthCreditUsageChunk["rate_limits"] = {
crawl: 15,
scrape: 100,
search: 100,
map: 100,
extract: 100,
preview: 25,
extractStatus: 25000,
crawlStatus: 25000,
};
2024-10-01 16:04:39 -03:00
export function getRateLimiter(
mode: RateLimiterMode,
2025-04-10 18:49:23 +02:00
rate_limits: AuthCreditUsageChunk["rate_limits"] | null,
2024-12-11 19:46:11 -03:00
): RateLimiterRedis {
return createRateLimiter(
2025-04-10 18:49:23 +02:00
`${mode}`,
(rate_limits ?? fallbackRateLimits)[mode] ?? 500,
2024-12-11 19:46:11 -03:00
);
2024-04-15 17:01:47 -04:00
}
export function isTestSuiteToken(token: string): boolean {
return testSuiteTokens.some((testToken) => token.includes(testToken));
}