mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Tweak reconnect timing
This commit is contained in:
parent
889d8ae3d7
commit
c0e554c943
1 changed files with 15 additions and 14 deletions
|
@ -1,15 +1,13 @@
|
||||||
# Responsible for ensuring the cable connection is in good health by validating the heartbeat pings sent from the server, and attempting
|
# Responsible for ensuring the cable connection is in good health by validating the heartbeat pings sent from the server, and attempting
|
||||||
# revival reconnections if things go astray. Internal class, not intended for direct user manipulation.
|
# revival reconnections if things go astray. Internal class, not intended for direct user manipulation.
|
||||||
class Cable.ConnectionMonitor
|
class Cable.ConnectionMonitor
|
||||||
identifier: Cable.PING_IDENTIFIER
|
@pollInterval:
|
||||||
|
min: 3
|
||||||
pollInterval:
|
|
||||||
min: 2
|
|
||||||
max: 30
|
max: 30
|
||||||
|
|
||||||
staleThreshold:
|
@staleThreshold: 6 # Server::Connections::BEAT_INTERVAL * 2 (missed two pings)
|
||||||
startedAt: 4
|
|
||||||
pingedAt: 8
|
identifier: Cable.PING_IDENTIFIER
|
||||||
|
|
||||||
constructor: (@consumer) ->
|
constructor: (@consumer) ->
|
||||||
@consumer.subscriptions.add(this)
|
@consumer.subscriptions.add(this)
|
||||||
|
@ -18,8 +16,10 @@ class Cable.ConnectionMonitor
|
||||||
connected: ->
|
connected: ->
|
||||||
@reset()
|
@reset()
|
||||||
@pingedAt = now()
|
@pingedAt = now()
|
||||||
|
delete @disconnectedAt
|
||||||
|
|
||||||
disconnected: ->
|
disconnected: ->
|
||||||
|
@disconnectedAt = now()
|
||||||
|
|
||||||
received: ->
|
received: ->
|
||||||
@pingedAt = now()
|
@pingedAt = now()
|
||||||
|
@ -46,20 +46,21 @@ class Cable.ConnectionMonitor
|
||||||
, @getInterval()
|
, @getInterval()
|
||||||
|
|
||||||
getInterval: ->
|
getInterval: ->
|
||||||
{min, max} = @pollInterval
|
{min, max} = @constructor.pollInterval
|
||||||
interval = 4 * Math.log(@reconnectAttempts + 1)
|
interval = 5 * Math.log(@reconnectAttempts + 1)
|
||||||
clamp(interval, min, max) * 1000
|
clamp(interval, min, max) * 1000
|
||||||
|
|
||||||
reconnectIfStale: ->
|
reconnectIfStale: ->
|
||||||
if @connectionIsStale()
|
if @connectionIsStale()
|
||||||
@reconnectAttempts++
|
@reconnectAttempts++
|
||||||
@consumer.connection.reopen()
|
unless @disconnectedRecently()
|
||||||
|
@consumer.connection.reopen()
|
||||||
|
|
||||||
connectionIsStale: ->
|
connectionIsStale: ->
|
||||||
if @pingedAt
|
secondsSince(@pingedAt ? @startedAt) > @constructor.staleThreshold
|
||||||
secondsSince(@pingedAt) > @staleThreshold.pingedAt
|
|
||||||
else
|
disconnectedRecently: ->
|
||||||
secondsSince(@startedAt) > @staleThreshold.startedAt
|
@disconnectedAt and secondsSince(@disconnectedAt) < @constructor.staleThreshold
|
||||||
|
|
||||||
visibilityDidChange: =>
|
visibilityDidChange: =>
|
||||||
if document.visibilityState is "visible"
|
if document.visibilityState is "visible"
|
||||||
|
|
Loading…
Reference in a new issue