Class: ClickhouseRuby::ConnectionPool
- Inherits:
-
Object
- Object
- ClickhouseRuby::ConnectionPool
- Defined in:
- lib/clickhouse_ruby/connection_pool.rb
Overview
Thread-safe connection pool for managing multiple ClickHouse connections
Features:
-
Thread-safe checkout/checkin with mutex
-
Configurable pool size and timeout
-
Automatic health checks before returning connections
-
with_connection block pattern for safe usage
-
Idle connection cleanup
Instance Attribute Summary collapse
-
#size ⇒ Integer
readonly
Maximum number of connections in the pool.
-
#timeout ⇒ Integer
readonly
Timeout in seconds when waiting for a connection.
Instance Method Summary collapse
-
#available_count ⇒ Integer
Returns the number of currently available connections.
-
#checkin(connection) ⇒ void
Returns a connection to the pool.
-
#checkout ⇒ Connection
Checks out a connection from the pool.
-
#cleanup(max_idle_seconds = 300) ⇒ Integer
Removes idle/unhealthy connections from the pool.
-
#exhausted? ⇒ Boolean
Checks if all connections are currently in use.
-
#health_check ⇒ Hash
Pings all available connections to check health.
-
#in_use_count ⇒ Integer
Returns the number of connections currently in use.
-
#initialize(config, size: nil, timeout: nil) ⇒ ConnectionPool
constructor
Creates a new connection pool.
-
#inspect ⇒ String
Returns a string representation of the pool.
-
#shutdown ⇒ void
Closes all connections and resets the pool.
-
#stats ⇒ Hash
Returns pool statistics.
-
#total_count ⇒ Integer
Returns the total number of connections (available + in use).
-
#with_connection {|Connection| ... } ⇒ Object
Executes a block with a checked-out connection.
Constructor Details
#initialize(config, size: nil, timeout: nil) ⇒ ConnectionPool
Creates a new connection pool
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 42 def initialize(config, size: nil, timeout: nil) @config = config @size = size || config.pool_size @timeout = timeout || config.pool_timeout @connection_options = config. # Pool state @available = [] # Connections available for checkout @in_use = [] # Connections currently checked out @all_connections = [] # All connections ever created # Synchronization @mutex = Mutex.new @condition = ConditionVariable.new # Stats @total_checkouts = 0 @total_timeouts = 0 @created_at = Time.now end |
Instance Attribute Details
#size ⇒ Integer (readonly)
Returns maximum number of connections in the pool.
32 33 34 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 32 def size @size end |
#timeout ⇒ Integer (readonly)
Returns timeout in seconds when waiting for a connection.
35 36 37 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 35 def timeout @timeout end |
Instance Method Details
#available_count ⇒ Integer
Returns the number of currently available connections
150 151 152 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 150 def available_count @mutex.synchronize { @available.size } end |
#checkin(connection) ⇒ void
This method returns an undefined value.
Returns a connection to the pool
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 128 def checkin(connection) return unless connection @mutex.synchronize do @in_use.delete(connection) # Only return healthy connections to the available pool if connection.healthy? && !connection.stale? @available << connection else # Disconnect unhealthy connections connection.disconnect rescue nil @all_connections.delete(connection) end @condition.signal end end |
#checkout ⇒ Connection
Checks out a connection from the pool
If no connections are available and the pool is at capacity, waits up to @timeout seconds for one to become available.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 89 def checkout deadline = Time.now + @timeout @mutex.synchronize do loop do # Try to get an available connection if (conn = get_available_connection) @in_use << conn @total_checkouts += 1 return conn end # Try to create a new connection if under capacity if @all_connections.size < @size conn = create_connection @in_use << conn @total_checkouts += 1 return conn end # Wait for a connection to be returned remaining = deadline - Time.now if remaining <= 0 @total_timeouts += 1 raise PoolTimeout.new( "Could not obtain a connection from the pool within #{@timeout} seconds " \ "(pool size: #{@size}, in use: #{@in_use.size})" ) end @condition.wait(@mutex, remaining) end end end |
#cleanup(max_idle_seconds = 300) ⇒ Integer
Removes idle/unhealthy connections from the pool
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 198 def cleanup(max_idle_seconds = 300) removed = 0 @mutex.synchronize do @available.reject! do |conn| if conn.stale?(max_idle_seconds) || !conn.healthy? conn.disconnect rescue nil @all_connections.delete(conn) removed += 1 true else false end end end removed end |
#exhausted? ⇒ Boolean
Checks if all connections are currently in use
171 172 173 174 175 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 171 def exhausted? @mutex.synchronize do @available.empty? && @all_connections.size >= @size end end |
#health_check ⇒ Hash
Pings all available connections to check health
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 220 def health_check @mutex.synchronize do healthy = 0 unhealthy = 0 @available.each do |conn| if conn.ping healthy += 1 else unhealthy += 1 end end { available: @available.size, in_use: @in_use.size, total: @all_connections.size, capacity: @size, healthy: healthy, unhealthy: unhealthy } end end |
#in_use_count ⇒ Integer
Returns the number of connections currently in use
157 158 159 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 157 def in_use_count @mutex.synchronize { @in_use.size } end |
#inspect ⇒ String
Returns a string representation of the pool
264 265 266 267 268 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 264 def inspect @mutex.synchronize do "#<#{self.class.name} size=#{@size} available=#{@available.size} in_use=#{@in_use.size}>" end end |
#shutdown ⇒ void
This method returns an undefined value.
Closes all connections and resets the pool
This should be called when shutting down the application.
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 182 def shutdown @mutex.synchronize do (@available + @in_use).each do |conn| conn.disconnect rescue nil end @available.clear @in_use.clear @all_connections.clear end end |
#stats ⇒ Hash
Returns pool statistics
247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 247 def stats @mutex.synchronize do { size: @size, available: @available.size, in_use: @in_use.size, total_connections: @all_connections.size, total_checkouts: @total_checkouts, total_timeouts: @total_timeouts, uptime_seconds: Time.now - @created_at } end end |
#total_count ⇒ Integer
Returns the total number of connections (available + in use)
164 165 166 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 164 def total_count @mutex.synchronize { @all_connections.size } end |
#with_connection {|Connection| ... } ⇒ Object
Executes a block with a checked-out connection
This is the recommended way to use the pool. The connection is automatically returned to the pool when the block completes, even if an exception is raised.
72 73 74 75 76 77 78 79 |
# File 'lib/clickhouse_ruby/connection_pool.rb', line 72 def with_connection conn = checkout begin yield conn ensure checkin(conn) end end |