From 7aa5a5a6ed658407307a52c37dcd0b3db84aae37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A4r=20Halberkamp?= Date: Fri, 10 Aug 2018 16:10:18 -0400 Subject: [PATCH] Pace pruning --- chat-logger.js | 17 +++++++++++++---- redis.js | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/chat-logger.js b/chat-logger.js index 332f1db..75ae2be 100644 --- a/chat-logger.js +++ b/chat-logger.js @@ -3,6 +3,7 @@ const redis = require('./redis.js'); const MINUTE = 1000 * 60; +const MAX_PRUNE_AMOUNT = 2500; let leftpad = val => (val < 10 ? `0${val}`: `${val}`); @@ -212,10 +213,9 @@ class ChatLogger { return (await this.seen.get(userid)); } - async pruneAll() { - let keys = await this.logs.keys('*'); - - for (let user of keys) { + async prune(keys) { + // Pace the pruning to avoid overloading the redis server. + for (let user of keys.slice(0, MAX_PRUNE_AMOUNT)) { let linecount = await this.logs.hkeys(user); let today = new Date(); @@ -234,6 +234,15 @@ class ChatLogger { this.logs.hdel.apply(this.logs, toPrune); } } + + let rest = keys.slice(MAX_PRUNE_AMOUNT); + if (rest.length) this.prune(rest); + } + + async pruneAll() { + let keys = await this.logs.keys('*'); + + this.prune(keys); } } diff --git a/redis.js b/redis.js index c2bea6b..6815bfc 100644 --- a/redis.js +++ b/redis.js @@ -39,7 +39,7 @@ module.exports = { }); client.on('close', () => { client.clientConnect(); - }) + }); this.databases[name] = client; return client; },