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.



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

def initialize(*args)
  super
  reset_counter
end

Instance Attribute Details

#time_leftObject (readonly)

Returns the value of attribute time_left.



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

def time_left
  @time_left
end

#total_timeoutObject (readonly)

Returns the value of attribute total_timeout.



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

def total_timeout
  @total_timeout
end

Instance Method Details

#connect(socket_class, host, port, nodelay = false) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/http/timeout/global.rb', line 23

def connect(socket_class, host, port, nodelay = false)
  reset_timer
  ::Timeout.timeout(time_left, TimeoutError) do
    @socket = socket_class.open(host, port)
    @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay
  end

  log_time
end

#connect_sslObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/http/timeout/global.rb', line 33

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



50
51
52
# File 'lib/http/timeout/global.rb', line 50

def readpartial(size)
  perform_io { read_nonblock(size) }
end

#reset_counterObject

To future me: Don't remove this again, past you was smarter.



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

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

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

Write to the socket



55
56
57
# File 'lib/http/timeout/global.rb', line 55

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