diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2015-12-14 15:48:54 +0100 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2015-12-14 15:48:54 +0100 |
commit | bf40bddfceebaff637161be6c5d992d6978679ff (patch) | |
tree | b3541d5ec5015ab9459d1970a9f10fd37c0156c3 /actioncable/lib/action_cable/server/connections.rb | |
parent | 4073a3e3fe77141d09ed767224a1089796de2f7d (diff) | |
download | rails-bf40bddfceebaff637161be6c5d992d6978679ff.tar.gz rails-bf40bddfceebaff637161be6c5d992d6978679ff.tar.bz2 rails-bf40bddfceebaff637161be6c5d992d6978679ff.zip |
Get ready to merge into Rails
Diffstat (limited to 'actioncable/lib/action_cable/server/connections.rb')
-rw-r--r-- | actioncable/lib/action_cable/server/connections.rb | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/actioncable/lib/action_cable/server/connections.rb b/actioncable/lib/action_cable/server/connections.rb new file mode 100644 index 0000000000..47dcea8c20 --- /dev/null +++ b/actioncable/lib/action_cable/server/connections.rb @@ -0,0 +1,37 @@ +module ActionCable + module Server + # Collection class for all the connections that's been established on this specific server. Remember, usually you'll run many cable servers, so + # 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 + + def add_connection(connection) + connections << connection + end + + def remove_connection(connection) + 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 + EM.next_tick do + @heartbeat_timer ||= EventMachine.add_periodic_timer(BEAT_INTERVAL) do + EM.next_tick { connections.map(&:beat) } + end + end + end + + def open_connections_statistics + connections.map(&:statistics) + end + end + end +end |