From 01231151b39a308608f14bbee9ecebebf9fcfb61 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Sun, 30 Jul 2023 18:04:53 +0200 Subject: [PATCH] Add cache clearing of moderation requests This removes the memory leak of not removing the moderation api cache --- src/moderation.ts | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/moderation.ts b/src/moderation.ts index 64291c8..2e84d09 100644 --- a/src/moderation.ts +++ b/src/moderation.ts @@ -1,10 +1,13 @@ -import { Collection, InteractionResponse, Message } from "discord.js"; +import { Collection, InteractionResponse, Message, SnowflakeUtil } from "discord.js"; + import { openai } from "./index"; import { formatRequestOrResponse } from "./toOpenAIMessages"; +import config from "./config"; export default class Moderation { /** Represents cache of messages that have been checked aganist OpenAI moderation API. */ private static cache = new Collection(); + private static worker: NodeJS.Timeout | null = null; public static async checkMessage(message: Message | InteractionResponse): Promise { if (this.cache.has(message.id)) { @@ -43,4 +46,29 @@ export default class Moderation { return false; } } + + public static removeExpiredCacheEntries() { + const now = Date.now(); + for (const i of this.cache.keys()) { + if (now - SnowflakeUtil.timestampFrom(i) >= config.limits.time * 2) { + this.cache.delete(i); + } + } + } + public static startCacheClearWorker() { + if (this.worker) return; + this.worker = setInterval(() => { + this.removeExpiredCacheEntries(); + }, + 1000 * 60 * 15 /* 15 minutes */, + ); + } + + public static stopCacheClearWorker() { + if (!this.worker) return; + clearInterval(this.worker); + this.worker = null; + } } + +Moderation.startCacheClearWorker();