server: fetch meta only once in skippedInstances

This commit is contained in:
Norm 2022-12-02 09:26:14 -05:00
parent 5e6b51094e
commit 81d63720f2
Signed by untrusted user: norm
GPG key ID: 7123E30E441E80DE

View file

@ -2,6 +2,7 @@ import { db } from '@/db/postgre.js';
import { fetchMeta } from '@/misc/fetch-meta.js'; import { fetchMeta } from '@/misc/fetch-meta.js';
import { Instance } from '@/models/entities/instance.js'; import { Instance } from '@/models/entities/instance.js';
import { DAY } from '@/const.js'; import { DAY } from '@/const.js';
import { Meta } from '@/models/entities/meta.js';
// Threshold from last contact after which an instance will be considered // Threshold from last contact after which an instance will be considered
// "dead" and should no longer get activities delivered to it. // "dead" and should no longer get activities delivered to it.
@ -26,10 +27,11 @@ function matchHost(host: Instance['host'], pattern: string): boolean {
* Returns whether a specific host (punycoded) should be blocked. * Returns whether a specific host (punycoded) should be blocked.
* *
* @param host punycoded instance host * @param host punycoded instance host
* @param meta a Promise contatining the information from the meta table (oprional)
* @returns whether the given host should be blocked * @returns whether the given host should be blocked
*/ */
export async function shouldBlockInstance(host: string): Promise<boolean> { export async function shouldBlockInstance(host: string, meta: Promise<Meta> = fetchMeta()): Promise<boolean> {
const { blockedHosts } = await fetchMeta(); const { blockedHosts } = await meta;
return blockedHosts.some(blockedHost => matchHost(host, blockedHost)); return blockedHosts.some(blockedHost => matchHost(host, blockedHost));
} }
@ -41,7 +43,8 @@ export async function shouldBlockInstance(host: string): Promise<boolean> {
*/ */
export async function skippedInstances(hosts: Array<Instance['host']>): Promise<Array<Instance['host']>> { export async function skippedInstances(hosts: Array<Instance['host']>): Promise<Array<Instance['host']>> {
// Resolve the boolean promises before filtering // Resolve the boolean promises before filtering
const shouldSkip = await Promise.all(hosts.map(shouldBlockInstance)); const meta = fetchMeta();
const shouldSkip = await Promise.all(hosts.map(host => shouldBlockInstance(host, meta)));
const skipped = hosts.filter((_, i) => shouldSkip[i]); const skipped = hosts.filter((_, i) => shouldSkip[i]);
// if possible return early and skip accessing the database // if possible return early and skip accessing the database