2016-10-24 11:36:09 -04:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
|
2019-08-13 06:12:04 -04:00
|
|
|
module.exports = DrainManager =
|
|
|
|
|
|
|
|
startDrainTimeWindow: (io, minsToDrain)->
|
|
|
|
drainPerMin = io.sockets.clients().length / minsToDrain
|
2019-08-13 11:15:30 -04:00
|
|
|
DrainManager.startDrain(io, Math.max(drainPerMin / 60, 4)) # enforce minimum drain rate
|
2019-08-13 06:12:04 -04:00
|
|
|
|
2016-10-24 11:36:09 -04:00
|
|
|
startDrain: (io, rate) ->
|
|
|
|
# Clear out any old interval
|
|
|
|
clearInterval @interval
|
2019-08-15 09:41:22 -04:00
|
|
|
logger.log rate: rate, "starting drain"
|
2016-10-24 11:36:09 -04:00
|
|
|
if rate == 0
|
|
|
|
return
|
2019-05-24 10:23:01 -04:00
|
|
|
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
|
2016-10-24 11:36:09 -04:00
|
|
|
@interval = setInterval () =>
|
|
|
|
@reconnectNClients(io, rate)
|
2019-05-24 10:23:01 -04:00
|
|
|
, pollingInterval
|
2016-10-24 11:36:09 -04:00
|
|
|
|
|
|
|
RECONNECTED_CLIENTS: {}
|
|
|
|
reconnectNClients: (io, N) ->
|
|
|
|
drainedCount = 0
|
|
|
|
for client in io.sockets.clients()
|
|
|
|
if !@RECONNECTED_CLIENTS[client.id]
|
|
|
|
@RECONNECTED_CLIENTS[client.id] = true
|
|
|
|
logger.log {client_id: client.id}, "Asking client to reconnect gracefully"
|
2020-02-04 07:13:03 -05:00
|
|
|
try
|
|
|
|
client.emit "reconnectGracefully"
|
|
|
|
catch err
|
|
|
|
logger.warn client_id: client.id, err: err, "error asking client to reconnect gracefully"
|
2016-10-24 11:36:09 -04:00
|
|
|
drainedCount++
|
2016-10-24 11:54:56 -04:00
|
|
|
haveDrainedNClients = (drainedCount == N)
|
|
|
|
if haveDrainedNClients
|
2016-10-24 11:36:09 -04:00
|
|
|
break
|
|
|
|
if drainedCount < N
|
2019-10-17 07:45:56 -04:00
|
|
|
logger.log "All clients have been told to reconnectGracefully"
|