class HTTP::Timeout::PerOperation
def connect(socket_class, host, port, nodelay = false)
def connect(socket_class, host, port, nodelay = false) ::Timeout.timeout(connect_timeout, TimeoutError) do @socket = socket_class.open(host, port) @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay end end
def connect_ssl
def connect_ssl rescue_readable(@connect_timeout) do rescue_writable(@connect_timeout) do @socket.connect_nonblock end end end
def initialize(*args)
def initialize(*args) super @read_timeout = options.fetch(:read_timeout, READ_TIMEOUT) @write_timeout = options.fetch(:write_timeout, WRITE_TIMEOUT) @connect_timeout = options.fetch(:connect_timeout, CONNECT_TIMEOUT) end
def readpartial(size)
def readpartial(size) rescue_readable do @socket.read_nonblock(size) end rescue EOFError :eof end
def readpartial(size)
def readpartial(size) loop do result = @socket.read_nonblock(size, :exception => false) return :eof if result.nil? return result if result != :wait_readable unless IO.select([@socket], nil, nil, read_timeout) raise TimeoutError, "Read timed out after #{read_timeout} seconds" end end end
def write(data)
def write(data) rescue_writable do @socket.write_nonblock(data) end rescue EOFError :eof end
def write(data)
def write(data) loop do result = @socket.write_nonblock(data, :exception => false) return result unless result == :wait_writable unless IO.select(nil, [@socket], nil, write_timeout) raise TimeoutError, "Write timed out after #{write_timeout} seconds" end end end