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

Allow passing custom config to ActionCable::Server::Base

That allows us to create a separate, isolated Action Cable server
instance within the same app.
This commit is contained in:
Vladimir Dementyev 2018-12-14 16:46:39 -05:00
parent 7432e25187
commit 3cd69fa2c0
No known key found for this signature in database
GPG key ID: 8E0A19D3D1EDF5EB
3 changed files with 29 additions and 11 deletions

View file

@ -1,3 +1,26 @@
* Allow passing custom configuration to `ActionCable::Server::Base`.
You can now create a standalone Action Cable server with a custom configuration
(e.g. to run it in isolation from the default one):
```ruby
config = ActionCable::Server::Configuration.new
config.cable = { adapter: "redis", channel_prefix: "custom_" }
CUSTOM_CABLE = ActionCable::Server::Base.new(config: config)
```
Then you can mount it in the `routes.rb` file:
```ruby
Rails.application.routes.draw do
mount CUSTOM_CABLE => "/custom_cable"
# ...
end
```
*Vladimir Dementyev*
* Add `:action_cable_connection` and `:action_cable_channel` load hooks.
You can use them to extend `ActionCable::Connection::Base` and `ActionCable::Channel::Base`

View file

@ -12,14 +12,17 @@ module ActionCable
include ActionCable::Server::Broadcasting
include ActionCable::Server::Connections
cattr_accessor :config, instance_accessor: true, default: ActionCable::Server::Configuration.new
cattr_accessor :config, instance_accessor: false, default: ActionCable::Server::Configuration.new
attr_reader :config
def self.logger; config.logger; end
delegate :logger, to: :config
attr_reader :mutex
def initialize
def initialize(config: self.class.config)
@config = config
@mutex = Monitor.new
@remote_connections = @event_loop = @worker_pool = @pubsub = nil
end

View file

@ -2,17 +2,9 @@
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 = ActionCable::Server::Base.new(config: ActionCable::Server::Configuration.new)
server2.config.cable = alt_cable_config
server2.config.logger = Logger.new(StringIO.new).tap { |l| l.level = Logger::UNKNOWN }