2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:13:46 -04:00
|
|
|
require "nio"
|
|
|
|
require "thread"
|
2016-01-27 23:55:31 -05:00
|
|
|
|
|
|
|
module ActionCable
|
|
|
|
module Connection
|
|
|
|
class StreamEventLoop
|
|
|
|
def initialize
|
2016-09-27 17:14:23 -04:00
|
|
|
@nio = @executor = @thread = nil
|
2016-01-27 23:55:31 -05:00
|
|
|
@map = {}
|
|
|
|
@stopping = false
|
|
|
|
@todo = Queue.new
|
|
|
|
|
2016-01-28 03:16:14 -05:00
|
|
|
@spawn_mutex = Mutex.new
|
2016-03-01 19:50:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def timer(interval, &block)
|
|
|
|
Concurrent::TimerTask.new(execution_interval: interval, &block).tap(&:execute)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post(task = nil, &block)
|
|
|
|
task ||= block
|
|
|
|
|
2016-09-27 17:14:23 -04:00
|
|
|
spawn
|
|
|
|
@executor << task
|
2016-01-27 23:55:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def attach(io, stream)
|
|
|
|
@todo << lambda do
|
2016-09-27 17:14:23 -04:00
|
|
|
@map[io] = @nio.register(io, :r)
|
|
|
|
@map[io].value = stream
|
2016-01-27 23:55:31 -05:00
|
|
|
end
|
2016-01-28 03:16:14 -05:00
|
|
|
wakeup
|
2016-01-27 23:55:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def detach(io, stream)
|
|
|
|
@todo << lambda do
|
2016-01-28 03:16:14 -05:00
|
|
|
@nio.deregister io
|
2016-01-27 23:55:31 -05:00
|
|
|
@map.delete io
|
2016-10-05 20:01:14 -04:00
|
|
|
io.close
|
2016-01-27 23:55:31 -05:00
|
|
|
end
|
2016-01-28 03:16:14 -05:00
|
|
|
wakeup
|
2016-01-27 23:55:31 -05:00
|
|
|
end
|
|
|
|
|
2016-09-27 17:14:23 -04:00
|
|
|
def writes_pending(io)
|
|
|
|
@todo << lambda do
|
|
|
|
if monitor = @map[io]
|
|
|
|
monitor.interests = :rw
|
|
|
|
end
|
|
|
|
end
|
|
|
|
wakeup
|
|
|
|
end
|
|
|
|
|
2016-01-27 23:55:31 -05:00
|
|
|
def stop
|
|
|
|
@stopping = true
|
2016-01-28 03:16:14 -05:00
|
|
|
wakeup if @nio
|
2016-01-27 23:55:31 -05:00
|
|
|
end
|
|
|
|
|
2016-01-28 03:16:14 -05:00
|
|
|
private
|
|
|
|
def spawn
|
|
|
|
return if @thread && @thread.status
|
|
|
|
|
|
|
|
@spawn_mutex.synchronize do
|
|
|
|
return if @thread && @thread.status
|
|
|
|
|
|
|
|
@nio ||= NIO::Selector.new
|
2016-09-27 17:14:23 -04:00
|
|
|
|
|
|
|
@executor ||= Concurrent::ThreadPoolExecutor.new(
|
|
|
|
min_threads: 1,
|
|
|
|
max_threads: 10,
|
|
|
|
max_queue: 0,
|
|
|
|
)
|
|
|
|
|
2016-01-28 03:16:14 -05:00
|
|
|
@thread = Thread.new { run }
|
2016-01-27 23:55:31 -05:00
|
|
|
|
2016-01-28 03:16:14 -05:00
|
|
|
return true
|
2016-01-27 23:55:31 -05:00
|
|
|
end
|
2016-01-28 03:16:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def wakeup
|
|
|
|
spawn || @nio.wakeup
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
loop do
|
|
|
|
if @stopping
|
|
|
|
@nio.close
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
until @todo.empty?
|
|
|
|
@todo.pop(true).call
|
|
|
|
end
|
|
|
|
|
|
|
|
next unless monitors = @nio.select
|
2016-01-27 23:55:31 -05:00
|
|
|
|
|
|
|
monitors.each do |monitor|
|
|
|
|
io = monitor.io
|
2016-09-27 17:14:23 -04:00
|
|
|
stream = monitor.value
|
2016-01-27 23:55:31 -05:00
|
|
|
|
|
|
|
begin
|
2016-09-27 17:14:23 -04:00
|
|
|
if monitor.writable?
|
|
|
|
if stream.flush_write_buffer
|
|
|
|
monitor.interests = :r
|
|
|
|
end
|
|
|
|
next unless monitor.readable?
|
|
|
|
end
|
|
|
|
|
|
|
|
incoming = io.read_nonblock(4096, exception: false)
|
|
|
|
case incoming
|
|
|
|
when :wait_readable
|
|
|
|
next
|
|
|
|
when nil
|
|
|
|
stream.close
|
|
|
|
else
|
|
|
|
stream.receive incoming
|
|
|
|
end
|
2016-01-28 03:16:14 -05:00
|
|
|
rescue
|
|
|
|
# We expect one of EOFError or Errno::ECONNRESET in
|
|
|
|
# normal operation (when the client goes away). But if
|
|
|
|
# anything else goes wrong, this is still the best way
|
|
|
|
# to handle it.
|
|
|
|
begin
|
|
|
|
stream.close
|
|
|
|
rescue
|
|
|
|
@nio.deregister io
|
|
|
|
@map.delete io
|
|
|
|
end
|
2016-01-27 23:55:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|