Add cache clearing of moderation requests

This removes the memory leak of not removing the moderation api cache
This commit is contained in:
Wroclaw 2023-07-30 18:04:53 +02:00
parent 33a16bd629
commit 01231151b3

View file

@ -1,10 +1,13 @@
import { Collection, InteractionResponse, Message } from "discord.js"; import { Collection, InteractionResponse, Message, SnowflakeUtil } from "discord.js";
import { openai } from "./index"; import { openai } from "./index";
import { formatRequestOrResponse } from "./toOpenAIMessages"; import { formatRequestOrResponse } from "./toOpenAIMessages";
import config from "./config";
export default class Moderation { export default class Moderation {
/** Represents cache of messages that have been checked aganist OpenAI moderation API. */ /** Represents cache of messages that have been checked aganist OpenAI moderation API. */
private static cache = new Collection<string, boolean>(); private static cache = new Collection<string, boolean>();
private static worker: NodeJS.Timeout | null = null;
public static async checkMessage(message: Message | InteractionResponse): Promise<boolean> { public static async checkMessage(message: Message | InteractionResponse): Promise<boolean> {
if (this.cache.has(message.id)) { if (this.cache.has(message.id)) {
@ -43,4 +46,29 @@ export default class Moderation {
return false; 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();