aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/app/assets/javascripts/action_cable/connection_monitor.coffee
blob: 0cc675fa94c6e001b3021c8da0c21c353c90f624 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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 ActionCable.ConnectionMonitor
  @pollInterval:
    min: 3
    max: 30

  @staleThreshold: 6 # Server::Connections::BEAT_INTERVAL * 2 (missed two pings)

  constructor: (@connection) ->
    @reconnectAttempts = 0

  start: ->
    unless @isRunning()
      @startedAt = now()
      delete @stoppedAt
      @startPolling()
      document.addEventListener("visibilitychange", @visibilityDidChange)
      ActionCable.log("ConnectionMonitor started. pollInterval = #{@getPollInterval()} ms")

  stop: ->
    if @isRunning()
      @stoppedAt = now()
      @stopPolling()
      document.removeEventListener("visibilitychange", @visibilityDidChange)
      ActionCable.log("ConnectionMonitor stopped")

  isRunning: ->
    @startedAt? and not @stoppedAt?

  recordPing: ->
    @pingedAt = now()

  recordConnect: ->
    @reconnectAttempts = 0
    @recordPing()
    delete @disconnectedAt
    ActionCable.log("ConnectionMonitor recorded connect")

  recordDisconnect: ->
    @disconnectedAt = now()
    ActionCable.log("ConnectionMonitor recorded disconnect")

  # Private

  startPolling: ->
    @stopPolling()
    @poll()

  stopPolling: ->
    clearTimeout(@pollTimeout)

  poll: ->
    @pollTimeout = setTimeout =>
      @reconnectIfStale()
      @poll()
    , @getPollInterval()

  getPollInterval: ->
    {min, max} = @constructor.pollInterval
    interval = 5 * Math.log(@reconnectAttempts + 1)
    Math.round(clamp(interval, min, max) * 1000)

  reconnectIfStale: ->
    if @connectionIsStale()
      ActionCable.log("ConnectionMonitor detected stale connection. reconnectAttempts = #{@reconnectAttempts}, pollInterval = #{@getPollInterval()} ms, time disconnected = #{secondsSince(@disconnectedAt)} s, stale threshold = #{@constructor.staleThreshold} s")
      @reconnectAttempts++
      if @disconnectedRecently()
        ActionCable.log("ConnectionMonitor skipping reopening recent disconnect")
      else
        ActionCable.log("ConnectionMonitor reopening")
        @connection.reopen()

  connectionIsStale: ->
    secondsSince(@pingedAt ? @startedAt) > @constructor.staleThreshold

  disconnectedRecently: ->
    @disconnectedAt and secondsSince(@disconnectedAt) < @constructor.staleThreshold

  visibilityDidChange: =>
    if document.visibilityState is "visible"
      setTimeout =>
        if @connectionIsStale() or not @connection.isOpen()
          ActionCable.log("ConnectionMonitor reopening stale connection on visibilitychange. visbilityState = #{document.visibilityState}")
          @connection.reopen()
      , 200

  now = ->
    new Date().getTime()

  secondsSince = (time) ->
    (now() - time) / 1000

  clamp = (number, min, max) ->
    Math.max(min, Math.min(max, number))