/
WrappedExpiringReadWriteLocker.ts
75 lines (64 loc) · 3.01 KB
/
WrappedExpiringReadWriteLocker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
import { getLoggerFor } from '../../logging/LogUtil';
import { InternalServerError } from '../errors/InternalServerError';
import type { ExpiringReadWriteLocker } from './ExpiringReadWriteLocker';
import type { ReadWriteLocker } from './ReadWriteLocker';
import Timeout = NodeJS.Timeout;
/**
* Wraps around an existing {@link ReadWriteLocker} and adds expiration logic to prevent locks from getting stuck.
*/
export class WrappedExpiringReadWriteLocker implements ExpiringReadWriteLocker {
protected readonly logger = getLoggerFor(this);
protected readonly locker: ReadWriteLocker;
protected readonly expiration: number;
/**
* @param locker - Instance of ResourceLocker to use for acquiring a lock.
* @param expiration - Time in ms after which the lock expires.
*/
public constructor(locker: ReadWriteLocker, expiration: number) {
this.locker = locker;
this.expiration = expiration;
}
public async withReadLock<T>(identifier: ResourceIdentifier,
whileLocked: (maintainLock: () => void) => T | Promise<T>): Promise<T> {
return this.locker.withReadLock(identifier, async(): Promise<T> => this.expiringPromise(identifier, whileLocked));
}
public async withWriteLock<T>(identifier: ResourceIdentifier,
whileLocked: (maintainLock: () => void) => T | Promise<T>): Promise<T> {
return this.locker.withWriteLock(identifier, async(): Promise<T> => this.expiringPromise(identifier, whileLocked));
}
/**
* Creates a Promise that either resolves the given input function or rejects if time runs out,
* whichever happens first. The input function can reset the timer by calling the `maintainLock` function
* it receives. The ResourceIdentifier is only used for logging.
*/
private async expiringPromise<T>(identifier: ResourceIdentifier,
whileLocked: (maintainLock: () => void) => T | Promise<T>): Promise<T> {
let timer: Timeout;
let createTimeout: () => Timeout;
// Promise that throws an error when the timer finishes
const timerPromise = new Promise<never>((resolve, reject): void => {
// Starts the timer that will cause this promise to error after a given time
createTimeout = (): Timeout => setTimeout((): void => {
this.logger.error(`Lock expired after ${this.expiration}ms on ${identifier.path}`);
reject(new InternalServerError(`Lock expired after ${this.expiration}ms on ${identifier.path}`));
}, this.expiration);
timer = createTimeout();
});
// Restarts the timer
const renewTimer = (): void => {
this.logger.verbose(`Renewed expiring lock on ${identifier.path}`);
clearTimeout(timer);
timer = createTimeout();
};
// Runs the main function and cleans up the timer afterwards
async function runWithTimeout(): Promise<T> {
try {
return await whileLocked(renewTimer);
} finally {
clearTimeout(timer);
}
}
return Promise.race([ timerPromise, runWithTimeout() ]);
}
}