Class: Puma::Reactor
- Inherits:
-
Object
- Object
- Puma::Reactor
- Defined in:
- lib/puma/reactor.rb
Constant Summary collapse
- DefaultSleepFor =
5
Instance Method Summary collapse
- #add(c) ⇒ Object
- #calculate_sleep ⇒ Object
-
#clear! ⇒ Object
Close all watched sockets and clear them from being watched.
-
#initialize(server, app_pool) ⇒ Reactor
constructor
A new instance of Reactor.
- #run ⇒ Object
- #run_in_thread ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(server, app_pool) ⇒ Reactor
Returns a new instance of Reactor.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/puma/reactor.rb', line 8 def initialize(server, app_pool) @server = server @events = server.events @app_pool = app_pool @mutex = Mutex.new @ready, @trigger = Puma::Util.pipe @input = [] @sleep_for = DefaultSleepFor @timeouts = [] @sockets = [@ready] end |
Instance Method Details
#add(c) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/puma/reactor.rb', line 179 def add(c) @mutex.synchronize do @input << c @trigger << "*" if c.timeout_at @timeouts << c @timeouts.sort! { |a,b| a.timeout_at <=> b.timeout_at } calculate_sleep end end end |
#calculate_sleep ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/puma/reactor.rb', line 165 def calculate_sleep if @timeouts.empty? @sleep_for = DefaultSleepFor else diff = @timeouts.first.timeout_at.to_f - Time.now.to_f if diff < 0.0 @sleep_for = 0 else @sleep_for = diff end end end |
#clear! ⇒ Object
Close all watched sockets and clear them from being watched
194 195 196 197 198 199 |
# File 'lib/puma/reactor.rb', line 194 def clear! begin @trigger << "c" rescue IOError end end |
#run ⇒ Object
143 144 145 146 147 148 |
# File 'lib/puma/reactor.rb', line 143 def run run_internal ensure @trigger.close @ready.close end |
#run_in_thread ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/puma/reactor.rb', line 150 def run_in_thread @thread = Thread.new do begin run_internal rescue StandardError => e STDERR.puts "Error in reactor loop escaped: #{e.} (#{e.class})" STDERR.puts e.backtrace retry ensure @trigger.close @ready.close end end end |
#shutdown ⇒ Object
201 202 203 204 205 206 207 208 |
# File 'lib/puma/reactor.rb', line 201 def shutdown begin @trigger << "!" rescue IOError end @thread.join end |