1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-26 12:38:08 +09:00
hotomoe/packages/backend/src/misc/app-lock.ts

32 lines
875 B
TypeScript
Raw Normal View History

import { redisClient } from '../db/redis.js';
import { promisify } from 'node:util';
import redisLock from 'redis-lock';
2019-09-09 22:46:45 +09:00
/**
* Retry delay (ms) for lock acquisition
*/
const retryDelay = 100;
const lock: (key: string, timeout?: number) => Promise<() => void>
= redisClient
? promisify(redisLock(redisClient, retryDelay))
2019-09-09 22:46:45 +09:00
: async () => () => { };
/**
* Get AP Object lock
* @param uri AP object ID
* @param timeout Lock timeout (ms), The timeout releases previous lock.
* @returns Unlock function
*/
export function getApLock(uri: string, timeout = 30 * 1000) {
return lock(`ap-object:${uri}`, timeout);
}
export function getFetchInstanceMetadataLock(host: string, timeout = 30 * 1000) {
return lock(`instance:${host}`, timeout);
}
export function getChartInsertLock(lockKey: string, timeout = 30 * 1000) {
return lock(`chart-insert:${lockKey}`, timeout);
}