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 =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.


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

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)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key


12
13
14
# File 'lib/rorvswild/client.rb', line 12

def api_key
  @api_key
end

#api_urlObject (readonly)

Returns the value of attribute api_url


12
13
14
# File 'lib/rorvswild/client.rb', line 12

def api_url
  @api_url
end

#threadsObject (readonly)

Returns the value of attribute threads


12
13
14
# File 'lib/rorvswild/client.rb', line 12

def threads
  @threads
end

#timeoutObject (readonly)

Returns the value of attribute timeout


12
13
14
# File 'lib/rorvswild/client.rb', line 12

def timeout
  @timeout
end

Instance Method Details

#at_exitObject


99
100
101
# File 'lib/rorvswild/client.rb', line 99

def at_exit
  threads.each(&:join)
end

#max_connectionsObject


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

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

#new_httpObject


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rorvswild/client.rb', line 72

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


32
33
34
35
36
37
38
# File 'lib/rorvswild/client.rb', line 32

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 = data.to_json
  transmit(post)
end

#post_async(path, data) ⇒ Object


88
89
90
91
92
93
94
95
96
97
# File 'lib/rorvswild/client.rb', line 88

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


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

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

#take_connectionObject


40
41
42
# File 'lib/rorvswild/client.rb', line 40

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

#take_or_create_connectionObject


52
53
54
55
56
57
58
59
60
# File 'lib/rorvswild/client.rb', line 52

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


62
63
64
65
66
67
68
69
70
# File 'lib/rorvswild/client.rb', line 62

def transmit(request)
  if http = take_or_create_connection
    http.request(request)
  end
rescue Exception => ex
  RorVsWild.logger.error(ex.full_message)
ensure
  release_connection(http)
end