Class: RorVsWild::Client

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

Constant Summary collapse

HTTPS =
"https".freeze
CERTIFICATE_AUTHORITIES_PATH =
File.expand_path("../../../cacert.pem", __FILE__)
DEFAULT_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rorvswild/client.rb', line 16

def initialize(config)
  Kernel.at_exit(&method(:at_exit))
  @api_url = config[:api_url]
  @api_key = config[:api_key]
  @timeout ||= config[:timeout] || DEFAULT_TIMEOUT
  @threads = Set.new
  @connections = []
  @connection_count = 0
  @mutex = Mutex.new
  @config = config
  @headers = {
    "Content-Type" => "application/json",
    "X-RorVsWild-Version" => RorVsWild::VERSION,
    "X-Ruby-Version" => RUBY_VERSION,
  }
  @headers["X-Rails-Version"] = Rails.version if defined?(Rails)
  @http_unauthorized = false
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



14
15
16
# File 'lib/rorvswild/client.rb', line 14

def api_key
  @api_key
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



14
15
16
# File 'lib/rorvswild/client.rb', line 14

def api_url
  @api_url
end

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/rorvswild/client.rb', line 14

def config
  @config
end

#threadsObject (readonly)

Returns the value of attribute threads.



14
15
16
# File 'lib/rorvswild/client.rb', line 14

def threads
  @threads
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



14
15
16
# File 'lib/rorvswild/client.rb', line 14

def timeout
  @timeout
end

Instance Method Details

#at_exitObject



104
105
106
# File 'lib/rorvswild/client.rb', line 104

def at_exit
  threads.each(&:join)
end

#max_connectionsObject



51
52
53
# File 'lib/rorvswild/client.rb', line 51

def max_connections
  @max_connections ||= [Process.getrlimit(Process::RLIMIT_NOFILE).first / 10, 10].max
end

#new_httpObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rorvswild/client.rb', line 77

def new_http
  uri = URI(api_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = timeout
  http.keep_alive_timeout = 5

  if uri.scheme == HTTPS
    # Disable peer verification while there is a memory leak with OpenSSL
    # http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    # http.ca_file = CERTIFICATE_AUTHORITIES_PATH
    http.use_ssl = true
  end

  http
end

#post(path, data) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rorvswild/client.rb', line 35

def post(path, data)
  uri = URI(api_url + path)
  post = Net::HTTP::Post.new(uri.path, @headers)
  post.basic_auth(nil, api_key)
  post.body = JSON.generate(data)
  transmit(post)
end

#post_async(path, data) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/rorvswild/client.rb', line 93

def post_async(path, data)
  Thread.new do
    begin
      threads.add(Thread.current)
      post(path, data)
    ensure
      threads.delete(Thread.current)
    end
  end
end

#release_connection(http) ⇒ Object



47
48
49
# File 'lib/rorvswild/client.rb', line 47

def release_connection(http)
  @mutex.synchronize { @connections.push(http) } if http
end

#take_connectionObject



43
44
45
# File 'lib/rorvswild/client.rb', line 43

def take_connection
  @mutex.synchronize { @connections.shift }
end

#take_or_create_connectionObject



55
56
57
58
59
60
61
62
63
# File 'lib/rorvswild/client.rb', line 55

def take_or_create_connection
  if http = take_connection
    http.start unless http.active?
    http
  elsif @connection_count < max_connections
    @connection_count += 1
    new_http
  end
end

#transmit(request) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rorvswild/client.rb', line 65

def transmit(request)
  if !@http_unauthorized && http = take_or_create_connection
    response = http.request(request)
    @http_unauthorized = true if response.code == "401"
    response
  end
rescue Exception => ex
  RorVsWild.logger.error(ex.full_message)
ensure
  release_connection(http)
end