Class: HTTP::Timeout::PerOperation

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

Direct Known Subclasses

Global

Constant Summary collapse

HostResolver =
::Resolv::Hosts.new.tap(&:lazy_initialize)
CONNECT_TIMEOUT =
0.25
WRITE_TIMEOUT =
0.25
READ_TIMEOUT =
0.25

Instance Attribute Summary collapse

Attributes inherited from Null

#options, #socket

Instance Method Summary collapse

Methods inherited from Null

#start_tls

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 = options.fetch(:read_timeout, READ_TIMEOUT)
  @write_timeout = options.fetch(:write_timeout, WRITE_TIMEOUT)
  @connect_timeout = options.fetch(:connect_timeout, CONNECT_TIMEOUT)
end

Instance Attribute Details

#connect_timeoutObject (readonly)

Returns the value of attribute connect_timeout.



12
13
14
# File 'lib/http/timeout/per_operation.rb', line 12

def connect_timeout
  @connect_timeout
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



12
13
14
# File 'lib/http/timeout/per_operation.rb', line 12

def read_timeout
  @read_timeout
end

#write_timeoutObject (readonly)

Returns the value of attribute write_timeout.



12
13
14
# File 'lib/http/timeout/per_operation.rb', line 12

def write_timeout
  @write_timeout
end

Instance Method Details

#connect(socket_class, host, port) ⇒ Object



22
23
24
25
26
# File 'lib/http/timeout/per_operation.rb', line 22

def connect(socket_class, host, port)
  ::Timeout.timeout(connect_timeout, TimeoutError) do
    @socket = socket_class.open(host, port)
  end
end

#connect_sslObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/http/timeout/per_operation.rb', line 28

def connect_ssl
  socket.connect_nonblock
rescue IO::WaitReadable
  if IO.select([socket], nil, nil, connect_timeout)
    retry
  else
    raise TimeoutError, "Connection timed out after #{connect_timeout} seconds"
  end
rescue IO::WaitWritable
  if IO.select(nil, [socket], nil, connect_timeout)
    retry
  else
    raise TimeoutError, "Connection timed out after #{connect_timeout} seconds"
  end
end

#readpartial(size) ⇒ Object

Read data from the socket



45
46
47
48
49
50
51
52
53
# File 'lib/http/timeout/per_operation.rb', line 45

def readpartial(size)
  socket.read_nonblock(size)
rescue IO::WaitReadable
  if IO.select([socket], nil, nil, read_timeout)
    retry
  else
    raise TimeoutError, "Read timed out after #{read_timeout} seconds"
  end
end

#write(data) ⇒ Object

Write data to the socket



56
57
58
59
60
61
62
63
64
# File 'lib/http/timeout/per_operation.rb', line 56

def write(data)
  socket.write_nonblock(data)
rescue IO::WaitWritable
  if IO.select(nil, [socket], nil, write_timeout)
    retry
  else
    raise TimeoutError, "Read timed out after #{write_timeout} seconds"
  end
end