From 0b77de8b4c6bdc7cbb256b4e52fe8ac253edcb1c Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Fri, 4 Apr 2025 09:14:04 +0200 Subject: [PATCH] fix ttl sometimes -1 --- src/MutexLock.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/MutexLock.ts b/src/MutexLock.ts index 98c79bf..b355cd7 100644 --- a/src/MutexLock.ts +++ b/src/MutexLock.ts @@ -31,21 +31,22 @@ export class MutexLock { const releaseFunc = async () => { await this.redisClient.del(lockIdentifier); }; - + + let ttl = this.mutexOptions?.mutex?.ttl || 60; + if (ttl <= 0) ttl = 60; + while (true) { const acquired = await this.redisClient.set(lockIdentifier, "1", { - NX: true + NX: true, + EX: ttl, }); - + if (acquired) { - await this.redisClient.expire( - lockIdentifier, - this.mutexOptions?.mutex?.ttl || 60 - ); - return releaseFunc; + return releaseFunc;/* */ } - await new Promise(resolve => - setTimeout(resolve, this.mutexOptions?.mutex?.checkInterval || 100) + await new Promise((resolve) => + setTimeout(resolve, this.mutexOptions?.mutex?.checkInterval || 100), ); } - }} + } +}