1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-05 02:08:14 +09:00

頻繁にAPリクエストが発生するサーバーに対してfetchInstanceMetadata手動更新が無視される問題を修正 (MisskeyIO#194)

This commit is contained in:
まっちゃとーにゅ 2023-10-29 09:40:48 +09:00 committed by GitHub
parent 239d9a4c81
commit 42bc2817be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 6 deletions

View File

@ -52,20 +52,20 @@ export class FetchInstanceMetadataService {
@bindThis
public async tryLock(host: string): Promise<boolean> {
const mutex = await this.redisClient.set(`fetchInstanceMetadata:mutex:${host}`, '1', 'EX', 60 * 5, 'NX', 'GET');
return mutex !== '1';
const mutex = await this.redisClient.set(`fetchInstanceMetadata:mutex:${host}`, Date.now(), 'EX', 60 * 5, 'NX');
return mutex !== null;
}
@bindThis
public unlock(host: string): Promise<number> {
return this.redisClient.del(`fetchInstanceMetadata:mutex:${host}`);
return this.redisClient.unlink(`fetchInstanceMetadata:mutex:${host}`);
}
@bindThis
public async fetchInstanceMetadata(instance: MiInstance, force = false): Promise<void> {
const host = instance.host;
// Acquire mutex to ensure no parallel runs
if (!await this.tryLock(host)) return;
if (!await this.tryLock(host) && !force) return;
try {
if (!force) {
const _instance = await this.federatedInstanceService.fetch(host);

View File

@ -23,9 +23,10 @@ import type { MockFunctionMetadata } from 'jest-mock';
function mockRedis() {
const hash = {};
const set = jest.fn((key, value) => {
const ret = hash[key];
// このテストで呼び出すSETにはNXオプションが付いてる
if (hash[key]) return null;
hash[key] = value;
return ret;
return 'OK';
});
return set;
}