mirror of
https://github.com/mendableai/firecrawl.git
synced 2024-11-16 11:42:24 +08:00
fix(redlock): use redlock.using for stability
This commit is contained in:
parent
eef116bef8
commit
953d4fb197
|
@ -40,27 +40,29 @@ function normalizedApiIsUuid(potentialUuid: string): boolean {
|
|||
export async function setCachedACUC(api_key: string, acuc: AuthCreditUsageChunk | ((acuc: AuthCreditUsageChunk) => AuthCreditUsageChunk)) {
|
||||
const cacheKeyACUC = `acuc_${api_key}`;
|
||||
const redLockKey = `lock_${cacheKeyACUC}`;
|
||||
const lockTTL = 10000; // 10 seconds
|
||||
|
||||
try {
|
||||
const lock = await redlock.acquire([redLockKey], lockTTL);
|
||||
|
||||
try {
|
||||
await redlock.using([redLockKey], 10000, {}, async signal => {
|
||||
if (typeof acuc === "function") {
|
||||
acuc = acuc(JSON.parse(await getValue(cacheKeyACUC)));
|
||||
|
||||
if (acuc === null) {
|
||||
await lock.release();
|
||||
if (signal.aborted) {
|
||||
throw signal.error;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (signal.aborted) {
|
||||
throw signal.error;
|
||||
}
|
||||
|
||||
// Cache for 10 minutes. This means that changing subscription tier could have
|
||||
// a maximum of 10 minutes of a delay. - mogery
|
||||
await setValue(cacheKeyACUC, JSON.stringify(acuc), 600);
|
||||
} finally {
|
||||
await lock.release();
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
Logger.error(`Error updating cached ACUC: ${error}`);
|
||||
Sentry.captureException(error);
|
||||
|
|
|
@ -11,7 +11,7 @@ export const redlock = new Redlock(
|
|||
driftFactor: 0.01, // multiplied by lock ttl to determine drift time
|
||||
|
||||
retryCount: 200,
|
||||
|
||||
|
||||
retryDelay: 100,
|
||||
|
||||
// the max time in ms randomly added to retries
|
||||
|
|
Loading…
Reference in New Issue
Block a user