Nick: removed redlock
This commit is contained in:
@@ -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,56 +123,60 @@ export async function supaAuthenticateUser(
|
|||||||
status: 401,
|
status: 401,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheKey = `api_key:${normalizedApi}`;
|
cacheKey = `api_key:${normalizedApi}`;
|
||||||
redLockKey = `redlock:${cacheKey}`;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const lock = await redlock.acquire([redLockKey], lockTTL);
|
const teamIdPriceId = await getValue(cacheKey);
|
||||||
|
if (teamIdPriceId) {
|
||||||
try {
|
const { team_id, price_id } = JSON.parse(teamIdPriceId);
|
||||||
const teamIdPriceId = await getValue(cacheKey);
|
teamId = team_id;
|
||||||
if (teamIdPriceId) {
|
priceId = price_id;
|
||||||
const { team_id, price_id } = JSON.parse(teamIdPriceId);
|
} else {
|
||||||
teamId = team_id;
|
const {
|
||||||
priceId = price_id;
|
success,
|
||||||
} else {
|
teamId: tId,
|
||||||
const { data, error } = await supabase_service.rpc(
|
priceId: pId,
|
||||||
"get_key_and_price_id_2",
|
error,
|
||||||
{ api_key: normalizedApi }
|
status,
|
||||||
);
|
} = await getKeyAndPriceId(normalizedApi);
|
||||||
if (error) {
|
if (!success) {
|
||||||
Logger.error(
|
return { success, error, status };
|
||||||
`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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
teamId = tId;
|
||||||
Logger.error(`Error with auth function: ${error.message}`);
|
priceId = pId;
|
||||||
} finally {
|
await setValue(
|
||||||
await lock.release();
|
cacheKey,
|
||||||
|
JSON.stringify({ team_id: teamId, price_id: priceId }),
|
||||||
|
10
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
Logger.error(`Error acquiring the rate limiter lock: ${error}`);
|
Logger.error(`Error with auth function: ${error.message}`);
|
||||||
|
// const {
|
||||||
|
// success,
|
||||||
|
// teamId: tId,
|
||||||
|
// priceId: pId,
|
||||||
|
// 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
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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"));
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user