Skip to content

Commit 2cfb0e0

Browse files
Lock working without redis?
1 parent 5b9386b commit 2cfb0e0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

server/private/lib/lock.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export class LockManager {
2626
lockKey: string,
2727
ttlMs: number = 30000
2828
): Promise<boolean> {
29+
if (!redis || !redis.status || redis.status !== "ready") {
30+
return true;
31+
}
32+
2933
const lockValue = `${
3034
config.getRawConfig().gerbil.exit_node_name
3135
}:${Date.now()}`;
@@ -81,6 +85,10 @@ export class LockManager {
8185
* @param lockKey - Unique identifier for the lock
8286
*/
8387
async releaseLock(lockKey: string): Promise<void> {
88+
if (!redis || !redis.status || redis.status !== "ready") {
89+
return;
90+
}
91+
8492
const redisKey = `lock:${lockKey}`;
8593

8694
// Lua script to ensure we only delete the lock if it belongs to this worker
@@ -127,6 +135,10 @@ export class LockManager {
127135
* @param lockKey - Unique identifier for the lock
128136
*/
129137
async forceReleaseLock(lockKey: string): Promise<void> {
138+
if (!redis || !redis.status || redis.status !== "ready") {
139+
return;
140+
}
141+
130142
const redisKey = `lock:${lockKey}`;
131143

132144
try {
@@ -150,6 +162,10 @@ export class LockManager {
150162
ttl: number;
151163
owner?: string;
152164
}> {
165+
if (!redis || !redis.status || redis.status !== "ready") {
166+
return { exists: false, ownedByMe: true, ttl: 0 };
167+
}
168+
153169
const redisKey = `lock:${lockKey}`;
154170

155171
try {
@@ -183,6 +199,10 @@ export class LockManager {
183199
* @returns Promise<boolean> - true if extended successfully
184200
*/
185201
async extendLock(lockKey: string, ttlMs: number): Promise<boolean> {
202+
if (!redis || !redis.status || redis.status !== "ready") {
203+
return true;
204+
}
205+
186206
const redisKey = `lock:${lockKey}`;
187207

188208
// Lua script to extend TTL only if lock is owned by this worker
@@ -237,6 +257,10 @@ export class LockManager {
237257
maxRetries: number = 5,
238258
baseDelayMs: number = 100
239259
): Promise<boolean> {
260+
if (!redis || !redis.status || redis.status !== "ready") {
261+
return true;
262+
}
263+
240264
for (let attempt = 0; attempt <= maxRetries; attempt++) {
241265
const acquired = await this.acquireLock(lockKey, ttlMs);
242266

@@ -270,6 +294,10 @@ export class LockManager {
270294
fn: () => Promise<T>,
271295
ttlMs: number = 30000
272296
): Promise<T> {
297+
if (!redis || !redis.status || redis.status !== "ready") {
298+
return await fn();
299+
}
300+
273301
const acquired = await this.acquireLock(lockKey, ttlMs);
274302

275303
if (!acquired) {
@@ -292,6 +320,10 @@ export class LockManager {
292320
activeLocksCount: number;
293321
locksOwnedByMe: number;
294322
}> {
323+
if (!redis || !redis.status || redis.status !== "ready") {
324+
return { activeLocksCount: 0, locksOwnedByMe: 0 };
325+
}
326+
295327
try {
296328
const keys = await redis.keys("lock:*");
297329
let locksOwnedByMe = 0;
@@ -321,6 +353,9 @@ export class LockManager {
321353
* Close the Redis connection
322354
*/
323355
async disconnect(): Promise<void> {
356+
if (!redis || !redis.status || redis.status !== "ready") {
357+
return;
358+
}
324359
await redis.quit();
325360
}
326361
}

0 commit comments

Comments
 (0)