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.
This commit is contained in:
calzoneman 2016-05-25 18:52:17 -07:00
parent 5a2aa396fe
commit 594a9e17da
2 changed files with 9 additions and 6 deletions

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "3.16.0", "version": "3.16.1",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },

View File

@ -62,12 +62,15 @@ function initChannelDumper(Server) {
var CHANNEL_SAVE_INTERVAL = parseInt(Config.get("channel-save-interval")) var CHANNEL_SAVE_INTERVAL = parseInt(Config.get("channel-save-interval"))
* 60000; * 60000;
setInterval(function () { setInterval(function () {
var wait = CHANNEL_SAVE_INTERVAL / Server.channels.length;
Promise.reduce(Server.channels, (_, chan) => { Promise.reduce(Server.channels, (_, chan) => {
if (!chan.dead && chan.users && chan.users.length > 0) { return Promise.delay(wait).then(() => {
return chan.saveState().catch(err => { if (!chan.dead && chan.users && chan.users.length > 0) {
Logger.errlog.log(`Failed to save /r/${chan.name}: ${err.stack}`); return chan.saveState().catch(err => {
}); Logger.errlog.log(`Failed to save /r/${chan.name}: ${err.stack}`);
} });
}
});
}, 0); }, 0);
}, CHANNEL_SAVE_INTERVAL); }, CHANNEL_SAVE_INTERVAL);
} }