2020-06-23 13:29:44 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
module.exports = {
|
2020-06-23 13:29:44 -04:00
|
|
|
startDrainTimeWindow(io, minsToDrain) {
|
|
|
|
const drainPerMin = io.sockets.clients().length / minsToDrain
|
2020-07-07 06:06:02 -04:00
|
|
|
// enforce minimum drain rate
|
|
|
|
this.startDrain(io, Math.max(drainPerMin / 60, 4))
|
|
|
|
},
|
2019-08-13 06:12:04 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
startDrain(io, rate) {
|
|
|
|
// Clear out any old interval
|
|
|
|
clearInterval(this.interval)
|
|
|
|
logger.log({ rate }, 'starting drain')
|
|
|
|
if (rate === 0) {
|
|
|
|
return
|
2020-07-07 06:06:02 -04:00
|
|
|
}
|
|
|
|
let pollingInterval
|
|
|
|
if (rate < 1) {
|
2020-06-23 13:29:44 -04:00
|
|
|
// allow lower drain rates
|
|
|
|
// e.g. rate=0.1 will drain one client every 10 seconds
|
|
|
|
pollingInterval = 1000 / rate
|
|
|
|
rate = 1
|
|
|
|
} else {
|
|
|
|
pollingInterval = 1000
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
this.interval = setInterval(() => {
|
|
|
|
this.reconnectNClients(io, rate)
|
|
|
|
}, pollingInterval)
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
2019-08-13 06:12:04 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
RECONNECTED_CLIENTS: {},
|
|
|
|
reconnectNClients(io, N) {
|
|
|
|
let drainedCount = 0
|
2020-07-07 06:06:02 -04:00
|
|
|
for (const client of io.sockets.clients()) {
|
2020-06-23 13:29:44 -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) {
|
2020-07-07 06:06:02 -04:00
|
|
|
logger.log('All clients have been told to reconnectGracefully')
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|