class Recurly::ConnectionPool

Public Class Methods

new() click to toggle source
# File lib/recurly/connection_pool.rb, line 7
def initialize
  @mutex = Mutex.new
  @pool = Hash.new { |h, k| h[k] = [] }
end

Public Instance Methods

init_http_connection(uri, ca_file) click to toggle source
# 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) { |http| ... } click to toggle source
# 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