Class: HTTP::Timeout::PerOperation
- Defined in:
- lib/http/timeout/per_operation.rb
Constant Summary collapse
- CONNECT_TIMEOUT =
0.25
- WRITE_TIMEOUT =
0.25
- READ_TIMEOUT =
0.25
Instance Attribute Summary
Attributes inherited from Null
Instance Method Summary collapse
- #connect(socket_class, host, port, nodelay = false) ⇒ Object
- #connect_ssl ⇒ Object
-
#initialize(*args) ⇒ PerOperation
constructor
A new instance of PerOperation.
-
#readpartial(size, buffer = nil) ⇒ Object
Read data from the socket.
-
#write(data) ⇒ Object
Write data to the socket.
Methods inherited from Null
Constructor Details
#initialize(*args) ⇒ PerOperation
Returns a new instance of PerOperation.
14 15 16 17 18 19 20 |
# File 'lib/http/timeout/per_operation.rb', line 14 def initialize(*args) super @read_timeout = .fetch(:read_timeout, READ_TIMEOUT) @write_timeout = .fetch(:write_timeout, WRITE_TIMEOUT) @connect_timeout = .fetch(:connect_timeout, CONNECT_TIMEOUT) end |
Instance Method Details
#connect(socket_class, host, port, nodelay = false) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/http/timeout/per_operation.rb', line 22 def connect(socket_class, host, port, nodelay = false) ::Timeout.timeout(@connect_timeout, ConnectTimeoutError) do @socket = socket_class.open(host, port) @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay end end |
#connect_ssl ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/http/timeout/per_operation.rb', line 29 def connect_ssl rescue_readable(@connect_timeout) do rescue_writable(@connect_timeout) do @socket.connect_nonblock end end end |
#readpartial(size, buffer = nil) ⇒ Object
Read data from the socket
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/http/timeout/per_operation.rb', line 38 def readpartial(size, buffer = nil) timeout = false loop do result = @socket.read_nonblock(size, buffer, :exception => false) return :eof if result.nil? return result if result != :wait_readable raise TimeoutError, "Read timed out after #{@read_timeout} seconds" if timeout # marking the socket for timeout. Why is this not being raised immediately? # it seems there is some race-condition on the network level between calling # #read_nonblock and #wait_readable, in which #read_nonblock signalizes waiting # for reads, and when waiting for x seconds, it returns nil suddenly without completing # the x seconds. In a normal case this would be a timeout on wait/read, but it can # also mean that the socket has been closed by the server. Therefore we "mark" the # socket for timeout and try to read more bytes. If it returns :eof, it's all good, no # timeout. Else, the first timeout was a proper timeout. # This hack has to be done because io/wait#wait_readable doesn't provide a value for when # the socket is closed by the server, and HTTP::Parser doesn't provide the limit for the chunks. timeout = true unless @socket.to_io.wait_readable(@read_timeout) end end |
#write(data) ⇒ Object
Write data to the socket
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/http/timeout/per_operation.rb', line 63 def write(data) timeout = false loop do result = @socket.write_nonblock(data, :exception => false) return result unless result == :wait_writable raise TimeoutError, "Write timed out after #{@write_timeout} seconds" if timeout timeout = true unless @socket.to_io.wait_writable(@write_timeout) end end |