aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJavan Makhmali <javan@javan.us>2015-10-14 18:07:08 -0500
committerJavan Makhmali <javan@javan.us>2015-10-14 18:07:08 -0500
commitc0e554c9432e688935d129a18c0288a518faecc7 (patch)
tree84cc0050aab5cd591fa664fada30fcf21b05658b /lib
parent889d8ae3d702004eef6f42c00f31538e16e09fbb (diff)
downloadrails-c0e554c9432e688935d129a18c0288a518faecc7.tar.gz
rails-c0e554c9432e688935d129a18c0288a518faecc7.tar.bz2
rails-c0e554c9432e688935d129a18c0288a518faecc7.zip
Tweak reconnect timing
Diffstat (limited to 'lib')
-rw-r--r--lib/assets/javascripts/cable/connection_monitor.coffee29
1 files changed, 15 insertions, 14 deletions
diff --git a/lib/assets/javascripts/cable/connection_monitor.coffee b/lib/assets/javascripts/cable/connection_monitor.coffee
index b8be94ae60..bf99dee34d 100644
--- a/lib/assets/javascripts/cable/connection_monitor.coffee
+++ b/lib/assets/javascripts/cable/connection_monitor.coffee
@@ -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
# revival reconnections if things go astray. Internal class, not intended for direct user manipulation.
class Cable.ConnectionMonitor
- identifier: Cable.PING_IDENTIFIER
-
- pollInterval:
- min: 2
+ @pollInterval:
+ min: 3
max: 30
- staleThreshold:
- startedAt: 4
- pingedAt: 8
+ @staleThreshold: 6 # Server::Connections::BEAT_INTERVAL * 2 (missed two pings)
+
+ identifier: Cable.PING_IDENTIFIER
constructor: (@consumer) ->
@consumer.subscriptions.add(this)
@@ -18,8 +16,10 @@ class Cable.ConnectionMonitor
connected: ->
@reset()
@pingedAt = now()
+ delete @disconnectedAt
disconnected: ->
+ @disconnectedAt = now()
received: ->
@pingedAt = now()
@@ -46,20 +46,21 @@ class Cable.ConnectionMonitor
, @getInterval()
getInterval: ->
- {min, max} = @pollInterval
- interval = 4 * Math.log(@reconnectAttempts + 1)
+ {min, max} = @constructor.pollInterval
+ interval = 5 * Math.log(@reconnectAttempts + 1)
clamp(interval, min, max) * 1000
reconnectIfStale: ->
if @connectionIsStale()
@reconnectAttempts++
- @consumer.connection.reopen()
+ unless @disconnectedRecently()
+ @consumer.connection.reopen()
connectionIsStale: ->
- if @pingedAt
- secondsSince(@pingedAt) > @staleThreshold.pingedAt
- else
- secondsSince(@startedAt) > @staleThreshold.startedAt
+ secondsSince(@pingedAt ? @startedAt) > @constructor.staleThreshold
+
+ disconnectedRecently: ->
+ @disconnectedAt and secondsSince(@disconnectedAt) < @constructor.staleThreshold
visibilityDidChange: =>
if document.visibilityState is "visible"