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",
"name": "CyTube",
"description": "Online media synchronizer and chat",
"version": "3.16.0",
"version": "3.16.1",
"repository": {
"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"))
* 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);
}