From 9393388cc5a9ca16480ad532b5b2709636526243 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Fri, 6 Dec 2024 17:34:21 +0000 Subject: [PATCH 1/3] Add acquireTimeout option and improve lock options in Semaphore class --- Common/Server/Infrastructure/Semaphore.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Common/Server/Infrastructure/Semaphore.ts b/Common/Server/Infrastructure/Semaphore.ts index 50749d701f9..631f9e062a6 100644 --- a/Common/Server/Infrastructure/Semaphore.ts +++ b/Common/Server/Infrastructure/Semaphore.ts @@ -1,5 +1,5 @@ import Redis, { ClientType } from "./Redis"; -import { Mutex } from "redis-semaphore"; +import { Mutex, LockOptions } from "redis-semaphore"; export type SemaphoreMutex = Mutex; @@ -8,7 +8,8 @@ export default class Semaphore { public static async lock(data: { key: string; namespace: string; - lockTimeout?: number; + lockTimeout?: number | undefined; + acquireTimeout?: number | undefined; }): Promise { if (!data.lockTimeout) { data.lockTimeout = 5000; @@ -22,12 +23,23 @@ export default class Semaphore { throw new Error("Redis client is not connected"); } + + const lockOptions: LockOptions = { + + }; + + if(data.lockTimeout) { + lockOptions.lockTimeout = data.lockTimeout; + } + + if(data.acquireTimeout) { + lockOptions.acquireTimeout = data.acquireTimeout; + } + const mutex: SemaphoreMutex = new Mutex( client, data.namespace + "-" + key, - { - lockTimeout: data.lockTimeout, - }, + lockOptions ); await mutex.acquire(); From a3f1302e37119ef973c5fda4778251e274edbf13 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Fri, 6 Dec 2024 17:36:17 +0000 Subject: [PATCH 2/3] Add lockTimeout and acquireTimeout options in Monitor API semaphore --- ProbeIngest/API/Monitor.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ProbeIngest/API/Monitor.ts b/ProbeIngest/API/Monitor.ts index 4424a1ba7d1..1d9cd70f779 100644 --- a/ProbeIngest/API/Monitor.ts +++ b/ProbeIngest/API/Monitor.ts @@ -327,6 +327,8 @@ router.post( mutex = await Semaphore.lock({ key: probeId.toString(), namespace: "MonitorAPI.monitor-list", + lockTimeout: 15000, + acquireTimeout: 20000, }); } catch (err) { logger.error(err); From a286aba4327a1207d27acbaabe0eb833bc46a5a9 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Fri, 6 Dec 2024 17:39:12 +0000 Subject: [PATCH 3/3] Refactor Semaphore class to streamline lockOptions initialization --- Common/Server/Infrastructure/Semaphore.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Common/Server/Infrastructure/Semaphore.ts b/Common/Server/Infrastructure/Semaphore.ts index 631f9e062a6..a1874d38d11 100644 --- a/Common/Server/Infrastructure/Semaphore.ts +++ b/Common/Server/Infrastructure/Semaphore.ts @@ -23,23 +23,20 @@ export default class Semaphore { throw new Error("Redis client is not connected"); } + const lockOptions: LockOptions = {}; - const lockOptions: LockOptions = { - - }; - - if(data.lockTimeout) { + if (data.lockTimeout) { lockOptions.lockTimeout = data.lockTimeout; } - if(data.acquireTimeout) { + if (data.acquireTimeout) { lockOptions.acquireTimeout = data.acquireTimeout; } const mutex: SemaphoreMutex = new Mutex( client, data.namespace + "-" + key, - lockOptions + lockOptions, ); await mutex.acquire();