aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/server/base.rb
blob: 6dcd282e4a533179b3abc7294a71b6e7e4379a43 (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
module ActionCable
  module Server
    class Base
      include ActionCable::Server::Broadcasting

      cattr_accessor(:logger, instance_reader: true) { Rails.logger }

      attr_accessor :registered_channels, :redis_config, :log_tags

      def initialize(redis_config:, channels:, worker_pool_size: 100, connection: Connection, log_tags: [ 'ActionCable' ])
        @redis_config = redis_config.with_indifferent_access
        @registered_channels = Set.new(channels)
        @worker_pool_size = worker_pool_size
        @connection_class = connection
        @log_tags = log_tags

        @connections = []

        logger.info "[ActionCable] Initialized server (redis_config: #{@redis_config.inspect}, worker_pool_size: #{@worker_pool_size})"
      end

      def call(env)
        @connection_class.new(self, env).process
      end

      def worker_pool
        @worker_pool ||= ActionCable::Server::Worker.pool(size: @worker_pool_size)
      end

      def pubsub
        @pubsub ||= redis.pubsub
      end

      def redis
        @redis ||= begin
          redis = EM::Hiredis.connect(@redis_config[:url])
          redis.on(:reconnect_failed) do
            logger.info "[ActionCable] Redis reconnect failed."
            # logger.info "[ActionCable] Redis reconnected. Closing all the open connections."
            # @connections.map &:close
          end
          redis
        end
      end

      def threaded_redis
        @threaded_redis ||= Redis.new(redis_config)
      end

      def remote_connections
        @remote_connections ||= RemoteConnections.new(self)
      end

      def connection_identifiers
        @connection_class.identifiers
      end

      def add_connection(connection)
        @connections << connection
      end

      def remove_connection(connection)
        @connections.delete connection
      end

      def open_connections_statistics
        @connections.map(&:statistics)
      end
    end
  end
end