mirror of
https://git.joinfirefish.org/firefish/firefish.git
synced 2024-05-19 18:21:11 +02:00
41 lines
997 B
TypeScript
41 lines
997 B
TypeScript
import { redisClient } from "../db/redis.js";
|
|
import { Mutex } from "redis-semaphore";
|
|
|
|
/**
|
|
* Retry delay (ms) for lock acquisition
|
|
*/
|
|
const retryDelay = 100;
|
|
|
|
/**
|
|
* Get AP Object lock
|
|
* @param uri AP object ID
|
|
* @param timeout Lock timeout (ms), The timeout releases previous lock.
|
|
* @returns Unlock function
|
|
*/
|
|
export async function getApLock(uri: string, timeout = 30 * 1000) {
|
|
const lock = new Mutex(redisClient, `ap-object:${uri}`, {
|
|
lockTimeout: timeout,
|
|
retryInterval: retryDelay,
|
|
});
|
|
await lock.acquire();
|
|
}
|
|
|
|
export async function getFetchInstanceMetadataLock(
|
|
host: string,
|
|
timeout = 30 * 1000,
|
|
) {
|
|
const lock = new Mutex(redisClient, `instance:${host}`, {
|
|
lockTimeout: timeout,
|
|
retryInterval: retryDelay,
|
|
});
|
|
await lock.acquire();
|
|
}
|
|
|
|
export async function getChartInsertLock(lockKey: string, timeout = 30 * 1000) {
|
|
const lock = new Mutex(redisClient, `chart-insert:${lockKey}`, {
|
|
lockTimeout: timeout,
|
|
retryInterval: retryDelay,
|
|
});
|
|
await lock.acquire();
|
|
}
|