2020-06-23 13:29:38 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 13:29:34 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let DrainManager;
|
|
|
|
const logger = require("logger-sharelatex");
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
module.exports = (DrainManager = {
|
2019-08-13 06:12:04 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
startDrainTimeWindow(io, minsToDrain){
|
|
|
|
const drainPerMin = io.sockets.clients().length / minsToDrain;
|
|
|
|
return DrainManager.startDrain(io, Math.max(drainPerMin / 60, 4));
|
|
|
|
}, // enforce minimum drain rate
|
2019-08-13 06:12:04 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
startDrain(io, rate) {
|
|
|
|
// Clear out any old interval
|
|
|
|
let pollingInterval;
|
|
|
|
clearInterval(this.interval);
|
|
|
|
logger.log({rate}, "starting drain");
|
|
|
|
if (rate === 0) {
|
|
|
|
return;
|
|
|
|
} else if (rate < 1) {
|
|
|
|
// allow lower drain rates
|
|
|
|
// e.g. rate=0.1 will drain one client every 10 seconds
|
|
|
|
pollingInterval = 1000 / rate;
|
|
|
|
rate = 1;
|
|
|
|
} else {
|
|
|
|
pollingInterval = 1000;
|
|
|
|
}
|
|
|
|
return this.interval = setInterval(() => {
|
|
|
|
return this.reconnectNClients(io, rate);
|
|
|
|
}
|
|
|
|
, pollingInterval);
|
|
|
|
},
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
RECONNECTED_CLIENTS: {},
|
|
|
|
reconnectNClients(io, N) {
|
|
|
|
let drainedCount = 0;
|
2020-06-23 13:29:38 -04:00
|
|
|
for (const client of Array.from(io.sockets.clients())) {
|
2020-06-23 13:29:34 -04:00
|
|
|
if (!this.RECONNECTED_CLIENTS[client.id]) {
|
|
|
|
this.RECONNECTED_CLIENTS[client.id] = true;
|
|
|
|
logger.log({client_id: client.id}, "Asking client to reconnect gracefully");
|
|
|
|
client.emit("reconnectGracefully");
|
|
|
|
drainedCount++;
|
|
|
|
}
|
|
|
|
const haveDrainedNClients = (drainedCount === N);
|
|
|
|
if (haveDrainedNClients) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (drainedCount < N) {
|
|
|
|
return logger.log("All clients have been told to reconnectGracefully");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|