Class: ActivePostgres::ConnectionPooler

Inherits:
Object
  • Object
show all
Defined in:
lib/active_postgres/connection_pooler.rb

Overview

Production-ready connection pooling configuration for PgBouncer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, ssh_executor, logger = Logger.new) ⇒ ConnectionPooler

Returns a new instance of ConnectionPooler.



6
7
8
9
10
# File 'lib/active_postgres/connection_pooler.rb', line 6

def initialize(config, ssh_executor, logger = Logger.new)
  @config = config
  @ssh_executor = ssh_executor
  @logger = logger
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/active_postgres/connection_pooler.rb', line 4

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/active_postgres/connection_pooler.rb', line 4

def logger
  @logger
end

#ssh_executorObject (readonly)

Returns the value of attribute ssh_executor.



4
5
6
# File 'lib/active_postgres/connection_pooler.rb', line 4

def ssh_executor
  @ssh_executor
end

Class Method Details

.calculate_default_pool_size_static(max_connections) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/active_postgres/connection_pooler.rb', line 48

def self.calculate_default_pool_size_static(max_connections)
  # Formula: Reserve 80% of PostgreSQL connections for the pool
  # Split among expected number of databases/users
  pool_per_db = (max_connections * 0.8 / 4).to_i # Assume 4 databases

  # Reasonable bounds: 20-100 per pool
  pool_per_db.clamp(20, 100)
end

.calculate_optimal_pool_sizes(max_connections) ⇒ Object

Calculate optimal pool settings based on PostgreSQL max_connections This is a simpler method used by the PgBouncer component for template integration



37
38
39
40
41
42
43
44
45
46
# File 'lib/active_postgres/connection_pooler.rb', line 37

def self.calculate_optimal_pool_sizes(max_connections)
  {
    default_pool_size: calculate_default_pool_size_static(max_connections),
    min_pool_size: 5,
    reserve_pool_size: 5,
    max_client_conn: max_connections * 10,
    max_db_connections: [max_connections - 10, 10].max,
    max_user_connections: [max_connections - 10, 10].max
  }
end

Instance Method Details

#setup_on_host(host) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_postgres/connection_pooler.rb', line 12

def setup_on_host(host)
  @logger.info "Configuring optimized connection pooling on #{host}..."

  # Analyze PostgreSQL configuration to determine optimal pool settings
  pg_settings = get_postgresql_settings(host)
  pool_config = calculate_pool_settings(pg_settings)

  # Install PgBouncer
  install_pgbouncer(host)

  # Deploy optimized configuration
  deploy_pgbouncer_config(host, pool_config)

  # Setup authentication
  setup_authentication(host)

  # Enable and start PgBouncer
  enable_pgbouncer(host)

  # Verify the setup
  verify_pgbouncer(host)
end