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

refactor channel look up to use a hash instead of an array and reduce the number of calls to safe_constantize because it can be slow

This commit is contained in:
Lachlan Sylvester 2015-07-28 15:13:07 +10:00
parent 65fde3bf75
commit 60e2fa5e95
3 changed files with 4 additions and 6 deletions

View file

@ -24,9 +24,7 @@ module ActionCable
id_key = data['identifier']
id_options = ActiveSupport::JSON.decode(id_key).with_indifferent_access
subscription_klass = connection.server.channel_classes.detect do |channel_class|
channel_class == id_options[:channel].safe_constantize
end
subscription_klass = connection.server.channel_classes[id_options[:channel]]
if subscription_klass
subscriptions[id_key] ||= subscription_klass.new(connection, id_key, id_options)

View file

@ -36,11 +36,11 @@ module ActionCable
@worker_pool ||= ActionCable::Server::Worker.pool(size: config.worker_pool_size)
end
# Requires and returns an array of all the channel class constants in this application.
# Requires and returns an hash of all the channel class constants keyed by name.
def channel_classes
@channel_classes ||= begin
config.channel_paths.each { |channel_path| require channel_path }
config.channel_class_names.collect { |name| name.constantize }
config.channel_class_names.each_with_object({}) { |name, hash| hash[name] = name.constantize }
end
end

View file

@ -20,7 +20,7 @@ class ActionCable::Connection::SubscriptionsTest < ActiveSupport::TestCase
setup do
@server = TestServer.new
@server.stubs(:channel_classes).returns([ ChatChannel ])
@server.stubs(:channel_classes).returns(ChatChannel.name => ChatChannel)
env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket'
@connection = Connection.new(@server, env)