Class: RorVsWild::Client
- Inherits:
-
Object
- Object
- RorVsWild::Client
- Defined in:
- lib/rorvswild/client.rb
Constant Summary collapse
- HTTPS =
"https".freeze
- CERTIFICATE_AUTHORITIES_PATH =
File.("../../../cacert.pem", __FILE__)
- DEFAULT_TIMEOUT =
1
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
#threads ⇒ Object
readonly
Returns the value of attribute threads.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #at_exit ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #max_connections ⇒ Object
- #new_http ⇒ Object
- #post(path, data) ⇒ Object
- #post_async(path, data) ⇒ Object
- #release_connection(http) ⇒ Object
- #take_connection ⇒ Object
- #take_or_create_connection ⇒ Object
- #transmit(request) ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 22 23 24 |
# 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 end |
Instance Attribute Details
#api_key ⇒ Object (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_url ⇒ Object (readonly)
Returns the value of attribute api_url.
12 13 14 |
# File 'lib/rorvswild/client.rb', line 12 def api_url @api_url end |
#threads ⇒ Object (readonly)
Returns the value of attribute threads.
12 13 14 |
# File 'lib/rorvswild/client.rb', line 12 def threads @threads end |
#timeout ⇒ Object (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_exit ⇒ Object
90 91 92 |
# File 'lib/rorvswild/client.rb', line 90 def at_exit threads.each(&:join) end |
#max_connections ⇒ Object
43 44 45 |
# File 'lib/rorvswild/client.rb', line 43 def max_connections @max_connections ||= [Process.getrlimit(Process::RLIMIT_NOFILE).first / 10, 10].max end |
#new_http ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rorvswild/client.rb', line 64 def new_http uri = URI(api_url) http = Net::HTTP.new(uri.host, uri.port) http.open_timeout = timeout 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
26 27 28 29 30 31 32 33 |
# File 'lib/rorvswild/client.rb', line 26 def post(path, data) uri = URI(api_url + path) post = Net::HTTP::Post.new(uri.path, "X-Gem-Version".freeze => RorVsWild::VERSION) post.content_type = "application/json".freeze post.basic_auth(nil, api_key) post.body = data.to_json transmit(post) end |
#post_async(path, data) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rorvswild/client.rb', line 79 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
39 40 41 |
# File 'lib/rorvswild/client.rb', line 39 def release_connection(http) @mutex.synchronize { @connections.push(http) } if http end |
#take_connection ⇒ Object
35 36 37 |
# File 'lib/rorvswild/client.rb', line 35 def take_connection @mutex.synchronize { @connections.shift } end |
#take_or_create_connection ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/rorvswild/client.rb', line 47 def take_or_create_connection if http = take_connection http elsif @connection_count < max_connections @connection_count += 1 new_http end end |
#transmit(request) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/rorvswild/client.rb', line 56 def transmit(request) if http = take_or_create_connection http.request(request) end ensure release_connection(http) end |