4c879b3a33
* Perf: Avoid retries to acquire lock in fetchInstanceMetadata * Fix * Add Changelog * Fix typo * Fix lint * 記法をMisskey式にする * ???? * refactor https://github.com/misskey-dev/misskey/pull/11128#pullrequestreview-1518059366 * refactor * getいらない? * fix * fix * Update CHANGELOG.md * clean up --------- Co-authored-by: tamaina <tamaina@hotmail.co.jp>
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import { promisify } from 'node:util';
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
import redisLock from 'redis-lock';
|
|
import * as Redis from 'ioredis';
|
|
import { DI } from '@/di-symbols.js';
|
|
import { bindThis } from '@/decorators.js';
|
|
|
|
/**
|
|
* Retry delay (ms) for lock acquisition
|
|
*/
|
|
const retryDelay = 100;
|
|
|
|
@Injectable()
|
|
export class AppLockService {
|
|
private lock: (key: string, timeout?: number, _?: (() => Promise<void>) | undefined) => Promise<() => void>;
|
|
|
|
constructor(
|
|
@Inject(DI.redis)
|
|
private redisClient: Redis.Redis,
|
|
) {
|
|
this.lock = promisify(redisLock(this.redisClient, retryDelay));
|
|
}
|
|
|
|
/**
|
|
* Get AP Object lock
|
|
* @param uri AP object ID
|
|
* @param timeout Lock timeout (ms), The timeout releases previous lock.
|
|
* @returns Unlock function
|
|
*/
|
|
@bindThis
|
|
public getApLock(uri: string, timeout = 30 * 1000): Promise<() => void> {
|
|
return this.lock(`ap-object:${uri}`, timeout);
|
|
}
|
|
|
|
@bindThis
|
|
public getChartInsertLock(lockKey: string, timeout = 30 * 1000): Promise<() => void> {
|
|
return this.lock(`chart-insert:${lockKey}`, timeout);
|
|
}
|
|
}
|