Class: Recurly::ConnectionPool

Inherits:
Object
  • Object
show all
Defined in:
lib/recurly/connection_pool.rb

Instance Method Summary collapse

Constructor Details

#initializeConnectionPool

Returns a new instance of ConnectionPool.



7
8
9
10
# File 'lib/recurly/connection_pool.rb', line 7

def initialize
  @mutex = Mutex.new
  @pool = Hash.new { |h, k| h[k] = [] }
end

Instance Method Details

#init_http_connection(uri, ca_file) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/recurly/connection_pool.rb', line 32

def init_http_connection(uri, ca_file)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.ca_file = ca_file
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http.keep_alive_timeout = 600

  http
end

#with_connection(uri:, ca_file: nil) ⇒ Object



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

def with_connection(uri:, ca_file: nil)
  http = nil
  @mutex.synchronize do
    http = @pool[[uri.host, uri.port]].pop
  end

  # create connection if the pool was empty
  http ||= init_http_connection(uri, ca_file)

  response = yield http

  if http.started?
    @mutex.synchronize do
      @pool[[uri.host, uri.port]].push(http)
    end
  end

  response
end