Class: HTTP::Timeout::Global

Inherits:
PerOperation show all
Defined in:
lib/http/timeout/global.rb

Constant Summary

Constants inherited from PerOperation

PerOperation::CONNECT_TIMEOUT, PerOperation::READ_TIMEOUT, PerOperation::WRITE_TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from PerOperation

#connect_timeout, #read_timeout, #write_timeout

Attributes inherited from Null

#options, #socket

Instance Method Summary collapse

Methods inherited from Null

#start_tls

Constructor Details

#initialize(*args) ⇒ Global

Returns a new instance of Global.



6
7
8
9
10
11
# File 'lib/http/timeout/global.rb', line 6

def initialize(*args)
  super

  @time_left = connect_timeout + read_timeout + write_timeout
  @total_timeout = time_left
end

Instance Attribute Details

#time_leftObject (readonly)

Returns the value of attribute time_left.



4
5
6
# File 'lib/http/timeout/global.rb', line 4

def time_left
  @time_left
end

#total_timeoutObject (readonly)

Returns the value of attribute total_timeout.



4
5
6
# File 'lib/http/timeout/global.rb', line 4

def total_timeout
  @total_timeout
end

Instance Method Details

#connect(socket_class, host, port) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/http/timeout/global.rb', line 13

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

  log_time
end

#connect_sslObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/http/timeout/global.rb', line 22

def connect_ssl
  reset_timer

  begin
    socket.connect_nonblock
  rescue IO::WaitReadable
    IO.select([socket], nil, nil, time_left)
    log_time
    retry
  rescue IO::WaitWritable
    IO.select(nil, [socket], nil, time_left)
    log_time
    retry
  end
end

#readpartial(size) ⇒ Object

Read from the socket



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/http/timeout/global.rb', line 39

def readpartial(size)
  reset_timer

  begin
    socket.read_nonblock(size)
  rescue IO::WaitReadable
    IO.select([socket], nil, nil, time_left)
    log_time
    retry
  end
end

#write(data) ⇒ Object Also known as: <<

Write to the socket



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/http/timeout/global.rb', line 52

def write(data)
  reset_timer

  begin
    socket << data
  rescue IO::WaitWritable
    IO.select(nil, [socket], nil, time_left)
    log_time
    retry
  end
end