mirror of
https://git.mirrors.martin98.com/https://github.com/mendableai/firecrawl
synced 2025-08-15 01:45:59 +08:00
Nick: redlock cache in auth
This commit is contained in:
parent
5a778f2c22
commit
25a899eae3
@ -7,7 +7,15 @@ import { RateLimiterRedis } from "rate-limiter-flexible";
|
|||||||
import { setTraceAttributes } from '@hyperdx/node-opentelemetry';
|
import { setTraceAttributes } from '@hyperdx/node-opentelemetry';
|
||||||
import { sendNotification } from "../services/notification/email_notification";
|
import { sendNotification } from "../services/notification/email_notification";
|
||||||
import { Logger } from "../lib/logger";
|
import { Logger } from "../lib/logger";
|
||||||
|
import { redlock } from "../../src/services/redlock";
|
||||||
|
import { getValue } from "../../src/services/redis";
|
||||||
|
import { setValue } from "../../src/services/redis";
|
||||||
|
import { validate } from 'uuid';
|
||||||
|
|
||||||
|
function normalizedApiIsUuid(potentialUuid: string): boolean {
|
||||||
|
// Check if the string is a valid UUID
|
||||||
|
return validate(potentialUuid);
|
||||||
|
}
|
||||||
export async function authenticateUser(req, res, mode?: RateLimiterMode): Promise<AuthResponse> {
|
export async function authenticateUser(req, res, mode?: RateLimiterMode): Promise<AuthResponse> {
|
||||||
return withAuth(supaAuthenticateUser)(req, res, mode);
|
return withAuth(supaAuthenticateUser)(req, res, mode);
|
||||||
}
|
}
|
||||||
@ -54,17 +62,72 @@ export async function supaAuthenticateUser(
|
|||||||
let subscriptionData: { team_id: string, plan: string } | null = null;
|
let subscriptionData: { team_id: string, plan: string } | null = null;
|
||||||
let normalizedApi: string;
|
let normalizedApi: string;
|
||||||
|
|
||||||
let team_id: string;
|
let cacheKey= "";
|
||||||
|
let redLockKey = "";
|
||||||
|
const lockTTL = 5000; // 5 seconds
|
||||||
|
let teamId: string | null = null;
|
||||||
|
let priceId: string | null = null;
|
||||||
|
|
||||||
if (token == "this_is_just_a_preview_token") {
|
if (token == "this_is_just_a_preview_token") {
|
||||||
rateLimiter = getRateLimiter(RateLimiterMode.Preview, token);
|
rateLimiter = getRateLimiter(RateLimiterMode.Preview, token);
|
||||||
team_id = "preview";
|
teamId = "preview";
|
||||||
} else {
|
} else {
|
||||||
normalizedApi = parseApi(token);
|
normalizedApi = parseApi(token);
|
||||||
|
if(!normalizedApiIsUuid(normalizedApi)){
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Unauthorized: Invalid token",
|
||||||
|
status: 401,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
cacheKey = `api_key:${normalizedApi}`;
|
||||||
|
redLockKey = `redlock:${cacheKey}`;
|
||||||
|
|
||||||
|
try{
|
||||||
|
const lock = await redlock.acquire([redLockKey], lockTTL);
|
||||||
|
|
||||||
const { data, error } = await supabase_service.rpc(
|
try{
|
||||||
'get_key_and_price_id_2', { api_key: normalizedApi }
|
|
||||||
);
|
const teamIdPriceId = await getValue(cacheKey);
|
||||||
|
if(teamIdPriceId){
|
||||||
|
const { team_id, price_id } = JSON.parse(teamIdPriceId);
|
||||||
|
teamId = team_id;
|
||||||
|
priceId = price_id;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
const { data, error } = await supabase_service.rpc(
|
||||||
|
'get_key_and_price_id_2', { api_key: normalizedApi }
|
||||||
|
);
|
||||||
|
if(error){
|
||||||
|
Logger.error(`RPC ERROR (get_key_and_price_id_2): ${error.message}`);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "The server seems overloaded. Please contact hello@firecrawl.com if you aren't sending too many requests at once.",
|
||||||
|
status: 500,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (!data || data.length === 0) {
|
||||||
|
Logger.warn(`Error fetching api key: ${error.message} or data is empty`);
|
||||||
|
// TODO: change this error code ?
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: "Unauthorized: Invalid token",
|
||||||
|
status: 401,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
teamId = data[0].team_id;
|
||||||
|
priceId = data[0].price_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}finally{
|
||||||
|
await lock.release();
|
||||||
|
}
|
||||||
|
}catch(error){
|
||||||
|
Logger.error(`Error acquiring the rate limiter lock: ${error}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// get_key_and_price_id_2 rpc definition:
|
// get_key_and_price_id_2 rpc definition:
|
||||||
// create or replace function get_key_and_price_id_2(api_key uuid)
|
// create or replace function get_key_and_price_id_2(api_key uuid)
|
||||||
// returns table(key uuid, team_id uuid, price_id text) as $$
|
// returns table(key uuid, team_id uuid, price_id text) as $$
|
||||||
@ -82,30 +145,12 @@ export async function supaAuthenticateUser(
|
|||||||
// end;
|
// end;
|
||||||
// $$ language plpgsql;
|
// $$ language plpgsql;
|
||||||
|
|
||||||
if (error) {
|
|
||||||
Logger.warn(`Error fetching key and price_id: ${error.message}`);
|
|
||||||
} else {
|
|
||||||
// console.log('Key and Price ID:', data);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const plan = getPlanByPriceId(priceId);
|
||||||
|
|
||||||
if (error || !data || data.length === 0) {
|
|
||||||
Logger.warn(`Error fetching api key: ${error.message} or data is empty`);
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
error: "Unauthorized: Invalid token",
|
|
||||||
status: 401,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
const internal_team_id = data[0].team_id;
|
|
||||||
team_id = internal_team_id;
|
|
||||||
|
|
||||||
const plan = getPlanByPriceId(data[0].price_id);
|
|
||||||
// HyperDX Logging
|
// HyperDX Logging
|
||||||
setTrace(team_id, normalizedApi);
|
setTrace(teamId, normalizedApi);
|
||||||
subscriptionData = {
|
subscriptionData = {
|
||||||
team_id: team_id,
|
team_id: teamId,
|
||||||
plan: plan
|
plan: plan
|
||||||
}
|
}
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
@ -134,7 +179,7 @@ export async function supaAuthenticateUser(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const team_endpoint_token = token === "this_is_just_a_preview_token" ? iptoken : team_id;
|
const team_endpoint_token = token === "this_is_just_a_preview_token" ? iptoken : teamId;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await rateLimiter.consume(team_endpoint_token);
|
await rateLimiter.consume(team_endpoint_token);
|
||||||
@ -147,7 +192,13 @@ export async function supaAuthenticateUser(
|
|||||||
const startDate = new Date();
|
const startDate = new Date();
|
||||||
const endDate = new Date();
|
const endDate = new Date();
|
||||||
endDate.setDate(endDate.getDate() + 7);
|
endDate.setDate(endDate.getDate() + 7);
|
||||||
|
|
||||||
// await sendNotification(team_id, NotificationType.RATE_LIMIT_REACHED, startDate.toISOString(), endDate.toISOString());
|
// await sendNotification(team_id, NotificationType.RATE_LIMIT_REACHED, startDate.toISOString(), endDate.toISOString());
|
||||||
|
// TODO: cache 429 for a few minuts
|
||||||
|
if(teamId && priceId && mode !== RateLimiterMode.Preview){
|
||||||
|
await setValue(cacheKey, JSON.stringify({team_id: teamId, price_id: priceId}), 60 * 5);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: `Rate limit exceeded. Consumed points: ${rateLimiterRes.consumedPoints}, Remaining points: ${rateLimiterRes.remainingPoints}. Upgrade your plan at https://firecrawl.dev/pricing for increased rate limits or please retry after ${secs}s, resets at ${retryDate}`,
|
error: `Rate limit exceeded. Consumed points: ${rateLimiterRes.consumedPoints}, Remaining points: ${rateLimiterRes.remainingPoints}. Upgrade your plan at https://firecrawl.dev/pricing for increased rate limits or please retry after ${secs}s, resets at ${retryDate}`,
|
||||||
|
@ -4,37 +4,12 @@ import { sendNotification } from "../notification/email_notification";
|
|||||||
import { supabase_service } from "../supabase";
|
import { supabase_service } from "../supabase";
|
||||||
import { Logger } from "../../lib/logger";
|
import { Logger } from "../../lib/logger";
|
||||||
import { getValue, setValue } from "../redis";
|
import { getValue, setValue } from "../redis";
|
||||||
import Redlock from "redlock";
|
import { redlock } from "../redlock";
|
||||||
import Client from "ioredis";
|
|
||||||
|
|
||||||
const FREE_CREDITS = 500;
|
const FREE_CREDITS = 500;
|
||||||
|
|
||||||
const redlock = new Redlock(
|
|
||||||
// You should have one client for each independent redis node
|
|
||||||
// or cluster.
|
|
||||||
[new Client(process.env.REDIS_RATE_LIMIT_URL)],
|
|
||||||
{
|
|
||||||
// The expected clock drift; for more details see:
|
|
||||||
// http://redis.io/topics/distlock
|
|
||||||
driftFactor: 0.01, // multiplied by lock ttl to determine drift time
|
|
||||||
|
|
||||||
// The max number of times Redlock will attempt to lock a resource
|
|
||||||
// before erroring.
|
|
||||||
retryCount: 5,
|
|
||||||
|
|
||||||
// the time in ms between attempts
|
|
||||||
retryDelay: 100, // time in ms
|
|
||||||
|
|
||||||
// the max time in ms randomly added to retries
|
|
||||||
// to improve performance under high contention
|
|
||||||
// see https://www.awsarchitectureblog.com/2015/03/backoff.html
|
|
||||||
retryJitter: 200, // time in ms
|
|
||||||
|
|
||||||
// The minimum remaining time on a lock before an extension is automatically
|
|
||||||
// attempted with the `using` API.
|
|
||||||
automaticExtensionThreshold: 500, // time in ms
|
|
||||||
}
|
|
||||||
);
|
|
||||||
export async function billTeam(team_id: string, credits: number) {
|
export async function billTeam(team_id: string, credits: number) {
|
||||||
return withAuth(supaBillTeam)(team_id, credits);
|
return withAuth(supaBillTeam)(team_id, credits);
|
||||||
}
|
}
|
||||||
|
29
apps/api/src/services/redlock.ts
Normal file
29
apps/api/src/services/redlock.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import Redlock from "redlock";
|
||||||
|
import Client from "ioredis";
|
||||||
|
|
||||||
|
export const redlock = new Redlock(
|
||||||
|
// You should have one client for each independent redis node
|
||||||
|
// or cluster.
|
||||||
|
[new Client(process.env.REDIS_RATE_LIMIT_URL)],
|
||||||
|
{
|
||||||
|
// The expected clock drift; for more details see:
|
||||||
|
// http://redis.io/topics/distlock
|
||||||
|
driftFactor: 0.01, // multiplied by lock ttl to determine drift time
|
||||||
|
|
||||||
|
// The max number of times Redlock will attempt to lock a resource
|
||||||
|
// before erroring.
|
||||||
|
retryCount: 5,
|
||||||
|
|
||||||
|
// the time in ms between attempts
|
||||||
|
retryDelay: 100, // time in ms
|
||||||
|
|
||||||
|
// the max time in ms randomly added to retries
|
||||||
|
// to improve performance under high contention
|
||||||
|
// see https://www.awsarchitectureblog.com/2015/03/backoff.html
|
||||||
|
retryJitter: 200, // time in ms
|
||||||
|
|
||||||
|
// The minimum remaining time on a lock before an extension is automatically
|
||||||
|
// attempted with the `using` API.
|
||||||
|
automaticExtensionThreshold: 500, // time in ms
|
||||||
|
}
|
||||||
|
);
|
@ -36,9 +36,9 @@ class FirecrawlApp {
|
|||||||
* @param {Params | null} params - Additional parameters for the scrape request.
|
* @param {Params | null} params - Additional parameters for the scrape request.
|
||||||
* @returns {Promise<ScrapeResponse>} The response from the scrape operation.
|
* @returns {Promise<ScrapeResponse>} The response from the scrape operation.
|
||||||
*/
|
*/
|
||||||
scrapeUrl(url, params = null) {
|
scrapeUrl(url_1) {
|
||||||
var _a;
|
return __awaiter(this, arguments, void 0, function* (url, params = null) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
var _a;
|
||||||
const headers = {
|
const headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${this.apiKey}`,
|
Authorization: `Bearer ${this.apiKey}`,
|
||||||
@ -79,8 +79,8 @@ class FirecrawlApp {
|
|||||||
* @param {Params | null} params - Additional parameters for the search request.
|
* @param {Params | null} params - Additional parameters for the search request.
|
||||||
* @returns {Promise<SearchResponse>} The response from the search operation.
|
* @returns {Promise<SearchResponse>} The response from the search operation.
|
||||||
*/
|
*/
|
||||||
search(query, params = null) {
|
search(query_1) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, arguments, void 0, function* (query, params = null) {
|
||||||
const headers = {
|
const headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${this.apiKey}`,
|
Authorization: `Bearer ${this.apiKey}`,
|
||||||
@ -119,8 +119,8 @@ class FirecrawlApp {
|
|||||||
* @param {string} idempotencyKey - Optional idempotency key for the request.
|
* @param {string} idempotencyKey - Optional idempotency key for the request.
|
||||||
* @returns {Promise<CrawlResponse | any>} The response from the crawl operation.
|
* @returns {Promise<CrawlResponse | any>} The response from the crawl operation.
|
||||||
*/
|
*/
|
||||||
crawlUrl(url, params = null, waitUntilDone = true, pollInterval = 2, idempotencyKey) {
|
crawlUrl(url_1) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, arguments, void 0, function* (url, params = null, waitUntilDone = true, pollInterval = 2, idempotencyKey) {
|
||||||
const headers = this.prepareHeaders(idempotencyKey);
|
const headers = this.prepareHeaders(idempotencyKey);
|
||||||
let jsonData = { url };
|
let jsonData = { url };
|
||||||
if (params) {
|
if (params) {
|
||||||
|
4
apps/js-sdk/firecrawl/package-lock.json
generated
4
apps/js-sdk/firecrawl/package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@mendable/firecrawl-js",
|
"name": "@mendable/firecrawl-js",
|
||||||
"version": "0.0.34",
|
"version": "0.0.36",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@mendable/firecrawl-js",
|
"name": "@mendable/firecrawl-js",
|
||||||
"version": "0.0.34",
|
"version": "0.0.36",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.6.8",
|
"axios": "^1.6.8",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mendable/firecrawl-js",
|
"name": "@mendable/firecrawl-js",
|
||||||
"version": "0.0.35",
|
"version": "0.0.36",
|
||||||
"description": "JavaScript SDK for Firecrawl API",
|
"description": "JavaScript SDK for Firecrawl API",
|
||||||
"main": "build/cjs/index.js",
|
"main": "build/cjs/index.js",
|
||||||
"types": "types/index.d.ts",
|
"types": "types/index.d.ts",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user