Skip to content

Commit

Permalink
Revert "Use io/wait on platforms where it's available" (fixes #298)
Browse files Browse the repository at this point in the history
This reverts commit f4df840.

Conflicts:
	lib/http/timeout/null.rb
	lib/http/timeout/per_operation.rb
  • Loading branch information
tarcieri committed Mar 19, 2016
1 parent f7e028a commit 3db1e49
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 72 deletions.
3 changes: 0 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
AllCops:
DisplayCopNames: true

Metrics/BlockNesting:
Max: 2

Expand Down
42 changes: 13 additions & 29 deletions lib/http/timeout/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ def connect_ssl
reset_timer

begin
@socket.connect_nonblock
socket.connect_nonblock
rescue IO::WaitReadable
IO.select([@socket], nil, nil, time_left)
IO.select([socket], nil, nil, time_left)
log_time
retry
rescue IO::WaitWritable
IO.select(nil, [@socket], nil, time_left)
IO.select(nil, [socket], nil, time_left)
log_time
retry
end
Expand All @@ -54,7 +54,7 @@ def write(data)
perform_io { write_nonblock(data) }
end

alias << write
alias_method :<<, :write

private

Expand Down Expand Up @@ -104,32 +104,16 @@ def perform_io
:eof
end

if RUBY_VERSION < "2.0.0"
# Wait for a socket to become readable
def wait_readable_or_timeout
IO.select([@socket], nil, nil, time_left)
log_time
end

# Wait for a socket to become writable
def wait_writable_or_timeout
IO.select(nil, [@socket], nil, time_left)
log_time
end
else
require "io/wait"

# Wait for a socket to become readable
def wait_readable_or_timeout
@socket.to_io.wait_readable(time_left)
log_time
end
# Wait for a socket to become readable
def wait_readable_or_timeout
IO.select([@socket], nil, nil, time_left)
log_time
end

# Wait for a socket to become writable
def wait_writable_or_timeout
@socket.to_io.wait_writable(time_left)
log_time
end
# Wait for a socket to become writable
def wait_writable_or_timeout
IO.select(nil, [@socket], nil, time_left)
log_time
end

# Due to the run/retry nature of nonblocking I/O, it's easier to keep track of time
Expand Down
49 changes: 16 additions & 33 deletions lib/http/timeout/null.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,45 +48,28 @@ def readpartial(size)
def write(data)
@socket.write(data)
end
alias << write

# These cops can be re-eanbled after we go Ruby 2.0+ only
# rubocop:disable Lint/UselessAccessModifier, Metrics/BlockNesting
alias_method :<<, :write

private

if RUBY_VERSION < "2.0.0"
# Retry reading
def rescue_readable
yield
rescue IO::WaitReadable
retry if IO.select([@socket], nil, nil, read_timeout)
raise TimeoutError, "Read timed out after #{read_timeout} seconds"
end

# Retry writing
def rescue_writable
yield
rescue IO::WaitWritable
retry if IO.select(nil, [@socket], nil, write_timeout)
raise TimeoutError, "Write timed out after #{write_timeout} seconds"
end
else
require "io/wait"

# Retry reading
def rescue_readable
yield
rescue IO::WaitReadable
retry if @socket.to_io.wait_readable(read_timeout)
# Retry reading
def rescue_readable
yield
rescue IO::WaitReadable
if IO.select([socket], nil, nil, read_timeout)
retry
else
raise TimeoutError, "Read timed out after #{read_timeout} seconds"
end
end

# Retry writing
def rescue_writable
yield
rescue IO::WaitWritable
retry if @socket.to_io.wait_writable(write_timeout)
# Retry writing
def rescue_writable
yield
rescue IO::WaitWritable
if IO.select(nil, [socket], nil, write_timeout)
retry
else
raise TimeoutError, "Write timed out after #{write_timeout} seconds"
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/http/timeout/per_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def connect_ssl
# Read data from the socket
def readpartial(size)
rescue_readable do
@socket.read_nonblock(size)
socket.read_nonblock(size)
end
rescue EOFError
:eof
Expand All @@ -48,7 +48,7 @@ def readpartial(size)
# Write data to the socket
def write(data)
rescue_writable do
@socket.write_nonblock(data)
socket.write_nonblock(data)
end
rescue EOFError
:eof
Expand All @@ -59,14 +59,14 @@ def write(data)
# Read data from the socket
def readpartial(size)
loop do
result = @socket.read_nonblock(size, :exception => false)
result = socket.read_nonblock(size, :exception => false)
if result.nil?
return :eof
elsif result != :wait_readable
return result
end

unless @socket.to_io.wait_readable(read_timeout)
unless IO.select([socket], nil, nil, read_timeout)
fail TimeoutError, "Read timed out after #{read_timeout} seconds"
end
end
Expand All @@ -75,11 +75,11 @@ def readpartial(size)
# Write data to the socket
def write(data)
loop do
result = @socket.write_nonblock(data, :exception => false)
result = socket.write_nonblock(data, :exception => false)
return result unless result == :wait_writable

unless @socket.to_io.wait_writable(write_timeout)
fail TimeoutError, "Write timed out after #{write_timeout} seconds"
unless IO.select(nil, [socket], nil, write_timeout)
fail TimeoutError, "Read timed out after #{write_timeout} seconds"
end
end
end
Expand Down

0 comments on commit 3db1e49

Please sign in to comment.