Sha256: 663a894bec0e58fe2d00647f023772133e44140014b8eada7968d600287956c7

Contents?: true

Size: 890 Bytes

Versions: 46

Compression:

Stored size: 890 Bytes

Contents

# frozen_string_literal: true

require "net/https"

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

    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

    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
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
recurly-4.56.0 lib/recurly/connection_pool.rb
recurly-4.55.0 lib/recurly/connection_pool.rb
recurly-4.54.0 lib/recurly/connection_pool.rb
recurly-4.53.0 lib/recurly/connection_pool.rb
recurly-4.52.0 lib/recurly/connection_pool.rb
recurly-4.49.0 lib/recurly/connection_pool.rb
recurly-4.48.1 lib/recurly/connection_pool.rb
recurly-4.48.0 lib/recurly/connection_pool.rb
recurly-4.47.0 lib/recurly/connection_pool.rb
recurly-4.46.0 lib/recurly/connection_pool.rb
recurly-4.45.0 lib/recurly/connection_pool.rb
recurly-4.44.0 lib/recurly/connection_pool.rb
recurly-4.43.0 lib/recurly/connection_pool.rb
recurly-4.42.0 lib/recurly/connection_pool.rb
recurly-4.41.0 lib/recurly/connection_pool.rb
recurly-4.40.0 lib/recurly/connection_pool.rb
recurly-4.39.0 lib/recurly/connection_pool.rb
recurly-4.38.0 lib/recurly/connection_pool.rb
recurly-4.37.0 lib/recurly/connection_pool.rb
recurly-4.36.0 lib/recurly/connection_pool.rb