misskey/packages/backend/src/core/FederatedInstanceService.ts

96 lines
2.9 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
2023-04-14 13:50:05 +09:00
import * as Redis from 'ioredis';
2022-09-21 05:33:11 +09:00
import type { InstancesRepository } from '@/models/index.js';
2022-09-18 03:27:08 +09:00
import type { Instance } from '@/models/entities/Instance.js';
import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
2022-09-18 03:27:08 +09:00
import { IdService } from '@/core/IdService.js';
import { DI } from '@/di-symbols.js';
2022-12-04 10:16:03 +09:00
import { UtilityService } from '@/core/UtilityService.js';
import { bindThis } from '@/decorators.js';
2022-09-18 03:27:08 +09:00
@Injectable()
export class FederatedInstanceService implements OnApplicationShutdown {
public federatedInstanceCache: RedisKVCache<Instance | null>;
2022-09-18 03:27:08 +09:00
constructor(
@Inject(DI.redis)
private redisClient: Redis.Redis,
2022-09-18 03:27:08 +09:00
@Inject(DI.instancesRepository)
private instancesRepository: InstancesRepository,
private utilityService: UtilityService,
private idService: IdService,
) {
this.federatedInstanceCache = new RedisKVCache<Instance | null>(this.redisClient, 'federatedInstance', {
lifetime: 1000 * 60 * 30, // 30m
memoryCacheLifetime: 1000 * 60 * 3, // 3m
fetcher: (key) => this.instancesRepository.findOneBy({ host: key }),
toRedisConverter: (value) => JSON.stringify(value),
fromRedisConverter: (value) => {
const parsed = JSON.parse(value);
if (parsed == null) return null;
return {
...parsed,
firstRetrievedAt: new Date(parsed.firstRetrievedAt),
latestRequestReceivedAt: parsed.latestRequestReceivedAt ? new Date(parsed.latestRequestReceivedAt) : null,
infoUpdatedAt: parsed.infoUpdatedAt ? new Date(parsed.infoUpdatedAt) : null,
};
},
});
2022-09-18 03:27:08 +09:00
}
@bindThis
2023-01-03 09:32:36 +09:00
public async fetch(host: string): Promise<Instance> {
2022-09-18 03:27:08 +09:00
host = this.utilityService.toPuny(host);
const cached = await this.federatedInstanceCache.get(host);
2022-09-18 03:27:08 +09:00
if (cached) return cached;
2022-09-18 03:27:08 +09:00
const index = await this.instancesRepository.findOneBy({ host });
2022-09-18 03:27:08 +09:00
if (index == null) {
const i = await this.instancesRepository.insert({
id: this.idService.genId(),
host,
firstRetrievedAt: new Date(),
2022-09-18 03:27:08 +09:00
}).then(x => this.instancesRepository.findOneByOrFail(x.identifiers[0]));
this.federatedInstanceCache.set(host, i);
2022-09-18 03:27:08 +09:00
return i;
} else {
this.federatedInstanceCache.set(host, index);
2022-09-18 03:27:08 +09:00
return index;
}
}
2023-01-03 09:32:36 +09:00
@bindThis
public async update(id: Instance['id'], data: Partial<Instance>): Promise<void> {
2023-04-22 20:12:41 +09:00
const result = await this.instancesRepository.createQueryBuilder().update()
.set(data)
.where('id = :id', { id })
.returning('*')
.execute()
.then((response) => {
return response.raw[0];
});
this.federatedInstanceCache.set(result.host, result);
2023-01-03 09:32:36 +09:00
}
@bindThis
public dispose(): void {
this.federatedInstanceCache.dispose();
}
@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.dispose();
}
2022-09-18 03:27:08 +09:00
}