fix ttl sometimes -1

This commit is contained in:
HorizonCode 2025-04-04 09:14:04 +02:00
parent 6ae5b3ddd2
commit 0b77de8b4c

View File

@ -31,21 +31,22 @@ export class MutexLock {
const releaseFunc = async () => { const releaseFunc = async () => {
await this.redisClient.del(lockIdentifier); await this.redisClient.del(lockIdentifier);
}; };
let ttl = this.mutexOptions?.mutex?.ttl || 60;
if (ttl <= 0) ttl = 60;
while (true) { while (true) {
const acquired = await this.redisClient.set(lockIdentifier, "1", { const acquired = await this.redisClient.set(lockIdentifier, "1", {
NX: true NX: true,
EX: ttl,
}); });
if (acquired) { if (acquired) {
await this.redisClient.expire( return releaseFunc;/* */
lockIdentifier,
this.mutexOptions?.mutex?.ttl || 60
);
return releaseFunc;
} }
await new Promise(resolve => await new Promise((resolve) =>
setTimeout(resolve, this.mutexOptions?.mutex?.checkInterval || 100) setTimeout(resolve, this.mutexOptions?.mutex?.checkInterval || 100),
); );
} }
}} }
}