2012-07-23 13:26:52 -04:00
|
|
|
module Puma
|
|
|
|
class Reactor
|
|
|
|
DefaultSleepFor = 5
|
|
|
|
|
2012-07-23 20:00:53 -04:00
|
|
|
def initialize(server, app_pool)
|
|
|
|
@server = server
|
|
|
|
@events = server.events
|
2012-07-23 13:26:52 -04:00
|
|
|
@app_pool = app_pool
|
|
|
|
|
|
|
|
@mutex = Mutex.new
|
|
|
|
@ready, @trigger = IO.pipe
|
|
|
|
@input = []
|
|
|
|
@sleep_for = DefaultSleepFor
|
|
|
|
@timeouts = []
|
2012-08-27 13:56:43 -04:00
|
|
|
|
|
|
|
@sockets = [@ready]
|
2012-07-23 13:26:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2012-08-27 13:56:43 -04:00
|
|
|
sockets = @sockets
|
2012-07-23 13:26:52 -04:00
|
|
|
|
|
|
|
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
|
2012-08-10 13:10:30 -04:00
|
|
|
case @ready.read(1)
|
|
|
|
when "*"
|
|
|
|
sockets += @input
|
|
|
|
@input.clear
|
2012-09-02 23:33:09 -04:00
|
|
|
when "c"
|
|
|
|
sockets.delete_if do |s|
|
|
|
|
if s == @ready
|
|
|
|
false
|
|
|
|
else
|
|
|
|
s.close
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2012-08-10 13:10:30 -04:00
|
|
|
when "!"
|
|
|
|
return
|
|
|
|
end
|
2012-07-23 13:26:52 -04:00
|
|
|
end
|
|
|
|
else
|
2012-07-30 19:12:23 -04:00
|
|
|
# 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
|
|
|
|
@timeouts.delete c
|
|
|
|
end
|
|
|
|
|
2012-07-23 13:26:52 -04:00
|
|
|
begin
|
|
|
|
if c.try_to_finish
|
|
|
|
@app_pool << c
|
|
|
|
sockets.delete c
|
|
|
|
end
|
2012-08-11 01:41:35 -04:00
|
|
|
|
2012-07-23 13:26:52 -04:00
|
|
|
# The client doesn't know HTTP well
|
|
|
|
rescue HttpParserError => e
|
2012-09-06 01:09:42 -04:00
|
|
|
c.write_400
|
2012-07-23 20:08:11 -04:00
|
|
|
c.close
|
2012-09-06 01:09:42 -04:00
|
|
|
|
2012-07-23 20:08:11 -04:00
|
|
|
sockets.delete c
|
|
|
|
|
2012-07-23 20:00:53 -04:00
|
|
|
@events.parse_error @server, c.env, e
|
2012-08-27 13:56:43 -04:00
|
|
|
rescue StandardError => e
|
2012-09-06 01:09:42 -04:00
|
|
|
c.write_500
|
2012-07-23 13:26:52 -04:00
|
|
|
c.close
|
2012-09-06 01:09:42 -04:00
|
|
|
|
2012-07-23 13:26:52 -04:00
|
|
|
sockets.delete c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless @timeouts.empty?
|
|
|
|
now = Time.now
|
|
|
|
|
|
|
|
while @timeouts.first.timeout_at < now
|
|
|
|
c = @timeouts.shift
|
|
|
|
sockets.delete c
|
|
|
|
c.close
|
|
|
|
|
2012-07-30 19:12:23 -04:00
|
|
|
break if @timeouts.empty?
|
2012-07-23 13:26:52 -04:00
|
|
|
end
|
2012-07-30 19:12:23 -04:00
|
|
|
|
|
|
|
calculate_sleep
|
2012-07-23 13:26:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_in_thread
|
2012-08-09 19:54:55 -04:00
|
|
|
@thread = Thread.new {
|
2012-08-27 13:56:43 -04:00
|
|
|
while true
|
|
|
|
begin
|
|
|
|
run
|
|
|
|
break
|
|
|
|
rescue StandardError => e
|
|
|
|
STDERR.puts "Error in reactor loop escaped: #{e.message} (#{e.class})"
|
|
|
|
puts e.backtrace
|
|
|
|
end
|
2012-08-09 19:54:55 -04:00
|
|
|
end
|
|
|
|
}
|
2012-07-23 13:26:52 -04:00
|
|
|
end
|
|
|
|
|
2012-07-30 19:12:23 -04:00
|
|
|
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
|
|
|
|
|
2012-07-23 13:26:52 -04:00
|
|
|
def add(c)
|
|
|
|
@mutex.synchronize do
|
|
|
|
@input << c
|
2012-08-10 13:10:30 -04:00
|
|
|
@trigger << "*"
|
2012-07-23 13:26:52 -04:00
|
|
|
|
|
|
|
if c.timeout_at
|
|
|
|
@timeouts << c
|
|
|
|
@timeouts.sort! { |a,b| a.timeout_at <=> b.timeout_at }
|
2012-07-24 20:25:03 -04:00
|
|
|
|
2012-07-30 19:12:23 -04:00
|
|
|
calculate_sleep
|
2012-07-23 13:26:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-08-10 13:10:30 -04:00
|
|
|
|
2012-09-02 23:33:09 -04:00
|
|
|
# Close all watched sockets and clear them from being watched
|
|
|
|
def clear!
|
|
|
|
@trigger << "c"
|
|
|
|
end
|
|
|
|
|
2012-08-10 13:10:30 -04:00
|
|
|
def shutdown
|
|
|
|
@trigger << "!"
|
2012-09-02 23:33:09 -04:00
|
|
|
@thread.join
|
2012-08-10 13:10:30 -04:00
|
|
|
end
|
2012-07-23 13:26:52 -04:00
|
|
|
end
|
|
|
|
end
|