cache relay

This commit is contained in:
syuilo 2022-03-25 16:26:24 +09:00
parent d14c9718a6
commit c239b3477a

View File

@ -6,9 +6,13 @@ import { deliver } from '@/queue/index.js';
import { ILocalUser, User } from '@/models/entities/user.js'; import { ILocalUser, User } from '@/models/entities/user.js';
import { Users, Relays } from '@/models/index.js'; import { Users, Relays } from '@/models/index.js';
import { genId } from '@/misc/gen-id.js'; import { genId } from '@/misc/gen-id.js';
import { Cache } from '@/misc/cache.js';
import { Relay } from '@/models/entities/relay.js';
const ACTOR_USERNAME = 'relay.actor' as const; const ACTOR_USERNAME = 'relay.actor' as const;
const relaysCache = new Cache<Relay[]>(1000 * 60 * 10);
export async function getRelayActor(): Promise<ILocalUser> { export async function getRelayActor(): Promise<ILocalUser> {
const user = await Users.findOne({ const user = await Users.findOne({
host: null, host: null,
@ -78,9 +82,9 @@ export async function relayRejected(id: string) {
export async function deliverToRelays(user: { id: User['id']; host: null; }, activity: any) { export async function deliverToRelays(user: { id: User['id']; host: null; }, activity: any) {
if (activity == null) return; if (activity == null) return;
const relays = await Relays.find({ const relays = await relaysCache.fetch(null, () => Relays.find({
status: 'accepted', status: 'accepted',
}); }));
if (relays.length === 0) return; if (relays.length === 0) return;
const copy = JSON.parse(JSON.stringify(activity)); const copy = JSON.parse(JSON.stringify(activity));