1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +01:00
unleash.unleash/src/lib/util/db-lock.ts

50 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-04-04 13:28:17 +02:00
import { Client } from 'pg';
import { IDBOption } from '../types';
import { Logger } from '../logger';
export const defaultLockKey = 479341;
2023-10-20 12:10:18 +02:00
export const defaultTimeout = 30 * 60000;
2023-04-04 13:28:17 +02:00
interface IDbLockOptions {
timeout: number;
lockKey: number;
logger: Logger;
}
const defaultOptions: IDbLockOptions = {
timeout: defaultTimeout,
lockKey: defaultLockKey,
logger: { ...console, fatal: console.error },
};
export const withDbLock =
(dbConfig: IDBOption, config = defaultOptions) =>
<A extends any[], R>(fn: (...args: A) => Promise<R>) =>
async (...args: A): Promise<R> => {
const client = new Client({
...dbConfig,
});
try {
await client.connect();
// wait to obtain a lock
await client.query('SELECT pg_advisory_lock($1)', [config.lockKey]);
2023-10-20 12:10:18 +02:00
const promise = fn(...args);
const timeoutPromise = new Promise((_, reject) =>
setTimeout(
() => reject(new Error('Query read timeout')),
config.timeout,
),
);
const result = (await Promise.race([promise, timeoutPromise])) as R;
2023-04-04 13:28:17 +02:00
return result;
} catch (e) {
config.logger.error(`Locking error: ${e.message}`);
throw e;
} finally {
await client.query('SELECT pg_advisory_unlock($1)', [
config.lockKey,
]);
await client.end();
}
};