Class: HTTP::Timeout::Global
Overview
Timeout handler with a single global timeout for the entire request
Constant Summary collapse
- WAIT_RESULTS =
I/O wait result symbols returned by non-blocking operations
%i[wait_readable wait_writable].freeze
Constants inherited from Null
Instance Attribute Summary
Attributes inherited from Null
Instance Method Summary collapse
-
#connect(socket_class, host, port, nodelay: false) ⇒ void
Connects to a socket with global timeout.
-
#connect_ssl ⇒ void
Starts an SSL connection on a socket.
-
#initialize(global_timeout:, read_timeout: nil, write_timeout: nil, connect_timeout: nil) ⇒ HTTP::Timeout::Global
constructor
Initializes global timeout with options.
-
#readpartial(size, buffer = nil) ⇒ String, :eof
Read from the socket.
-
#reset_counter ⇒ Numeric
Resets the time left counter to initial timeout.
-
#write(data) ⇒ Integer, :eof
(also: #<<)
Write to the socket.
Methods inherited from Null
Constructor Details
#initialize(global_timeout:, read_timeout: nil, write_timeout: nil, connect_timeout: nil) ⇒ HTTP::Timeout::Global
Initializes global timeout with options
25 26 27 28 29 30 31 32 |
# File 'lib/http/timeout/global.rb', line 25 def initialize(global_timeout:, read_timeout: nil, write_timeout: nil, connect_timeout: nil) super @timeout = @time_left = global_timeout @read_timeout = read_timeout @write_timeout = write_timeout @connect_timeout = connect_timeout end |
Instance Method Details
#connect(socket_class, host, port, nodelay: false) ⇒ void
This method returns an undefined value.
Connects to a socket with global timeout
56 57 58 59 60 61 62 |
# File 'lib/http/timeout/global.rb', line 56 def connect(socket_class, host, port, nodelay: false) reset_timer @socket = open_socket(socket_class, host, port, connect_timeout: effective_timeout(@connect_timeout)) @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay log_time end |
#connect_ssl ⇒ void
This method returns an undefined value.
Starts an SSL connection on a socket
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/http/timeout/global.rb', line 71 def connect_ssl reset_timer begin @socket.connect_nonblock rescue IO::WaitReadable wait_readable_or_timeout(@connect_timeout) retry rescue IO::WaitWritable wait_writable_or_timeout(@connect_timeout) retry end end |
#readpartial(size, buffer = nil) ⇒ String, :eof
Read from the socket
94 95 96 |
# File 'lib/http/timeout/global.rb', line 94 def readpartial(size, buffer = nil) perform_io(@read_timeout) { read_nonblock(size, buffer) } end |
#reset_counter ⇒ Numeric
Resets the time left counter to initial timeout
41 42 43 |
# File 'lib/http/timeout/global.rb', line 41 def reset_counter @time_left = @timeout end |
#write(data) ⇒ Integer, :eof Also known as: <<
Write to the socket
106 107 108 |
# File 'lib/http/timeout/global.rb', line 106 def write(data) perform_io(@write_timeout) { write_nonblock(data) } end |