class RorVsWild::Client

Constants

CERTIFICATE_AUTHORITIES_PATH
DEFAULT_TIMEOUT
HTTPS

Attributes

api_key[R]
api_url[R]
config[R]
threads[R]
timeout[R]

Public Class Methods

new(config) click to toggle source
# 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

Public Instance Methods

at_exit() click to toggle source
# File lib/rorvswild/client.rb, line 104
def at_exit
  threads.each(&:join)
end
max_connections() click to toggle source
# File lib/rorvswild/client.rb, line 51
def max_connections
  @max_connections ||= [Process.getrlimit(Process::RLIMIT_NOFILE).first / 10, 10].max
end
new_http() click to toggle source
# 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) click to toggle source
# 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 = data.to_json
  transmit(post)
end
post_async(path, data) click to toggle source
# 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) click to toggle source
# File lib/rorvswild/client.rb, line 47
def release_connection(http)
  @mutex.synchronize { @connections.push(http) } if http
end
take_connection() click to toggle source
# File lib/rorvswild/client.rb, line 43
def take_connection
  @mutex.synchronize { @connections.shift }
end
take_or_create_connection() click to toggle source
# 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) click to toggle source
# 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