Class: Puma::Reactor
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/puma/reactor.rb |
Overview
Monitors a collection of IO objects, calling a block whenever
any monitored object either receives data or times out, or when the Reactor shuts down.
The waiting/wake up is performed with nio4r, which will use the appropriate backend (libev,
Java NIO or just plain IO#select). The call to NIO::Selector#select will
'wakeup' any IO object that receives data.
This class additionally tracks a timeout for every added object, and wakes up any object when its timeout elapses.
The implementation uses a Queue to synchronize adding new objects from the internal select loop.
Class Method Summary
-
.new(backend, &block) ⇒ Reactor
constructor
Create a new
Reactorto monitor IO objects added by #add.
Instance Attribute Summary
-
#reactor_max
rw
Maximum number of clients in the selector.
- #reactor_size readonly
Instance Method Summary
-
#add(client)
Add a new client to monitor.
-
#run(background = true)
Run the internal select loop, using a background thread by default.
-
#shutdown
Shutdown the reactor, blocking until the background thread is finished.
- #interests(client) private
-
#register(client)
private
Start monitoring the object.
- #requeue(client) private
-
#requeue_moved_timeouts
private
@timeoutsis sorted by the timeout each client had when it was queued. - #select_loop private
-
#wakeup!(client)
private
'Wake up' a monitored object by calling the provided block.
Constructor Details
.new(backend, &block) ⇒ Reactor
Create a new Reactor to monitor IO objects added by #add.
The provided block will be invoked when an IO has data available to read,
its timeout elapses, or when the Reactor shuts down.
# File 'lib/puma/reactor.rb', line 27
def initialize(backend, &block) require 'nio' valid_backends = [:auto, *::NIO::Selector.backends] unless valid_backends.include?(backend) raise ArgumentError.new("unsupported IO selector backend: #{backend} (available backends: #{valid_backends.join(', ')})") end @selector = ::NIO::Selector.new(NIO::Selector.backends.delete(backend)) @input = Queue.new @timeouts = [] @timeout_keys = {}.compare_by_identity @block = block @reactor_size = 0 @reactor_max = 0 end
Instance Attribute Details
#reactor_max (rw)
Maximum number of clients in the selector. Reset with calls to Server#stats.
# File 'lib/puma/reactor.rb', line 21
attr_accessor :reactor_max
#reactor_size (readonly)
[ GitHub ]# File 'lib/puma/reactor.rb', line 22
attr_reader :reactor_size
Instance Method Details
#add(client)
Add a new client to monitor.
The object must respond to #timeout and #timeout_at.
Returns false if the reactor is already shut down.
# File 'lib/puma/reactor.rb', line 58
def add(client) @input << client @selector.wakeup true rescue ClosedQueueError, IOError # Ignore if selector is already closed false end
#interests(client) (private)
[ GitHub ]# File 'lib/puma/reactor.rb', line 125
def interests(client) client.write_pending? ? :w : :r end
#register(client) (private)
Start monitoring the object.
# File 'lib/puma/reactor.rb', line 115
def register(client) @selector.register(client.to_io, interests(client)).value = client @reactor_size += 1 @reactor_max = @reactor_size if @reactor_max < @reactor_size @timeouts << client @timeout_keys[client] = client.timeout_at rescue ArgumentError # unreadable clients raise error when processed by NIO end
#requeue(client) (private)
[ GitHub ]# File 'lib/puma/reactor.rb', line 157
def requeue(client) key = @timeout_keys[client] = client.timeout_at index = @timeouts.bsearch_index { |c| @timeout_keys[c] > key } || @timeouts.size @timeouts.insert index, client end
#requeue_moved_timeouts (private)
@timeouts is sorted by the timeout each client had when it was queued.
Timeouts that moved earlier are requeued in #wakeup!. Timeouts that moved
later are requeued here, once the client reaches the front.
# File 'lib/puma/reactor.rb', line 150
def requeue_moved_timeouts while (client = @timeouts.first) && @timeout_keys[client] < client.timeout_at @timeouts.shift requeue client end end
#run(background = true)
Run the internal select loop, using a background thread by default.
# File 'lib/puma/reactor.rb', line 44
def run(background=true) if background @thread = Thread.new do Puma.set_thread_name "reactor" select_loop end else select_loop end end
#select_loop (private)
[ GitHub ]# File 'lib/puma/reactor.rb', line 78
def select_loop begin until @input.closed? && @input.empty? requeue_moved_timeouts # Wakeup any registered object that receives incoming data. # Block until the earliest timeout or Selector#wakeup is called. timeout = (earliest = @timeouts.first) && earliest.timeout @selector.select(timeout) do |monitor| monitor.interests = interests(monitor.value) unless wakeup!(monitor.value) end # Wakeup all objects that timed out. timed_out = @timeouts.take_while { |client| client.timeout == 0 } timed_out.each { |client| wakeup!(client) } unless @input.empty? until @input.empty? client = @input.pop register(client) if client.io_ok? end @timeouts.sort_by! { |client| @timeout_keys[client] } end end rescue StandardError => e STDERR.puts "Error in reactor loop escaped: #{e.} (#{e.class})" STDERR.puts e.backtrace retry end # Wakeup all remaining objects on shutdown. @timeouts.each(&@block) @selector.close end
#shutdown
Shutdown the reactor, blocking until the background thread is finished.
# File 'lib/puma/reactor.rb', line 67
def shutdown @input.close begin @selector.wakeup rescue IOError # Ignore if selector is already closed end @thread&.join end
#wakeup!(client) (private)
'Wake up' a monitored object by calling the provided block.
Stop monitoring the object if the block returns true.
# File 'lib/puma/reactor.rb', line 131
def wakeup!(client) if @block.call client @selector.deregister client.to_io @reactor_size -= 1 @timeouts.delete client @timeout_keys.delete client true else if client.timeout_at < @timeout_keys[client] @timeouts.delete client requeue client end false end end