Class: Net::Hippie::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/net/hippie/connection.rb

Overview

Persistent HTTP connection with automatic reconnection.

Constant Summary collapse

RETRYABLE_ERRORS =
[EOFError, Errno::ECONNABORTED, Errno::ECONNRESET, Errno::EPIPE, IOError].freeze

Instance Method Summary collapse

Constructor Details

#initialize(scheme, host, port, ipaddr, options = {}) ⇒ Connection

Returns a new instance of Connection.



9
10
11
12
13
# File 'lib/net/hippie/connection.rb', line 9

def initialize(scheme, host, port, ipaddr, options = {})
  @mutex = Mutex.new
  @created_at = Time.now
  @http = build_http(scheme, host, port, ipaddr, options)
end

Instance Method Details

#build_url_for(path) ⇒ Object



32
33
34
35
36
# File 'lib/net/hippie/connection.rb', line 32

def build_url_for(path)
  return path if path.start_with?('http')

  "#{@http.use_ssl? ? 'https' : 'http'}://#{@http.address}#{path}"
end

#closeObject



26
27
28
29
30
# File 'lib/net/hippie/connection.rb', line 26

def close
  @mutex.synchronize { @http.finish if @http.started? }
rescue IOError
  nil
end

#run(request, &block) ⇒ Object



15
16
17
18
19
20
# File 'lib/net/hippie/connection.rb', line 15

def run(request, &block)
  @mutex.synchronize do
    ensure_started
    execute(request, &block)
  end
end

#stale?(ttl) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/net/hippie/connection.rb', line 22

def stale?(ttl)
  (Time.now - @created_at) > ttl
end