Class: PgBouncerHero::Database

Inherits:
Object
  • Object
show all
Includes:
Methods::Basics
Defined in:
lib/pgbouncerhero/database.rb

Constant Summary collapse

DEFAULT_POOL_SIZE =
5
DEFAULT_POOL_TIMEOUT =
5

Constants included from Methods::Basics

Methods::Basics::SHUTDOWN_MODES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Methods::Basics

#clients, #conf, #databases, #lists, #pause, #pools, #reconnect, #reload, #resume, #servers, #shutdown, #state, #stats, #summary, #suspend, #users, #wait_close

Constructor Details

#initialize(group, id, config) ⇒ Database

Returns a new instance of Database.



13
14
15
16
17
18
19
20
21
# File 'lib/pgbouncerhero/database.rb', line 13

def initialize(group, id, config)
  @id = id
  @config = config || {}
  @url = URI.parse(@config["url"].to_s)
  @group = group
  @pool_size = positive_integer_setting("pool_size", "PGBOUNCERHERO_POOL_SIZE", DEFAULT_POOL_SIZE)
  @pool_timeout = positive_float_setting("pool_timeout", "PGBOUNCERHERO_POOL_TIMEOUT", DEFAULT_POOL_TIMEOUT)
  @pool_monitor = Monitor.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/pgbouncerhero/database.rb', line 11

def config
  @config
end

#groupObject (readonly)

Returns the value of attribute group.



11
12
13
# File 'lib/pgbouncerhero/database.rb', line 11

def group
  @group
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/pgbouncerhero/database.rb', line 11

def id
  @id
end

#pool_sizeObject (readonly)

Returns the value of attribute pool_size.



11
12
13
# File 'lib/pgbouncerhero/database.rb', line 11

def pool_size
  @pool_size
end

#pool_timeoutObject (readonly)

Returns the value of attribute pool_timeout.



11
12
13
# File 'lib/pgbouncerhero/database.rb', line 11

def pool_timeout
  @pool_timeout
end

Instance Method Details

#connectionObject



27
28
29
30
# File 'lib/pgbouncerhero/database.rb', line 27

def connection
  proxy = connection_proxy
  proxy if with_connection { true }
end

#dbnameObject



72
73
74
# File 'lib/pgbouncerhero/database.rb', line 72

def dbname
  @url.path[1..-1] if @url
end

#disconnect!Object



42
43
44
45
46
47
48
49
50
# File 'lib/pgbouncerhero/database.rb', line 42

def disconnect!
  pool = @pool_monitor.synchronize do
    current_pool = @connection_pool
    @connection_pool = nil
    @connection_proxy = nil
    current_pool
  end
  pool&.shutdown(&:disconnect!)
end

#hostObject



52
53
54
# File 'lib/pgbouncerhero/database.rb', line 52

def host
  @url.host if @url
end

#nameObject



23
24
25
# File 'lib/pgbouncerhero/database.rb', line 23

def name
  @name ||= id.to_s
end

#passwordObject



64
65
66
# File 'lib/pgbouncerhero/database.rb', line 64

def password
  @url.password if @url
end

#portObject



56
57
58
# File 'lib/pgbouncerhero/database.rb', line 56

def port
  @url.port if @url
end

#read_only?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/pgbouncerhero/database.rb', line 68

def read_only?
  PgBouncerHero.read_only_for?(self)
end

#userObject



60
61
62
# File 'lib/pgbouncerhero/database.rb', line 60

def user
  @url.user if @url
end

#with_connectionObject



32
33
34
35
36
37
38
39
40
# File 'lib/pgbouncerhero/database.rb', line 32

def with_connection
  pool = connection_pool
  pool.with do |managed_connection|
    managed_connection.with_connection { |raw_connection| yield raw_connection }
  end
rescue ConnectionPool::TimeoutError => e
  Rails.logger.error("[PGBouncerHero] #{name} connection pool timed out after #{pool_timeout}s: #{e.message}")
  nil
end