make MutexOptions optional

This commit is contained in:
HorizonCode 2025-01-10 01:19:55 +01:00
parent f65ab42231
commit 33a49cba4a

View File

@ -9,9 +9,9 @@ type RedisClient = redis.RedisClientType<
export class MutexLock { export class MutexLock {
redisClient: RedisClient; redisClient: RedisClient;
mutexOptions: MutexOptions; mutexOptions: MutexOptions | undefined;
constructor(redisClient: RedisClient, options: MutexOptions) { constructor(redisClient: RedisClient, options?: MutexOptions) {
this.mutexOptions = options; this.mutexOptions = options;
this.redisClient = redisClient; this.redisClient = redisClient;
} }
@ -19,7 +19,7 @@ export class MutexLock {
static async create(options: MutexOptions) { static async create(options: MutexOptions) {
const redisClient = await redis const redisClient = await redis
.createClient({ .createClient({
url: `redis://${options.redis?.host ?? "127.0.0.1"}:${options.redis?.port ?? 6379}`, url: `redis://${options?.redis?.host ?? "127.0.0.1"}:${options?.redis?.port ?? 6379}`,
}) })
.connect(); .connect();
@ -40,12 +40,12 @@ export class MutexLock {
if (acquired) { if (acquired) {
await this.redisClient.expire( await this.redisClient.expire(
lockIdentifier, lockIdentifier,
this.mutexOptions.mutex?.ttl || 60 this.mutexOptions?.mutex?.ttl || 60
); );
return releaseFunc; return releaseFunc;
} }
await new Promise(resolve => await new Promise(resolve =>
setTimeout(resolve, this.mutexOptions.mutex?.checkInterval || 100) setTimeout(resolve, this.mutexOptions?.mutex?.checkInterval || 100)
); );
} }
}} }}