mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-27 18:53:08 +02:00
d071d18dd7
* wip * wip * fix * clean up * Update tsconfig.json * Update activitypub.ts * wip
31 lines
875 B
TypeScript
31 lines
875 B
TypeScript
import { redisClient } from '../db/redis.js';
|
|
import { promisify } from 'node:util';
|
|
import redisLock from 'redis-lock';
|
|
|
|
/**
|
|
* Retry delay (ms) for lock acquisition
|
|
*/
|
|
const retryDelay = 100;
|
|
|
|
const lock: (key: string, timeout?: number) => Promise<() => void>
|
|
= redisClient
|
|
? promisify(redisLock(redisClient, retryDelay))
|
|
: 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);
|
|
}
|