Class: ClickhouseRuby::ConnectionPool

Inherits:
Object
  • Object
show all
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

Examples:

Basic usage with block (recommended)

pool = ClickhouseRuby::ConnectionPool.new(config)
pool.with_connection do |conn|
  response = conn.post('/query', 'SELECT 1')
end

Manual checkout/checkin (use with caution)

conn = pool.checkout
begin
  response = conn.post('/query', 'SELECT 1')
ensure
  pool.checkin(conn)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, size: nil, timeout: nil) ⇒ ConnectionPool

Creates a new connection pool

Parameters:

  • config (Configuration)

    connection configuration

  • size (Integer) (defaults to: nil)

    maximum pool size (default from config)

  • timeout (Integer) (defaults to: nil)

    wait timeout in seconds (default from config)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/clickhouse_ruby/connection_pool.rb', line 41

def initialize(config, size: nil, timeout: nil)
  @config = config
  @size = size || config.pool_size
  @timeout = timeout || config.pool_timeout
  @connection_options = config.to_connection_options

  # 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

#sizeInteger (readonly)

Returns maximum number of connections in the pool.

Returns:

  • (Integer)

    maximum number of connections in the pool



31
32
33
# File 'lib/clickhouse_ruby/connection_pool.rb', line 31

def size
  @size
end

#timeoutInteger (readonly)

Returns timeout in seconds when waiting for a connection.

Returns:

  • (Integer)

    timeout in seconds when waiting for a connection



34
35
36
# File 'lib/clickhouse_ruby/connection_pool.rb', line 34

def timeout
  @timeout
end

Instance Method Details

#available_countInteger

Returns the number of currently available connections

Returns:

  • (Integer)


169
170
171
# File 'lib/clickhouse_ruby/connection_pool.rb', line 169

def available_count
  @mutex.synchronize { @available.size }
end

#checkin(connection) ⇒ void

This method returns an undefined value.

Returns a connection to the pool

Parameters:

  • connection (Connection)

    the connection to return



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/clickhouse_ruby/connection_pool.rb', line 147

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
      safe_disconnect(connection)
      @all_connections.delete(connection)
    end

    @condition.signal
  end
end

#checkoutConnection

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.

Returns:

Raises:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/clickhouse_ruby/connection_pool.rb', line 103

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
        # Publish pool timeout event
        Instrumentation.publish(Instrumentation::EVENTS[:pool_timeout], {
          pool_id: object_id,
          wait_time_ms: @timeout * 1000,
          pool_size: @size,
          in_use: @in_use.size,
        },)
        raise PoolTimeout, "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

Parameters:

  • max_idle_seconds (Integer) (defaults to: 300)

    maximum idle time before removal

Returns:

  • (Integer)

    number of connections removed



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/clickhouse_ruby/connection_pool.rb', line 217

def cleanup(max_idle_seconds = 300)
  removed = 0

  @mutex.synchronize do
    @available.reject! do |conn|
      if conn.stale?(max_idle_seconds) || !conn.healthy?
        safe_disconnect(conn)
        @all_connections.delete(conn)
        removed += 1
        true
      else
        false
      end
    end
  end

  removed
end

#detailed_statsHash

Returns detailed pool statistics for monitoring

Provides comprehensive metrics suitable for Prometheus/StatsD export.

Examples:

pool.detailed_stats
# => {
#   capacity: 5,
#   connections: { total: 5, available: 3, in_use: 2 },
#   utilization_percent: 40.0,
#   checkouts: { total: 1000, rate_per_minute: 16.7 },
#   timeouts: { total: 5, rate_per_minute: 0.08 },
#   uptime_seconds: 3600
# }

Returns:

  • (Hash)

    detailed pool statistics



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/clickhouse_ruby/connection_pool.rb', line 296

def detailed_stats
  @mutex.synchronize do
    uptime = Time.now - @created_at
    uptime_minutes = uptime / 60.0

    in_use = @in_use.size
    available = @available.size
    total = @all_connections.size
    utilization = total.positive? ? (in_use.to_f / total * 100).round(2) : 0.0

    checkout_rate = uptime_minutes.positive? ? (@total_checkouts / uptime_minutes).round(2) : 0.0
    timeout_rate = uptime_minutes.positive? ? (@total_timeouts / uptime_minutes).round(4) : 0.0

    {
      capacity: @size,
      connections: {
        total: total,
        available: available,
        in_use: in_use,
      },
      utilization_percent: utilization,
      checkouts: {
        total: @total_checkouts,
        rate_per_minute: checkout_rate,
      },
      timeouts: {
        total: @total_timeouts,
        rate_per_minute: timeout_rate,
      },
      uptime_seconds: uptime.round(2),
    }
  end
end

#exhausted?Boolean

Checks if all connections are currently in use

Returns:

  • (Boolean)


190
191
192
193
194
# File 'lib/clickhouse_ruby/connection_pool.rb', line 190

def exhausted?
  @mutex.synchronize do
    @available.empty? && @all_connections.size >= @size
  end
end

#health_checkHash

Pings all available connections to check health

Returns:

  • (Hash)

    status report



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/clickhouse_ruby/connection_pool.rb', line 239

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_countInteger

Returns the number of connections currently in use

Returns:

  • (Integer)


176
177
178
# File 'lib/clickhouse_ruby/connection_pool.rb', line 176

def in_use_count
  @mutex.synchronize { @in_use.size }
end

#inspectString

Returns a string representation of the pool

Returns:

  • (String)


333
334
335
336
337
# File 'lib/clickhouse_ruby/connection_pool.rb', line 333

def inspect
  @mutex.synchronize do
    "#<#{self.class.name} size=#{@size} available=#{@available.size} in_use=#{@in_use.size}>"
  end
end

#shutdownvoid

This method returns an undefined value.

Closes all connections and resets the pool

This should be called when shutting down the application.



201
202
203
204
205
206
207
208
209
210
211
# File 'lib/clickhouse_ruby/connection_pool.rb', line 201

def shutdown
  @mutex.synchronize do
    (@available + @in_use).each do |conn|
      safe_disconnect(conn)
    end

    @available.clear
    @in_use.clear
    @all_connections.clear
  end
end

#statsHash

Returns pool statistics

Returns:

  • (Hash)

    pool statistics



266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/clickhouse_ruby/connection_pool.rb', line 266

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).round(2),
    }
  end
end

#total_countInteger

Returns the total number of connections (available + in use)

Returns:

  • (Integer)


183
184
185
# File 'lib/clickhouse_ruby/connection_pool.rb', line 183

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.

Yields:

Returns:

  • (Object)

    the block’s return value

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/clickhouse_ruby/connection_pool.rb', line 71

def with_connection
  started_at = Instrumentation.monotonic_time
  conn = checkout
  wait_time_ms = Instrumentation.duration_ms(started_at)

  # Publish pool checkout event
  Instrumentation.publish(Instrumentation::EVENTS[:pool_checkout], {
    pool_id: object_id,
    wait_time_ms: wait_time_ms,
    connection_id: conn.object_id,
  },)

  begin
    yield conn
  ensure
    checkin(conn)
    # Publish pool checkin event
    Instrumentation.publish(Instrumentation::EVENTS[:pool_checkin], {
      pool_id: object_id,
      connection_id: conn.object_id,
    },)
  end
end