mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-26 06:46:40 +00:00
Add retry to aquire
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user