123456789_123456789_123456789_123456789_123456789_

Module: ActionCable::Server::Connections

Do not use. This module is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: actioncable/lib/action_cable/server/connections.rb

Overview

Collection class for all the connections that have been established on this specific server. Remember, usually you'll run many Action Cable servers, so you can't use this collection as a full list of all of the connections established against your application. Instead, use ::ActionCable::RemoteConnections for that.

Constant Summary

Instance Method Summary

Instance Method Details

#add_connection(connection)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/server/connections.rb', line 25

def add_connection(connection)
  connections_map[connection.object_id] = connection
end

#connections

[ GitHub ]

  
# File 'actioncable/lib/action_cable/server/connections.rb', line 16

def connections = connections_map.values

#connections_map (private)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/server/connections.rb', line 49

def connections_map
  @connections_map ||= {}
end

#each_connection

[ GitHub ]

  
# File 'actioncable/lib/action_cable/server/connections.rb', line 18

def each_connection(...)
  # Iterate a snapshot: the heartbeat, #restart and statistics all walk the
  # live connections while worker threads concurrently add/remove entries,
  # and mutating a Hash mid-iteration raises a RuntimeError.
  connections.each(...)
end

#open_connections_statistics

[ GitHub ]

  
# File 'actioncable/lib/action_cable/server/connections.rb', line 44

def open_connections_statistics
  each_connection.map(&:statistics)
end

#remove_connection(connection)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/server/connections.rb', line 29

def remove_connection(connection)
  connections_map.delete connection.object_id
end

#setup_heartbeat_timer

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 communicate with the connection. To solve this, a 3 second heartbeat runs on all connections. If the beat fails, we automatically disconnect.

[ GitHub ]

  
# File 'actioncable/lib/action_cable/server/connections.rb', line 38

def setup_heartbeat_timer
  @heartbeat_timer ||= executor.timer(BEAT_INTERVAL) do
    executor.post { each_connection(&:beat) }
  end
end