Class: HTTP::Timeout::PerOperation

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

Constant Summary collapse

CONNECT_TIMEOUT =
0.25
WRITE_TIMEOUT =
0.25
READ_TIMEOUT =
0.25

Instance Attribute Summary

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 Method Details

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



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

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

#connect_sslObject



29
30
31
32
33
34
35
# File 'lib/http/timeout/per_operation.rb', line 29

def connect_ssl
  rescue_readable(@connect_timeout) do
    rescue_writable(@connect_timeout) do
      @socket.connect_nonblock
    end
  end
end

#readpartial(size, buffer = nil) ⇒ Object

Read data from the socket



40
41
42
43
44
45
46
# File 'lib/http/timeout/per_operation.rb', line 40

def readpartial(size, buffer = nil)
  rescue_readable do
    @socket.read_nonblock(size, buffer)
  end
rescue EOFError
  :eof
end

#write(data) ⇒ Object

Write data to the socket



49
50
51
52
53
54
55
# File 'lib/http/timeout/per_operation.rb', line 49

def write(data)
  rescue_writable do
    @socket.write_nonblock(data)
  end
rescue EOFError
  :eof
end