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.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/puma/reactor.rb', line 7 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
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/puma/reactor.rb', line 131 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
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/puma/reactor.rb', line 117 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
146 147 148 149 150 151 |
# File 'lib/puma/reactor.rb', line 146 def clear! begin @trigger << "c" rescue IOError end end |
#run ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/puma/reactor.rb', line 21 def run sockets = @sockets while true ready = IO.select sockets, nil, nil, @sleep_for if ready and reads = ready[0] reads.each do |c| if c == @ready @mutex.synchronize do case @ready.read(1) when "*" sockets += @input @input.clear when "c" sockets.delete_if do |s| if s == @ready false else s.close true end end when "!" return end end else # We have to be sure to remove it from the timeout # list or we'll accidentally close the socket when # it's in use! if c.timeout_at @mutex.synchronize do @timeouts.delete c end end begin if c.try_to_finish @app_pool << c sockets.delete c end # The client doesn't know HTTP well rescue HttpParserError => e c.write_400 c.close sockets.delete c @events.parse_error @server, c.env, e rescue StandardError => e c.write_500 c.close sockets.delete c end end end end unless @timeouts.empty? @mutex.synchronize do now = Time.now while @timeouts.first.timeout_at < now c = @timeouts.shift sockets.delete c c.close break if @timeouts.empty? end calculate_sleep end end end ensure @trigger.close @ready.close end |
#run_in_thread ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/puma/reactor.rb', line 103 def run_in_thread @thread = Thread.new { while true begin run break rescue StandardError => e STDERR.puts "Error in reactor loop escaped: #{e.} (#{e.class})" puts e.backtrace end end } end |
#shutdown ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/puma/reactor.rb', line 153 def shutdown begin @trigger << "!" rescue IOError end @thread.join end |