|
| 1 | +/** |
| 2 | + * 2 async locks for safe concurrent access to server resources: |
| 3 | + * |
| 4 | + * 1. AsyncRWLock - Read-Write lock allowing multiple concurrent readers OR a single writer |
| 5 | + * 2. AsyncMutex - Mutual exclusion lock allowing only one accessor at a time |
| 6 | + */ |
| 7 | + |
| 8 | +import AsyncLock from 'async-lock'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Read-Write lock that allows multiple concurrent readers but only one writer. |
| 12 | + * |
| 13 | + * Rules: |
| 14 | + * - Multiple readers can hold the lock simultaneously |
| 15 | + * - Only one writer can hold the lock at a time |
| 16 | + * - Writers are exclusive with both readers and other writers |
| 17 | + * |
| 18 | + * @example |
| 19 | + * ```typescript |
| 20 | + * const rwLock = new AsyncRWLock(); |
| 21 | + * |
| 22 | + * // Multiple readers can execute concurrently |
| 23 | + * await rwLock.withReadLock(async () => { |
| 24 | + * return data.value; |
| 25 | + * }); |
| 26 | + * |
| 27 | + * // Writers get exclusive access |
| 28 | + * await rwLock.withWriteLock(async () => { |
| 29 | + * data.value = newValue; |
| 30 | + * }); |
| 31 | + * ``` |
| 32 | + */ |
| 33 | +export class AsyncRWLock { |
| 34 | + private readonly lock: AsyncLock; |
| 35 | + private readonly READ_KEY = 'read'; |
| 36 | + private readonly WRITE_KEY = 'write'; |
| 37 | + |
| 38 | + constructor(maxPending: number, timeout: number) { |
| 39 | + this.lock = new AsyncLock({ |
| 40 | + maxPending: maxPending, |
| 41 | + timeout: timeout |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Acquire a read lock and execute the given function. |
| 47 | + * Multiple readers can execute concurrently. |
| 48 | + * |
| 49 | + * @param fn - Async function to execute while holding the read lock |
| 50 | + * @returns The result of the function |
| 51 | + * @throws Error if lock acquisition times out |
| 52 | + */ |
| 53 | + async withReadLock<T>(fn: () => Promise<T>): Promise<T> { |
| 54 | + return this.lock.acquire(this.READ_KEY, fn); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Acquire a write lock and execute the given function. |
| 59 | + * Only one writer can execute at a time, and it's exclusive with all readers. |
| 60 | + * |
| 61 | + * @param fn - Async function to execute while holding the write lock |
| 62 | + * @returns The result of the function |
| 63 | + * @throws Error if lock acquisition times out |
| 64 | + */ |
| 65 | + async withWriteLock<T>(fn: () => Promise<T>): Promise<T> { |
| 66 | + // Write locks are exclusive with both reads and other writes |
| 67 | + return this.lock.acquire([this.READ_KEY, this.WRITE_KEY], fn); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Mutual exclusion lock that allows only one accessor at a time. |
| 73 | + * |
| 74 | + * Use this for protecting operations that must be atomic or when you need to ensure serial execution of critical sections. |
| 75 | + * |
| 76 | + * @example |
| 77 | + * ```typescript |
| 78 | + * const mutex = new AsyncMutex(); |
| 79 | + * |
| 80 | + * // Only one execution at a time |
| 81 | + * await mutex.withLock(async () => { |
| 82 | + * // Critical section |
| 83 | + * await doSomethingImportant(); |
| 84 | + * }); |
| 85 | + * ``` |
| 86 | + */ |
| 87 | +export class AsyncMutex { |
| 88 | + private readonly lock: AsyncLock; |
| 89 | + private readonly KEY = 'mutex'; |
| 90 | + |
| 91 | + constructor(maxPending: number, timeout: number) { |
| 92 | + this.lock = new AsyncLock({ |
| 93 | + maxPending: maxPending, |
| 94 | + timeout: timeout |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Acquire the mutex lock and execute the given function. |
| 100 | + * |
| 101 | + * @param fn - Async function to execute while holding the lock |
| 102 | + * @returns The result of the function |
| 103 | + * @throws Error if lock acquisition times out |
| 104 | + */ |
| 105 | + async withLock<T>(fn: () => Promise<T>): Promise<T> { |
| 106 | + return this.lock.acquire(this.KEY, fn); |
| 107 | + } |
| 108 | +} |
0 commit comments