1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add channel_prefix support to ActionCable redis/evented_redis adapters.

This commit is contained in:
Chad Ingram 2017-01-17 23:20:36 -05:00
parent 5ac8af2033
commit a9c4dcee8d
8 changed files with 72 additions and 0 deletions

View file

@ -4,5 +4,6 @@ module ActionCable
autoload :Base
autoload :SubscriberMap
autoload :ChannelPrefix
end
end

View file

@ -0,0 +1,26 @@
module ActionCable
module SubscriptionAdapter
module ChannelPrefix # :nodoc:
def broadcast(channel, payload)
channel = channel_with_prefix(channel)
super
end
def subscribe(channel, callback, success_callback = nil)
channel = channel_with_prefix(channel)
super
end
def unsubscribe(channel, callback)
channel = channel_with_prefix(channel)
super
end
private
# Returns the channel name, including channel_prefix specified in cable.yml
def channel_with_prefix(channel)
[@server.config.cable[:channel_prefix], channel].compact.join(":")
end
end
end
end

View file

@ -11,6 +11,8 @@ EventMachine.kqueue if EventMachine.kqueue?
module ActionCable
module SubscriptionAdapter
class EventedRedis < Base # :nodoc:
prepend ChannelPrefix
@@mutex = Mutex.new
# Overwrite this factory method for EventMachine Redis connections if you want to use a different Redis connection library than EM::Hiredis.

View file

@ -6,6 +6,8 @@ require "redis"
module ActionCable
module SubscriptionAdapter
class Redis < Base # :nodoc:
prepend ChannelPrefix
# Overwrite this factory method for redis connections if you want to use a different Redis library than Redis.
# This is needed, for example, when using Makara proxies for distributed Redis.
cattr_accessor(:redis_connector) { ->(config) { ::Redis.new(url: config[:url]) } }

View file

@ -0,0 +1,36 @@
require "test_helper"
class ActionCable::Server::WithIndependentConfig < ActionCable::Server::Base
# ActionCable::Server::Base defines config as a class variable.
# Need config to be an instance variable here as we're testing 2 separate configs
def config
@config ||= ActionCable::Server::Configuration.new
end
end
module ChannelPrefixTest
def test_channel_prefix
server2 = ActionCable::Server::WithIndependentConfig.new
server2.config.cable = alt_cable_config
server2.config.logger = Logger.new(StringIO.new).tap { |l| l.level = Logger::UNKNOWN }
adapter_klass = server2.config.pubsub_adapter
rx_adapter2 = adapter_klass.new(server2)
tx_adapter2 = adapter_klass.new(server2)
subscribe_as_queue("channel") do |queue|
subscribe_as_queue("channel", rx_adapter2) do |queue2|
@tx_adapter.broadcast("channel", "hello world")
tx_adapter2.broadcast("channel", "hello world 2")
assert_equal "hello world", queue.pop
assert_equal "hello world 2", queue2.pop
end
end
end
def alt_cable_config
cable_config.merge(channel_prefix: "foo")
end
end

View file

@ -1,8 +1,10 @@
require "test_helper"
require_relative "./common"
require_relative "./channel_prefix"
class EventedRedisAdapterTest < ActionCable::TestCase
include CommonSubscriptionAdapterTest
include ChannelPrefixTest
def setup
super

View file

@ -1,8 +1,10 @@
require "test_helper"
require_relative "./common"
require_relative "./channel_prefix"
class RedisAdapterTest < ActionCable::TestCase
include CommonSubscriptionAdapterTest
include ChannelPrefixTest
def cable_config
{ adapter: "redis", driver: "ruby", url: "redis://127.0.0.1:6379/12" }

View file

@ -7,3 +7,4 @@ test:
production:
adapter: redis
url: redis://localhost:6379/1
channel_prefix: <%= app_name %>_production