From 594a9e17da7f85d4866dd73d8ff6913f6a17339a Mon Sep 17 00:00:00 2001 From: calzoneman Date: Wed, 25 May 2016 18:52:17 -0700 Subject: [PATCH] Spread channel saves across the save interval Since all channels were saved sequentially, this would cause huge lag spikes every time the channel save interval fired. This change adds a delay between each channel so that the additional load is spread evenly across the save interval. --- package.json | 2 +- src/bgtask.js | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index c7d7994a..b0405ab4 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Calvin Montgomery", "name": "CyTube", "description": "Online media synchronizer and chat", - "version": "3.16.0", + "version": "3.16.1", "repository": { "url": "http://github.com/calzoneman/sync" }, diff --git a/src/bgtask.js b/src/bgtask.js index e2fb9e75..7cb6ab4c 100644 --- a/src/bgtask.js +++ b/src/bgtask.js @@ -62,12 +62,15 @@ function initChannelDumper(Server) { var CHANNEL_SAVE_INTERVAL = parseInt(Config.get("channel-save-interval")) * 60000; setInterval(function () { + var wait = CHANNEL_SAVE_INTERVAL / Server.channels.length; Promise.reduce(Server.channels, (_, chan) => { - if (!chan.dead && chan.users && chan.users.length > 0) { - return chan.saveState().catch(err => { - Logger.errlog.log(`Failed to save /r/${chan.name}: ${err.stack}`); - }); - } + return Promise.delay(wait).then(() => { + if (!chan.dead && chan.users && chan.users.length > 0) { + return chan.saveState().catch(err => { + Logger.errlog.log(`Failed to save /r/${chan.name}: ${err.stack}`); + }); + } + }); }, 0); }, CHANNEL_SAVE_INTERVAL); }