Add retry to aquire

This commit is contained in:
Owen
2026-01-11 10:39:28 -08:00
parent 192702daf9
commit 78b00a18cc

View File

@@ -24,7 +24,9 @@ export class LockManager {
*/ */
async acquireLock( async acquireLock(
lockKey: string, lockKey: string,
ttlMs: number = 30000 ttlMs: number = 30000,
maxRetries: number = 3,
retryDelayMs: number = 100
): Promise<boolean> { ): Promise<boolean> {
if (!redis || !redis.status || redis.status !== "ready") { if (!redis || !redis.status || redis.status !== "ready") {
return true; return true;
@@ -35,6 +37,7 @@ export class LockManager {
}:${Date.now()}`; }:${Date.now()}`;
const redisKey = `lock:${lockKey}`; const redisKey = `lock:${lockKey}`;
for (let attempt = 0; attempt < maxRetries; attempt++) {
try { try {
// Use SET with NX (only set if not exists) and PX (expire in milliseconds) // Use SET with NX (only set if not exists) and PX (expire in milliseconds)
// This is atomic and handles both setting and expiration // This is atomic and handles both setting and expiration
@@ -73,11 +76,28 @@ export class LockManager {
return true; return true;
} }
return false; // If this isn't our last attempt, wait before retrying with exponential backoff
} catch (error) { if (attempt < maxRetries - 1) {
logger.error(`Failed to acquire lock ${lockKey}:`, error); const delay = retryDelayMs * Math.pow(2, attempt);
return false; logger.debug(
`Lock ${lockKey} not available, retrying in ${delay}ms (attempt ${attempt + 1}/${maxRetries})`
);
await new Promise((resolve) => setTimeout(resolve, delay));
} }
} catch (error) {
logger.error(`Failed to acquire lock ${lockKey} (attempt ${attempt + 1}/${maxRetries}):`, error);
// On error, still retry if we have attempts left
if (attempt < maxRetries - 1) {
const delay = retryDelayMs * Math.pow(2, attempt);
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
}
logger.debug(
`Failed to acquire lock ${lockKey} after ${maxRetries} attempts`
);
return false;
} }
/** /**