Class: HTTPray::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/httpray.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port, timeout = 1, ssl_context = nil, retry_count = 1, retry_circuit_breaker_interval = 10) ⇒ Connection

Returns a new instance of Connection.



18
19
20
21
22
23
24
25
26
27
# File 'lib/httpray.rb', line 18

def initialize(host, port, timeout = 1, ssl_context = nil, retry_count = 1, retry_circuit_breaker_interval = 10)
  @host = host
  @port = port
  @timeout = timeout
  @ssl_context = ssl_context
  @retry_count = retry_count
  @retry_circuit_breaker_interval = retry_circuit_breaker_interval
  @next_retry_time = Time.now - @retry_circuit_breaker_interval
  @socket = connect
end

Instance Method Details

#connectObject



116
117
118
119
# File 'lib/httpray.rb', line 116

def connect
  socket, _ = connect2
  socket
end

#connect2Object

private



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/httpray.rb', line 78

def connect2
  address = Socket.getaddrinfo(@host, nil, Socket::AF_INET).first[3]
  socket_address = Socket.pack_sockaddr_in(@port, address)

  socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
  socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)

  expire_time = Time.now + @timeout
  begin
    raise Timeout if Time.now > expire_time
    socket.connect_nonblock(socket_address)
  rescue Errno::EISCONN
    nil #continue
  rescue IO::WaitReadable, IO::WaitWritable, Errno::EADDRNOTAVAIL
    select_timeout = expire_time - Time.now
    select_timeout = 0 if select_timeout < 0
    IO.select([socket], [socket], [socket], select_timeout)
    retry
  end

  original_socket = socket
  if @ssl_context
    socket = OpenSSL::SSL::SSLSocket.new(socket, @ssl_context)
    socket.hostname = @host
    socket.sync_close = true
    begin
      raise Timeout if Time.now > expire_time
      socket.connect_nonblock
    rescue IO::WaitReadable, IO::WaitWritable
      select_timeout = expire_time - Time.now
      select_timeout = 0 if select_timeout < 0
      IO.select([socket.io], [socket.io], [socket.io], select_timeout)
      retry
    end
  end
  return socket, original_socket
end

#reconnectObject



35
36
37
# File 'lib/httpray.rb', line 35

def reconnect
  @socket = connect unless @socket && !socket.closed?
end

#request(*args) {|socket| ... } ⇒ Object

Yields:



71
72
73
74
# File 'lib/httpray.rb', line 71

def request(*args)
  socket = request!(*args)
  yield(socket) if block_given?
end

#request!(method, uri, headers = {}, body = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/httpray.rb', line 39

def request!(method, uri, headers = {}, body = nil)
  tries ||= 0
  reconnect
  socket = @socket
  uri = URI.parse(uri) unless URI === uri

  headers = DEFAULT_HEADERS.merge(headers).merge("Host" => uri.host)
  headers["Content-Length"] = body.bytesize if body

  socket.write_nonblock "#{method} #{uri.request_uri} HTTP/1.0\r\n"
  headers.each do |header, value|
    socket.write_nonblock "#{header}: #{value}\r\n"
  end
  socket.write_nonblock "\r\n"
  socket.write_nonblock body if body
  socket
rescue => error
  @socket.close unless @socket.closed?
  now = Time.now
  if now > @next_retry_time
    if tries < @retry_count 
      tries += 1
      retry
    else
      @next_retry_time = now + @retry_circuit_breaker_interval
      raise error
    end
  else
    raise HTTPray::CircuitBreakerError
  end
end

#socketObject

public



31
32
33
# File 'lib/httpray.rb', line 31

def socket
  @socket
end