mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
add createMonitoredSubscriptionClient method
This commit is contained in:
parent
42cbaecbe0
commit
1ee287ed98
1 changed files with 31 additions and 0 deletions
|
@ -20,6 +20,7 @@ module.exports = RedisSharelatex =
|
||||||
client = require("redis").createClient opts.port, opts.host, standardOpts
|
client = require("redis").createClient opts.port, opts.host, standardOpts
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
# DO NOT USE. Not robust at all (ends up getting subscribed twice, etc)!
|
||||||
createRobustSubscriptionClient: (opts, heartbeatOpts = {}) ->
|
createRobustSubscriptionClient: (opts, heartbeatOpts = {}) ->
|
||||||
sub = RedisSharelatex.createClient(opts)
|
sub = RedisSharelatex.createClient(opts)
|
||||||
pub = RedisSharelatex.createClient(opts)
|
pub = RedisSharelatex.createClient(opts)
|
||||||
|
@ -65,6 +66,36 @@ module.exports = RedisSharelatex =
|
||||||
|
|
||||||
return sub
|
return sub
|
||||||
|
|
||||||
|
# Attaches an isAlive() method to the subsciption client which returns true
|
||||||
|
# so long as we have had a heartbeat recently.
|
||||||
|
createMonitoredSubscriptionClient: (opts, heartbeatOpts = {}) ->
|
||||||
|
sub = RedisSharelatex.createClient(opts)
|
||||||
|
pub = RedisSharelatex.createClient(opts)
|
||||||
|
|
||||||
|
heartbeatInterval = heartbeatOpts.heartbeat_interval or 2000 #ms
|
||||||
|
isAliveTimeout = heartbeatOpts.is_alive_timeout or 10000 #ms
|
||||||
|
|
||||||
|
id = require("crypto").pseudoRandomBytes(16).toString("hex")
|
||||||
|
heartbeatChannel = "heartbeat-#{id}"
|
||||||
|
lastHeartbeat = Date.now()
|
||||||
|
|
||||||
|
sub.subscribe heartbeatChannel, (error) ->
|
||||||
|
if error?
|
||||||
|
console.error "ERROR: failed to subscribe to #{heartbeatChannel} channel", error
|
||||||
|
sub.on "message", (channel, message) ->
|
||||||
|
if channel == heartbeatChannel
|
||||||
|
lastHeartbeat = Date.now()
|
||||||
|
|
||||||
|
sub.isAlive = () ->
|
||||||
|
timeSinceLastHeartbeat = Date.now() - lastHeartbeat
|
||||||
|
return (timeSinceLastHeartbeat < isAliveTimeout)
|
||||||
|
|
||||||
|
setInterval () ->
|
||||||
|
pub.publish heartbeatChannel, "PING"
|
||||||
|
, heartbeatInterval
|
||||||
|
|
||||||
|
return sub
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue