diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2015-10-08 14:30:58 -0500 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2015-10-08 15:50:11 -0500 |
commit | b099a7d2705428a4434813079f399dec54ec7611 (patch) | |
tree | 481ff38b1db2939f32a8ecb639f4e3b118f66417 /lib/action_cable/server | |
parent | 91d6db34357634eaa0903b5c05a5db8b10066366 (diff) | |
download | rails-b099a7d2705428a4434813079f399dec54ec7611.tar.gz rails-b099a7d2705428a4434813079f399dec54ec7611.tar.bz2 rails-b099a7d2705428a4434813079f399dec54ec7611.zip |
Run a single eventmachine timer to send heartbeats
Diffstat (limited to 'lib/action_cable/server')
-rw-r--r-- | lib/action_cable/server/base.rb | 1 | ||||
-rw-r--r-- | lib/action_cable/server/connections.rb | 11 |
2 files changed, 12 insertions, 0 deletions
diff --git a/lib/action_cable/server/base.rb b/lib/action_cable/server/base.rb index 43849928b9..9315a48f20 100644 --- a/lib/action_cable/server/base.rb +++ b/lib/action_cable/server/base.rb @@ -18,6 +18,7 @@ module ActionCable # Called by rack to setup the server. def call(env) + setup_heartbeat_timer config.connection_class.new(self, env).process end diff --git a/lib/action_cable/server/connections.rb b/lib/action_cable/server/connections.rb index 15d7c3c8c7..455ff1ab29 100644 --- a/lib/action_cable/server/connections.rb +++ b/lib/action_cable/server/connections.rb @@ -4,6 +4,8 @@ module ActionCable # you can't use this collection as an full list of all the connections established against your application. Use RemoteConnections for that. # As such, this is primarily for internal use. module Connections + BEAT_INTERVAL = 3 + def connections @connections ||= [] end @@ -16,6 +18,15 @@ module ActionCable connections.delete connection end + # Websocket connection implementations differ on when they'll mark a connection as stale. We basically never want a connection to go stale, as you + # then can't rely on being able to receive and send to it. So there's a 3 second heartbeat running on all connections. If the beat fails, we automatically + # disconnect. + def setup_heartbeat_timer + @heartbeat_timer ||= EventMachine.add_periodic_timer(BEAT_INTERVAL) do + EM.next_tick { connections.map &:beat } + end + end + def open_connections_statistics connections.map(&:statistics) end |