From 547713b4c91e5c232ac3b2a8288b72001c6dfc46 Mon Sep 17 00:00:00 2001 From: Mike Perham Date: Tue, 5 Jan 2016 14:31:16 -0800 Subject: Move async execution from celluloid to concurrent-ruby This removes 8 runtime gem dependencies from Rails: ``` Using hitimes 1.2.3 Using timers 4.1.1 Using celluloid-essentials 0.20.5 Using celluloid-extras 0.20.5 Using celluloid-fsm 0.20.5 Using celluloid-pool 0.20.5 Using celluloid-supervision 0.20.5 Using celluloid 0.17.2 ``` --- actioncable/lib/action_cable/server/worker.rb | 57 +++++++++++++++++++++------ 1 file changed, 44 insertions(+), 13 deletions(-) (limited to 'actioncable/lib/action_cable/server/worker.rb') diff --git a/actioncable/lib/action_cable/server/worker.rb b/actioncable/lib/action_cable/server/worker.rb index e063b2a2e1..6cddb4e7a5 100644 --- a/actioncable/lib/action_cable/server/worker.rb +++ b/actioncable/lib/action_cable/server/worker.rb @@ -1,39 +1,70 @@ -require 'celluloid' require 'active_support/callbacks' +require 'concurrent' module ActionCable module Server # Worker used by Server.send_async to do connection work in threads. Only for internal use. class Worker include ActiveSupport::Callbacks - include Celluloid - attr_reader :connection define_callbacks :work include ActiveRecordConnectionManagement + def initialize(max_size=5) + @pool = Concurrent::ThreadPoolExecutor.new( + min_threads: 1, + max_threads: max_size, + max_queue: 0, + ) + end + + def connection + Thread.current[:connection] || raise("No connection set") + end + + def async_invoke(receiver, method, *args) + @pool.post do + invoke(receiver, method, *args) + end + end + def invoke(receiver, method, *args) - @connection = receiver + begin + Thread.current[:connection] = receiver + + run_callbacks :work do + receiver.send method, *args + end + rescue Exception => e + logger.error "There was an exception - #{e.class}(#{e.message})" + logger.error e.backtrace.join("\n") - run_callbacks :work do - receiver.send method, *args + receiver.handle_exception if receiver.respond_to?(:handle_exception) + ensure + Thread.current[:connection] = nil end - rescue Exception => e - logger.error "There was an exception - #{e.class}(#{e.message})" - logger.error e.backtrace.join("\n") + end - receiver.handle_exception if receiver.respond_to?(:handle_exception) + def async_run_periodic_timer(channel, callback) + @pool.post do + run_periodic_timer(channel, callback) + end end def run_periodic_timer(channel, callback) - @connection = channel.connection + begin + Thread.current[:connection] = channel.connection - run_callbacks :work do - callback.respond_to?(:call) ? channel.instance_exec(&callback) : channel.send(callback) + run_callbacks :work do + callback.respond_to?(:call) ? channel.instance_exec(&callback) : channel.send(callback) + end + ensure + Thread.current[:connection] = nil end end private + def logger ActionCable.server.logger end -- cgit v1.2.3 From e529e3428e2fdcbc03ab9010127c743cc276dbf2 Mon Sep 17 00:00:00 2001 From: Mike Perham Date: Tue, 5 Jan 2016 15:11:10 -0800 Subject: Use Module#thread_mattr_accessor --- actioncable/lib/action_cable/server/worker.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'actioncable/lib/action_cable/server/worker.rb') diff --git a/actioncable/lib/action_cable/server/worker.rb b/actioncable/lib/action_cable/server/worker.rb index 6cddb4e7a5..7787e99baf 100644 --- a/actioncable/lib/action_cable/server/worker.rb +++ b/actioncable/lib/action_cable/server/worker.rb @@ -1,4 +1,5 @@ require 'active_support/callbacks' +require 'active_support/core_ext/module/attribute_accessors_per_thread' require 'concurrent' module ActionCable @@ -7,6 +8,7 @@ module ActionCable class Worker include ActiveSupport::Callbacks + thread_mattr_accessor :connection define_callbacks :work include ActiveRecordConnectionManagement @@ -18,10 +20,6 @@ module ActionCable ) end - def connection - Thread.current[:connection] || raise("No connection set") - end - def async_invoke(receiver, method, *args) @pool.post do invoke(receiver, method, *args) @@ -30,7 +28,7 @@ module ActionCable def invoke(receiver, method, *args) begin - Thread.current[:connection] = receiver + self.connection = receiver run_callbacks :work do receiver.send method, *args @@ -41,7 +39,7 @@ module ActionCable receiver.handle_exception if receiver.respond_to?(:handle_exception) ensure - Thread.current[:connection] = nil + self.connection = nil end end @@ -53,13 +51,13 @@ module ActionCable def run_periodic_timer(channel, callback) begin - Thread.current[:connection] = channel.connection + self.connection = channel.connection run_callbacks :work do callback.respond_to?(:call) ? channel.instance_exec(&callback) : channel.send(callback) end ensure - Thread.current[:connection] = nil + self.connection = nil end end -- cgit v1.2.3 From cd1d7e287b09a6461d8eb5febd877602de6aca6c Mon Sep 17 00:00:00 2001 From: Mike Perham Date: Tue, 5 Jan 2016 16:26:53 -0800 Subject: don't need explicit dep and a pretty neat pick --- actioncable/lib/action_cable/server/worker.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actioncable/lib/action_cable/server/worker.rb') diff --git a/actioncable/lib/action_cable/server/worker.rb b/actioncable/lib/action_cable/server/worker.rb index 7787e99baf..3b6c6d44a1 100644 --- a/actioncable/lib/action_cable/server/worker.rb +++ b/actioncable/lib/action_cable/server/worker.rb @@ -12,7 +12,7 @@ module ActionCable define_callbacks :work include ActiveRecordConnectionManagement - def initialize(max_size=5) + def initialize(max_size: 5) @pool = Concurrent::ThreadPoolExecutor.new( min_threads: 1, max_threads: max_size, -- cgit v1.2.3