mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-02 08:46:38 +00:00
Lock working without redis?
This commit is contained in:
@@ -26,6 +26,10 @@ export class LockManager {
|
|||||||
lockKey: string,
|
lockKey: string,
|
||||||
ttlMs: number = 30000
|
ttlMs: number = 30000
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
|
if (!redis || !redis.status || redis.status !== "ready") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
const lockValue = `${
|
const lockValue = `${
|
||||||
config.getRawConfig().gerbil.exit_node_name
|
config.getRawConfig().gerbil.exit_node_name
|
||||||
}:${Date.now()}`;
|
}:${Date.now()}`;
|
||||||
@@ -81,6 +85,10 @@ export class LockManager {
|
|||||||
* @param lockKey - Unique identifier for the lock
|
* @param lockKey - Unique identifier for the lock
|
||||||
*/
|
*/
|
||||||
async releaseLock(lockKey: string): Promise<void> {
|
async releaseLock(lockKey: string): Promise<void> {
|
||||||
|
if (!redis || !redis.status || redis.status !== "ready") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const redisKey = `lock:${lockKey}`;
|
const redisKey = `lock:${lockKey}`;
|
||||||
|
|
||||||
// Lua script to ensure we only delete the lock if it belongs to this worker
|
// Lua script to ensure we only delete the lock if it belongs to this worker
|
||||||
@@ -127,6 +135,10 @@ export class LockManager {
|
|||||||
* @param lockKey - Unique identifier for the lock
|
* @param lockKey - Unique identifier for the lock
|
||||||
*/
|
*/
|
||||||
async forceReleaseLock(lockKey: string): Promise<void> {
|
async forceReleaseLock(lockKey: string): Promise<void> {
|
||||||
|
if (!redis || !redis.status || redis.status !== "ready") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const redisKey = `lock:${lockKey}`;
|
const redisKey = `lock:${lockKey}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -150,6 +162,10 @@ export class LockManager {
|
|||||||
ttl: number;
|
ttl: number;
|
||||||
owner?: string;
|
owner?: string;
|
||||||
}> {
|
}> {
|
||||||
|
if (!redis || !redis.status || redis.status !== "ready") {
|
||||||
|
return { exists: false, ownedByMe: true, ttl: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
const redisKey = `lock:${lockKey}`;
|
const redisKey = `lock:${lockKey}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -183,6 +199,10 @@ export class LockManager {
|
|||||||
* @returns Promise<boolean> - true if extended successfully
|
* @returns Promise<boolean> - true if extended successfully
|
||||||
*/
|
*/
|
||||||
async extendLock(lockKey: string, ttlMs: number): Promise<boolean> {
|
async extendLock(lockKey: string, ttlMs: number): Promise<boolean> {
|
||||||
|
if (!redis || !redis.status || redis.status !== "ready") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
const redisKey = `lock:${lockKey}`;
|
const redisKey = `lock:${lockKey}`;
|
||||||
|
|
||||||
// Lua script to extend TTL only if lock is owned by this worker
|
// Lua script to extend TTL only if lock is owned by this worker
|
||||||
@@ -237,6 +257,10 @@ export class LockManager {
|
|||||||
maxRetries: number = 5,
|
maxRetries: number = 5,
|
||||||
baseDelayMs: number = 100
|
baseDelayMs: number = 100
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
|
if (!redis || !redis.status || redis.status !== "ready") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
||||||
const acquired = await this.acquireLock(lockKey, ttlMs);
|
const acquired = await this.acquireLock(lockKey, ttlMs);
|
||||||
|
|
||||||
@@ -270,6 +294,10 @@ export class LockManager {
|
|||||||
fn: () => Promise<T>,
|
fn: () => Promise<T>,
|
||||||
ttlMs: number = 30000
|
ttlMs: number = 30000
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
|
if (!redis || !redis.status || redis.status !== "ready") {
|
||||||
|
return await fn();
|
||||||
|
}
|
||||||
|
|
||||||
const acquired = await this.acquireLock(lockKey, ttlMs);
|
const acquired = await this.acquireLock(lockKey, ttlMs);
|
||||||
|
|
||||||
if (!acquired) {
|
if (!acquired) {
|
||||||
@@ -292,6 +320,10 @@ export class LockManager {
|
|||||||
activeLocksCount: number;
|
activeLocksCount: number;
|
||||||
locksOwnedByMe: number;
|
locksOwnedByMe: number;
|
||||||
}> {
|
}> {
|
||||||
|
if (!redis || !redis.status || redis.status !== "ready") {
|
||||||
|
return { activeLocksCount: 0, locksOwnedByMe: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const keys = await redis.keys("lock:*");
|
const keys = await redis.keys("lock:*");
|
||||||
let locksOwnedByMe = 0;
|
let locksOwnedByMe = 0;
|
||||||
@@ -321,6 +353,9 @@ export class LockManager {
|
|||||||
* Close the Redis connection
|
* Close the Redis connection
|
||||||
*/
|
*/
|
||||||
async disconnect(): Promise<void> {
|
async disconnect(): Promise<void> {
|
||||||
|
if (!redis || !redis.status || redis.status !== "ready") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
await redis.quit();
|
await redis.quit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user