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.



5
6
7
8
# File 'lib/recurly/connection_pool.rb', line 5

def initialize
  @mutex = Mutex.new
  @pool = []
end

Instance Method Details

#init_http_connectionObject



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

def init_http_connection
  http = Net::HTTP.new(Client::BASE_HOST, Client::BASE_PORT)
  http.use_ssl = true
  http.ca_file = Client::CA_FILE
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  http.keep_alive_timeout = 600

  http
end

#with_connectionObject



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

def with_connection
  http = nil
  @mutex.synchronize do
    http = @pool.pop
  end

  # create connection if the pool was empty
  http ||= init_http_connection

  response = yield http

  if http.started?
    @mutex.synchronize do
      @pool.push(http)
    end
  end

  response
end