AP Lock (#5410)
This commit is contained in:
parent
9b91b92bca
commit
827c378ac1
6 changed files with 100 additions and 60 deletions
22
src/misc/app-lock.ts
Normal file
22
src/misc/app-lock.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import redis from '../db/redis';
|
||||
import { promisify } from 'util';
|
||||
|
||||
/**
|
||||
* Retry delay (ms) for lock acquisition
|
||||
*/
|
||||
const retryDelay = 100;
|
||||
|
||||
const lock: (key: string, timeout?: number) => Promise<() => void>
|
||||
= redis
|
||||
? promisify(require('redis-lock')(redis, 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue