Move EventMachine initialization to Runner::EventLoop

This commit is contained in:
Felix Auringer
2021-07-27 13:40:50 +02:00
committed by Sebastian Serth
parent 9e2cff7558
commit e752df1b3c
2 changed files with 19 additions and 17 deletions

View File

@ -5,6 +5,7 @@
class Runner::EventLoop
def initialize
@queue = Queue.new
ensure_event_machine
end
# wait waits until another thread calls stop on this EventLoop.
@ -19,4 +20,22 @@ class Runner::EventLoop
def stop
@queue.push nil if @queue.empty?
end
private
# If there are multiple threads trying to connect to the WebSocket of their execution at the same time,
# the Faye WebSocket connections will use the same reactor. We therefore only need to start an EventMachine
# if there isn't a running reactor yet.
# See this StackOverflow answer: https://stackoverflow.com/a/8247947
def ensure_event_machine
unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
queue = Queue.new
Thread.new do
EventMachine.run { queue.push nil }
ensure
ActiveRecord::Base.connection_pool.release_connection
end
queue.pop
end
end
end