From 60e2fa5e955ce819c90f2081320554b5ed0ee83c Mon Sep 17 00:00:00 2001 From: Lachlan Sylvester Date: Tue, 28 Jul 2015 15:13:07 +1000 Subject: 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 --- lib/action_cable/connection/subscriptions.rb | 4 +--- lib/action_cable/server/base.rb | 4 ++-- test/connection/subscriptions_test.rb | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/action_cable/connection/subscriptions.rb b/lib/action_cable/connection/subscriptions.rb index 0411d96413..69e3f60706 100644 --- a/lib/action_cable/connection/subscriptions.rb +++ b/lib/action_cable/connection/subscriptions.rb @@ -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) diff --git a/lib/action_cable/server/base.rb b/lib/action_cable/server/base.rb index b09fbf6da4..43849928b9 100644 --- a/lib/action_cable/server/base.rb +++ b/lib/action_cable/server/base.rb @@ -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 diff --git a/test/connection/subscriptions_test.rb b/test/connection/subscriptions_test.rb index 4e134b6420..24fe8f9300 100644 --- a/test/connection/subscriptions_test.rb +++ b/test/connection/subscriptions_test.rb @@ -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) -- cgit v1.2.3