Skip to content

Commit d8b2bdf

Browse files
committed
add additional ruby >= 2.1 nonblocking error classes
closes excon#671
1 parent c38bb35 commit d8b2bdf

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

lib/excon/socket.rb

+17-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ class Socket
77

88
attr_accessor :data
99

10+
# drawn from https://github.com/ruby-amqp/bunny/commit/75d9dd79551b31a5dd3d1254c537bad471f108cf
11+
READ_RETRY_EXCEPTION_CLASSES = if defined?(IO::EAGAINWAITReadable) # Ruby >= 2.1
12+
[Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitReadable, IO::EAGAINWaitReadable, IO::EWOULDBLOCKWaitReadable]
13+
else # Ruby <= 2.0
14+
[Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitReadable]
15+
end
16+
WRITE_RETRY_EXCEPTION_CLASSES = if defined?(IO::EAGAINWaitWritable) # Ruby >= 2.1
17+
[Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitWritable, IO::EAGAINWaitWritable, IO::EWOULDBLOCKWaitWritable]
18+
else # Ruby <= 2.0
19+
[Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitWritable]
20+
end
21+
1022
def params
1123
Excon.display_warning('Excon::Socket#params is deprecated use Excon::Socket#data instead.')
1224
@data
@@ -46,7 +58,7 @@ def readline
4658
begin
4759
buffer << @socket.read_nonblock(1) while buffer[-1] != "\n"
4860
buffer
49-
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitReadable
61+
rescue *READ_RETRY_EXCEPTION_CLASSES
5062
select_with_timeout(@socket, :read) && retry
5163
rescue OpenSSL::SSL::SSLError => error
5264
if error.message == 'read would block'
@@ -170,7 +182,7 @@ def read_nonblock(max_length)
170182
else
171183
raise(error)
172184
end
173-
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitReadable
185+
rescue *READ_RETRY_EXCEPTION_CLASSES
174186
if @read_buffer.empty?
175187
# if we didn't read anything, try again...
176188
select_with_timeout(@socket, :read) && retry
@@ -199,7 +211,7 @@ def read_block(max_length)
199211
else
200212
raise(error)
201213
end
202-
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitReadable
214+
rescue *READ_RETRY_EXCEPTION_CLASSES
203215
if @read_buffer.empty?
204216
select_with_timeout(@socket, :read) && retry
205217
end
@@ -225,7 +237,7 @@ def write_nonblock(data)
225237
else
226238
raise error
227239
end
228-
rescue OpenSSL::SSL::SSLError, Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitWritable => error
240+
rescue OpenSSL::SSL::SSLError, *WRITE_RETRY_EXCEPTION_CLASSES => error
229241
if error.is_a?(OpenSSL::SSL::SSLError) && error.message != 'write would block'
230242
raise error
231243
else
@@ -246,7 +258,7 @@ def write_nonblock(data)
246258

247259
def write_block(data)
248260
@socket.write(data)
249-
rescue OpenSSL::SSL::SSLError, Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitWritable => error
261+
rescue OpenSSL::SSL::SSLError, *WRITE_RETRY_EXCEPTION_CLASSES => error
250262
if error.is_a?(OpenSSL::SSL::SSLError) && error.message != 'write would block'
251263
raise error
252264
else

0 commit comments

Comments
 (0)