Nick: removed redlock

This commit is contained in:
Nicolas
2024-08-12 15:07:30 -04:00
parent 0bd1a820ee
commit 74a5125185
2 changed files with 89 additions and 47 deletions
+79 -39
View File
@@ -37,6 +37,42 @@ function setTrace(team_id: string, api_key: string) {
Logger.error(`Error setting trace attributes: ${error.message}`); Logger.error(`Error setting trace attributes: ${error.message}`);
} }
} }
async function getKeyAndPriceId(normalizedApi: string): Promise<{
success: boolean;
teamId?: string;
priceId?: string;
error?: string;
status?: number;
}> {
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 {
return {
success: true,
teamId: data[0].team_id,
priceId: data[0].price_id,
};
}
}
export async function supaAuthenticateUser( export async function supaAuthenticateUser(
req, req,
res, res,
@@ -71,7 +107,7 @@ export async function supaAuthenticateUser(
let cacheKey = ""; let cacheKey = "";
let redLockKey = ""; let redLockKey = "";
const lockTTL = 5000; // 5 seconds const lockTTL = 15000; // 10 seconds
let teamId: string | null = null; let teamId: string | null = null;
let priceId: string | null = null; let priceId: string | null = null;
@@ -87,11 +123,8 @@ export async function supaAuthenticateUser(
status: 401, status: 401,
}; };
} }
cacheKey = `api_key:${normalizedApi}`;
redLockKey = `redlock:${cacheKey}`;
try { cacheKey = `api_key:${normalizedApi}`;
const lock = await redlock.acquire([redLockKey], lockTTL);
try { try {
const teamIdPriceId = await getValue(cacheKey); const teamIdPriceId = await getValue(cacheKey);
@@ -100,43 +133,50 @@ export async function supaAuthenticateUser(
teamId = team_id; teamId = team_id;
priceId = price_id; priceId = price_id;
} else { } else {
const { data, error } = await supabase_service.rpc( const {
"get_key_and_price_id_2", success,
{ api_key: normalizedApi } teamId: tId,
); priceId: pId,
if (error) { error,
Logger.error( status,
`RPC ERROR (get_key_and_price_id_2): ${error.message}` } = await getKeyAndPriceId(normalizedApi);
); if (!success) {
return { return { success, error, status };
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) { teamId = tId;
Logger.warn( priceId = pId;
`Error fetching api key: ${error.message} or data is empty` await setValue(
cacheKey,
JSON.stringify({ team_id: teamId, price_id: priceId }),
10
); );
// 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;
}
} }
} catch (error) { } catch (error) {
Logger.error(`Error with auth function: ${error.message}`); Logger.error(`Error with auth function: ${error.message}`);
} finally { // const {
await lock.release(); // success,
} // teamId: tId,
} catch (error) { // priceId: pId,
Logger.error(`Error acquiring the rate limiter lock: ${error}`); // error: e,
// status,
// } = await getKeyAndPriceId(normalizedApi);
// if (!success) {
// return { success, error: e, status };
// }
// teamId = tId;
// priceId = pId;
// const {
// success,
// teamId: tId,
// priceId: pId,
// error: e,
// status,
// } = await getKeyAndPriceId(normalizedApi);
// if (!success) {
// return { success, error: e, status };
// }
// teamId = tId;
// priceId = pId;
} }
// get_key_and_price_id_2 rpc definition: // get_key_and_price_id_2 rpc definition:
@@ -217,12 +257,12 @@ export async function supaAuthenticateUser(
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 // Cache longer for 429s
if (teamId && priceId && mode !== RateLimiterMode.Preview) { if (teamId && priceId && mode !== RateLimiterMode.Preview) {
await setValue( await setValue(
cacheKey, cacheKey,
JSON.stringify({ team_id: teamId, price_id: priceId }), JSON.stringify({ team_id: teamId, price_id: priceId }),
60 * 5 60 // 10 seconds, cache for everything
); );
} }
+2
View File
@@ -189,3 +189,5 @@ wsq.on("completed", j => ScrapeEvents.logJobEvent(j, "completed"));
wsq.on("paused", j => ScrapeEvents.logJobEvent(j, "paused")); wsq.on("paused", j => ScrapeEvents.logJobEvent(j, "paused"));
wsq.on("resumed", j => ScrapeEvents.logJobEvent(j, "resumed")); wsq.on("resumed", j => ScrapeEvents.logJobEvent(j, "resumed"));
wsq.on("removed", j => ScrapeEvents.logJobEvent(j, "removed")); wsq.on("removed", j => ScrapeEvents.logJobEvent(j, "removed"));