nstarter-lock
Version:
Mutex/Semaphore for Project NStarter.
75 lines (64 loc) • 2.17 kB
text/typescript
/**
* Copyright (c) 2015-2023, FineX, All Rights Reserved.
* @author vista@fanruan.com
* @date 2022/10/15
*/
import { Mutex as DefaultMutex, RedlockMutex } from 'redis-semaphore';
import { getRedis } from './redis';
import type { ILock, ILockOptions } from './types';
import { LockerInfoHelper } from './lockerInfo';
import { getNsError } from './error';
export class DistributedLock implements ILock {
readonly
readonly
readonly
constructor(
key: string,
options?: ILockOptions,
) {
this.
this.
const provider = getRedis();
if (provider.isCluster) {
// use red-lock implementation for cluster.
const cluster = provider.client.nodes('master');
this.
} else {
this.
}
}
public async acquire(): Promise<void> {
try {
await this.
if (this.
await this.
}
} catch (err: any) {
const locker = await this.
throw getNsError(err, { locker });
}
}
public async tryAcquire(): Promise<boolean> {
const acquired = await this.
if (acquired && this.
await this.
}
return acquired;
}
public async release(): Promise<void> {
await this.
await this.
}
public get key(): string {
return this.
}
public get identifier(): string {
return this.
}
public get isAcquired(): boolean {
return this.
}
public stopRefresh(): void {
this.
}
}