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.



10
11
12
13
14
15
# File 'lib/http/timeout/global.rb', line 10

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.



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

def time_left
  @time_left
end

#total_timeoutObject (readonly)

Returns the value of attribute total_timeout.



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

def total_timeout
  @total_timeout
end

Instance Method Details

#connect(socket_class, host, port) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/http/timeout/global.rb', line 17

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



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

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



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/http/timeout/global.rb', line 45

def readpartial(size)
  reset_timer

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

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

Write to the socket



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/http/timeout/global.rb', line 60

def write(data)
  reset_timer

  begin
    return socket.write_nonblock(data)
  rescue IO::WaitWritable
    IO.select(nil, [socket], nil, time_left)
    log_time
    retry
  end
rescue EOFError
  :eof
end