Skip to content

Commit

Permalink
Refactor Reactor and Client request buffering
Browse files Browse the repository at this point in the history
Refactor Reactor into a more generic IO-with-timeout monitor,
using a Queue to simplify the implementation.
Move request-buffering logic into Server#reactor_wakeup.
Fixes bug in managing timeouts on clients.
Move, update and rewrite documentation to match updated class structure.
  • Loading branch information
wjordan committed Oct 1, 2020
1 parent bb61c7b commit b830a1c
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 461 deletions.
1 change: 1 addition & 0 deletions History.md
Expand Up @@ -8,6 +8,7 @@

* Refactor
* Consolidate option handling in Server, Server small refactors, doc chang (#2389)
* Refactor Reactor and Client request buffering (#2279)

## 5.0.2 / 2020-09-28

Expand Down
17 changes: 12 additions & 5 deletions lib/puma/client.rb
Expand Up @@ -103,7 +103,12 @@ def in_data_phase
end

def set_timeout(val)
@timeout_at = Time.now + val
@timeout_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + val
end

# Number of seconds until the timeout elapses.
def timeout
[@timeout_at - Process.clock_gettime(Process::CLOCK_MONOTONIC), 0].max
end

def reset(fast_check=true)
Expand Down Expand Up @@ -255,14 +260,16 @@ def finish(timeout)
rescue ThreadPool::ForceShutdown
nil
end
unless can_read
write_error(408) if in_data_phase
raise ConnectionError
end
timeout! unless can_read
end
true
end

def timeout!
write_error(408) if in_data_phase
raise ConnectionError
end

def write_error(status_code)
begin
@io << ERROR_RESPONSE[status_code]
Expand Down
24 changes: 24 additions & 0 deletions lib/puma/queue_close.rb
@@ -0,0 +1,24 @@
# Queue#close was added in Ruby 2.3.
# Add a simple implementation for earlier Ruby versions.
unless Queue.instance_methods.include?(:close)
class ClosedQueueError < StandardError; end
module Puma
module QueueClose
def initialize
@closed = false
super
end
def close
@closed = true
end
def closed?
@closed
end
def push(object)
raise ClosedQueueError if @closed
super
end
end
Queue.prepend QueueClose
end
end

0 comments on commit b830a1c

Please sign in to comment.