Class: Sumologic::Http::ConnectionPool

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

Overview

Thread-safe connection pool for HTTP clients Allows multiple threads to have their own connections

Constant Summary collapse

DEFAULT_READ_TIMEOUT =
60
DEFAULT_OPEN_TIMEOUT =
10

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, max_connections: 10, read_timeout: nil, connect_timeout: nil) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.



13
14
15
16
17
18
19
20
# File 'lib/sumologic/http/connection_pool.rb', line 13

def initialize(base_url:, max_connections: 10, read_timeout: nil, connect_timeout: nil)
  @base_url = base_url
  @max_connections = max_connections
  @read_timeout = read_timeout || DEFAULT_READ_TIMEOUT
  @connect_timeout = connect_timeout || DEFAULT_OPEN_TIMEOUT
  @pool = []
  @mutex = Mutex.new
end

Instance Method Details

#close_allObject

Close all connections in the pool



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

def close_all
  @mutex.synchronize do
    @pool.each do |conn|
      conn[:http].finish if conn[:http].started?
    rescue StandardError => e
      warn "Error closing connection: #{e.message}"
    end
    @pool.clear
  end
end

#with_connection(uri) ⇒ Object

Get a connection from the pool (or create new one)



23
24
25
26
27
28
# File 'lib/sumologic/http/connection_pool.rb', line 23

def with_connection(uri)
  connection = acquire_connection(uri)
  yield connection
ensure
  release_connection(connection) if connection
end