From 21c1e5c06c9216d9b3acc28cd9f06a21c24755bf Mon Sep 17 00:00:00 2001 From: Chloe Kudryavtsev Date: Sun, 16 Oct 2022 09:22:05 -0400 Subject: [PATCH] backend: simplify suspended and dead queries This should also have better latency due to being a single query. Furthermore, it's no longer a linear scan, since host is indexed. Would be cool to simplify it further to a single query for blocks also... Why exactly are blocks not in the db? --- .../backend/src/queue/processors/deliver.ts | 35 +++++-------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/packages/backend/src/queue/processors/deliver.ts b/packages/backend/src/queue/processors/deliver.ts index a518c4116..67cb2fe60 100644 --- a/packages/backend/src/queue/processors/deliver.ts +++ b/packages/backend/src/queue/processors/deliver.ts @@ -13,15 +13,13 @@ import { Instance } from '@/models/entities/instance.js'; import { StatusError } from '@/misc/fetch.js'; import { DeliverJobData } from '@/queue/types.js'; import { LessThan } from 'typeorm'; +import { DAY } from '@/const.ts'; const logger = new Logger('deliver'); let latest: string | null = null; -const suspendedHostsCache = new Cache(1000 * 60 * 60); -// dead host list is a linear scan, so cache it longer -const deadHostsCache = new Cache(1000 * 60 * 60 * 24); -const deadThreshold = 1000 * 60 * 60 * 24 * 30; // 1 month +const deadThreshold = 30 * DAY; export default async (job: Bull.Job) => { const { host } = new URL(job.data.to); @@ -33,29 +31,12 @@ export default async (job: Bull.Job) => { return 'skip (blocked)'; } - // isSuspended - let suspendedHosts = suspendedHostsCache.get(null); - if (suspendedHosts == null) { - suspendedHosts = await Instances.findBy({ - isSuspended: true, - }); - suspendedHostsCache.set(null, suspendedHosts); - } - if (suspendedHosts.some(x => x.host == puny)) { - return 'skip (suspended)'; - } - - // dead - let deadHosts = deadHostsCache.get(null); - if (deadHosts == null) { - const deadTime = new Date(Date.now() - deadThreshold); - deadHosts = await Instances.findBy({ - lastCommunicatedAt: LessThan(deadTime), - }); - deadHostsCache.set(null, deadHosts); - } - if (deadHosts.some(x => x.host == puny)) { - return 'skip (dead instance)'; + const isSuspendedOrDead = await Instances.countBy([ + { host: puny, isSuspended: true }, + { host: puny, lastCommunicatedAt: LessThan(deadTime) }, + ]); + if (isSuspendedOrDead) { + return 'skip (suspended or dead)'; } try {