| 1 | import { SetMetadata } from '@nestjs/common' |
| 2 | |
| 3 | export interface RedlockOptions { |
| 4 | key: string | ((...args: any[]) => string) |
| 5 | ttl?: number |
| 6 | retryDelay?: number |
| 7 | retryCount?: number |
| 8 | throwOnFailure?: boolean |
| 9 | } |
| 10 | |
| 11 | export const REDLOCK_METADATA = Symbol('REDLOCK_METADATA') |
| 12 | |
| 13 | export function Redlock( |
| 14 | key: string | ((...args: any[]) => string), |
| 15 | // secs |
| 16 | ttl?: number, |
| 17 | options?: { retryDelay?: number, retryCount?: number, throwOnFailure?: boolean }, |
| 18 | ): MethodDecorator { |
| 19 | const lockOptions: RedlockOptions = { |
| 20 | key, |
| 21 | ttl, |
| 22 | throwOnFailure: true, // 默认抛出错误 |
| 23 | ...(options ?? {}), |
| 24 | } |
| 25 | return SetMetadata(REDLOCK_METADATA, lockOptions) |
| 26 | } |
| 27 |