From c330c5c6f9711479294998f2639f9945a4670fd5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 28 Sep 2020 17:04:52 +0200 Subject: [PATCH] Add garbage collection --- src/blacklist.ts | 6 ++++++ src/manager.ts | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/src/blacklist.ts b/src/blacklist.ts index 2c8439e..4d2b9c3 100644 --- a/src/blacklist.ts +++ b/src/blacklist.ts @@ -74,6 +74,12 @@ export class Blacklist { return data; } + public async garbageCollect(): Promise { + const statement = await this.db.prepare(`DELETE FROM ${this.tableName} WHERE lastSeen < ?`); + // Delete entries older than 30 days. + await statement.run(Date.now() - 86400_000 * 30); + } + protected get tableName(): string { return `blacklist_${this.type}`; } diff --git a/src/manager.ts b/src/manager.ts index 1ca0139..c5cbda3 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -146,6 +146,15 @@ export class Manager { ); } + try { + await blacklist.garbageCollect(); + } catch (e) { + console.error( + `Failed to garbage collect the data for '${blacklist.type}': ${e.message}`, + e.stack, + ); + } + return blacklist; } -- 2.20.1