Class: HTTP::Timeout::Global

Inherits:
Null
  • Object
show all
Defined in:
lib/http/timeout/global.rb

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

Null::NATIVE_CONNECT_TIMEOUT

Instance Attribute Summary

Attributes inherited from Null

#options, #socket

Instance Method Summary collapse

Methods inherited from Null

#close, #closed?, #start_tls

Constructor Details

#initialize(global_timeout:, read_timeout: nil, write_timeout: nil, connect_timeout: nil) ⇒ HTTP::Timeout::Global

Initializes global timeout with options

Examples:

HTTP::Timeout::Global.new(global_timeout: 5)

Parameters:

  • global_timeout (Numeric)

    Global timeout in seconds

  • read_timeout (Numeric, nil) (defaults to: nil)

    Read timeout in seconds

  • write_timeout (Numeric, nil) (defaults to: nil)

    Write timeout in seconds

  • connect_timeout (Numeric, nil) (defaults to: nil)

    Connect timeout in seconds



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

Examples:

timeout.connect(TCPSocket, "example.com", 80)

Parameters:

  • socket_class (Class)
  • host (String)
  • port (Integer)
  • nodelay (Boolean) (defaults to: false)


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_sslvoid

This method returns an undefined value.

Starts an SSL connection on a socket

Examples:

timeout.connect_ssl


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

Examples:

timeout.readpartial(1024)

Parameters:

  • size (Integer)
  • buffer (String, nil) (defaults to: nil)

Returns:

  • (String, :eof)


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_counterNumeric

Resets the time left counter to initial timeout

Examples:

timeout.reset_counter

Returns:

  • (Numeric)


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

Examples:

timeout.write("GET / HTTP/1.1")

Parameters:

  • data (String)

Returns:

  • (Integer, :eof)


106
107
108
# File 'lib/http/timeout/global.rb', line 106

def write(data)
  perform_io(@write_timeout) { write_nonblock(data) }
end