Class: Async::Reactor
- Inherits:
-
Object
- Object
- Async::Reactor
- Extended by:
- Forwardable
- Defined in:
- lib/async/reactor.rb
Instance Attribute Summary collapse
-
#stopped ⇒ Object
readonly
Returns the value of attribute stopped.
-
#wrappers ⇒ Object
readonly
Returns the value of attribute wrappers.
Class Method Summary collapse
Instance Method Summary collapse
- #async(*ios, &block) ⇒ Object
-
#initialize(wrappers: IO) ⇒ Reactor
constructor
A new instance of Reactor.
- #register(*args) ⇒ Object
- #run(*args, &block) ⇒ Object
- #sleep(duration) ⇒ Object
- #stop ⇒ Object
- #timeout(duration) ⇒ Object
- #with(io, &block) ⇒ Object
- #wrap(io, task) ⇒ Object
Constructor Details
#initialize(wrappers: IO) ⇒ Reactor
Returns a new instance of Reactor.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/async/reactor.rb', line 42 def initialize(wrappers: IO) @wrappers = wrappers @selector = NIO::Selector.new @timers = Timers::Group.new @fibers = [] @stopped = true end |
Instance Attribute Details
#stopped ⇒ Object (readonly)
Returns the value of attribute stopped.
54 55 56 |
# File 'lib/async/reactor.rb', line 54 def stopped @stopped end |
#wrappers ⇒ Object (readonly)
Returns the value of attribute wrappers.
53 54 55 |
# File 'lib/async/reactor.rb', line 53 def wrappers @wrappers end |
Class Method Details
.run(*args, &block) ⇒ Object
36 37 38 39 40 |
# File 'lib/async/reactor.rb', line 36 def self.run(*args, &block) reactor = self.new reactor.async(*args, &block) end |
Instance Method Details
#async(*ios, &block) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/async/reactor.rb', line 68 def async(*ios, &block) task = Task.new(ios, self, &block) # I want to take a moment to explain the logic of this. # When calling an async block, we deterministically execute it until the # first blocking operation. We don't *have* to do this - we could schedule # it for later execution, but it's useful to: # - Fail at the point of call where possible. # - Execute determinstically where possible. # - Avoid overhead if no blocking operation is performed. fiber = task.run # We only start tracking this if the fiber is still alive: @fibers << fiber if fiber.alive? # Async.logger.debug "Initial execution of task #{fiber} complete (#{result} -> #{fiber.alive?})..." return task end |
#register(*args) ⇒ Object
87 88 89 |
# File 'lib/async/reactor.rb', line 87 def register(*args) @selector.register(*args) end |
#run(*args, &block) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/async/reactor.rb', line 95 def run(*args, &block) @stopped = false # Allow the user to kick of the initial async tasks. async(*args, &block) if block_given? @timers.wait do |interval| # - nil: no timers # - -ve: timers expired already # - 0: timers ready to fire # - +ve: timers waiting to fire interval = 0 if interval && interval < 0 # Async.logger.debug "[#{self} Pre] Updating #{@fibers.count} fibers..." # Async.logger.debug @fibers.collect{|fiber| [fiber, fiber.alive?]}.inspect # As timeouts may have been updated, and caused fibers to complete, we should check this. @fibers.delete_if{|fiber| !fiber.alive?} # If there is nothing to do, then finish: # Async.logger.debug "[#{self}] @fibers.empty? = #{@fibers.empty?} && interval #{interval.inspect}" return if @fibers.empty? && interval.nil? # Async.logger.debug "Selecting with #{@fibers.count} fibers interval = #{interval}..." if monitors = @selector.select(interval) monitors.each do |monitor| if task = monitor.value # Async.logger.debug "Resuming task #{task} due to IO..." task.resume end end end end until @stopped ensure # Async.logger.debug "[#{self} Ensure] Exiting run-loop (stopped: #{@stopped} exception: #{$!})..." # Async.logger.debug @fibers.collect{|fiber| [fiber, fiber.alive?]}.inspect @stopped = true end |
#sleep(duration) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/async/reactor.rb', line 133 def sleep(duration) task = Fiber.current timer = self.after(duration) do if task.alive? task.resume end end result = Fiber.yield raise result if result.is_a? Exception ensure timer.cancel if timer end |
#stop ⇒ Object
91 92 93 |
# File 'lib/async/reactor.rb', line 91 def stop @stopped = true end |
#timeout(duration) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/async/reactor.rb', line 149 def timeout(duration) backtrace = caller task = Fiber.current timer = self.after(duration) do if task.alive? error = TimeoutError.new("execution expired") error.set_backtrace backtrace task.resume error end end yield ensure timer.cancel if timer end |
#with(io, &block) ⇒ Object
62 63 64 65 66 |
# File 'lib/async/reactor.rb', line 62 def with(io, &block) async do |task| task.with(io, &block) end end |
#wrap(io, task) ⇒ Object
58 59 60 |
# File 'lib/async/reactor.rb', line 58 def wrap(io, task) @wrappers[io].new(io, task) end |