aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/lib/action_cable/server
diff options
context:
space:
mode:
authorJon Moss <me@jonathanmoss.me>2016-01-06 17:16:02 -0500
committerJon Moss <me@jonathanmoss.me>2016-01-18 18:58:57 -0500
commit0016e0410b11d40a1d730a1232c40f428d67abeb (patch)
tree172b2fe1e0e2424a2eab33c95ec38487d5b5996d /actioncable/lib/action_cable/server
parent75f1b229fdb340b2cdaf632bb6a223213f05dc75 (diff)
downloadrails-0016e0410b11d40a1d730a1232c40f428d67abeb.tar.gz
rails-0016e0410b11d40a1d730a1232c40f428d67abeb.tar.bz2
rails-0016e0410b11d40a1d730a1232c40f428d67abeb.zip
Adapterize ActionCable storage and extract behavior
Diffstat (limited to 'actioncable/lib/action_cable/server')
-rw-r--r--actioncable/lib/action_cable/server/base.rb17
-rw-r--r--actioncable/lib/action_cable/server/broadcasting.rb10
-rw-r--r--actioncable/lib/action_cable/server/configuration.rb20
3 files changed, 22 insertions, 25 deletions
diff --git a/actioncable/lib/action_cable/server/base.rb b/actioncable/lib/action_cable/server/base.rb
index 3785bbd154..6539745c79 100644
--- a/actioncable/lib/action_cable/server/base.rb
+++ b/actioncable/lib/action_cable/server/base.rb
@@ -1,5 +1,3 @@
-require 'em-hiredis'
-
module ActionCable
module Server
# A singleton ActionCable::Server instance is available via ActionCable.server. It's used by the rack process that starts the cable server, but
@@ -47,20 +45,9 @@ module ActionCable
end
end
- # The redis pubsub adapter used for all streams/broadcasting.
+ # The pubsub adapter used for all streams/broadcasting.
def pubsub
- @pubsub ||= redis.pubsub
- end
-
- # The EventMachine Redis instance used by the pubsub adapter.
- def redis
- @redis ||= EM::Hiredis.connect(config.redis[:url]).tap do |redis|
- 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
- end
+ @pubsub ||= config.storage_adapter.new(self).pubsub
end
# All the identifiers applied to the connection class associated with this server.
diff --git a/actioncable/lib/action_cable/server/broadcasting.rb b/actioncable/lib/action_cable/server/broadcasting.rb
index c759239a0e..847ef50971 100644
--- a/actioncable/lib/action_cable/server/broadcasting.rb
+++ b/actioncable/lib/action_cable/server/broadcasting.rb
@@ -1,5 +1,3 @@
-require 'redis'
-
module ActionCable
module Server
# Broadcasting is how other parts of your application can send messages to the channel subscribers. As explained in Channel, most of the time, these
@@ -31,11 +29,6 @@ module ActionCable
Broadcaster.new(self, broadcasting)
end
- # The redis instance used for broadcasting. Not intended for direct user use.
- def broadcasting_redis
- @broadcasting_redis ||= Redis.new(config.redis)
- end
-
private
class Broadcaster
attr_reader :server, :broadcasting
@@ -46,7 +39,8 @@ module ActionCable
def broadcast(message)
server.logger.info "[ActionCable] Broadcasting to #{broadcasting}: #{message}"
- server.broadcasting_redis.publish broadcasting, ActiveSupport::JSON.encode(message)
+ broadcast_storage_adapter = server.config.storage_adapter.new(server).broadcast
+ broadcast_storage_adapter.publish broadcasting, ActiveSupport::JSON.encode(message)
end
end
end
diff --git a/actioncable/lib/action_cable/server/configuration.rb b/actioncable/lib/action_cable/server/configuration.rb
index 935133cbba..2bed5a9ea2 100644
--- a/actioncable/lib/action_cable/server/configuration.rb
+++ b/actioncable/lib/action_cable/server/configuration.rb
@@ -5,9 +5,9 @@ module ActionCable
class Configuration
attr_accessor :logger, :log_tags
attr_accessor :connection_class, :worker_pool_size
- attr_accessor :redis, :channels_path
+ attr_accessor :channels_path
attr_accessor :disable_request_forgery_protection, :allowed_request_origins
- attr_accessor :url
+ attr_accessor :config_opts, :url
def initialize
@log_tags = []
@@ -29,6 +29,22 @@ module ActionCable
Pathname.new(channel_path).basename.to_s.split('.').first.camelize
end
end
+
+ ADAPTER = ActionCable::StorageAdapter
+
+ # Returns constant of storage adapter specified in config/cable.yml
+ # If the adapter cannot be found, this will default to the Redis adapter
+ def storage_adapter
+ # "ActionCable::StorageAdapter::#{adapter.capitalize}"
+ adapter = config_opts['adapter']
+ adapter_const = "ActionCable::StorageAdapter::#{adapter.capitalize}"
+
+ if Object.const_defined?(adapter_const)
+ adapter_const.constantize
+ else
+ ADAPTER_BASE::Redis
+ end
+ end
end
end
end