Class: HTTP::Timeout::Global

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

Instance Attribute Summary

Attributes inherited from Null

#options, #socket

Instance Method Summary collapse

Methods inherited from Null

#close, #closed?, #start_tls

Constructor Details

#initialize(*args) ⇒ Global

Returns a new instance of Global.



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

def initialize(*args)
  super

  @timeout = @time_left = options.fetch(:global_timeout)
end

Instance Method Details

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



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

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

  log_time
end

#connect_sslObject



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

def connect_ssl
  reset_timer

  begin
    @socket.connect_nonblock
  rescue IO::WaitReadable
    wait_readable_or_timeout
    retry
  rescue IO::WaitWritable
    wait_writable_or_timeout
    retry
  end
end

#readpartial(size, buffer = nil) ⇒ Object

Read from the socket



47
48
49
# File 'lib/http/timeout/global.rb', line 47

def readpartial(size, buffer = nil)
  perform_io { read_nonblock(size, buffer) }
end

#reset_counterObject

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



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

def reset_counter
  @time_left = @timeout
end

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

Write to the socket



52
53
54
# File 'lib/http/timeout/global.rb', line 52

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