-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
pool_config.rb
73 lines (56 loc) · 1.54 KB
/
pool_config.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true
module ActiveRecord
module ConnectionAdapters
class PoolConfig # :nodoc:
include Mutex_m
attr_reader :db_config, :connection_class, :role, :shard
attr_accessor :schema_cache
INSTANCES = ObjectSpace::WeakMap.new
private_constant :INSTANCES
class << self
def discard_pools!
INSTANCES.each_key(&:discard_pool!)
end
end
def initialize(connection_class, db_config, role, shard)
super()
@connection_class = connection_class
@db_config = db_config
@role = role
@shard = shard
@pool = nil
INSTANCES[self] = self
end
def connection_specification_name
if connection_class.primary_class?
"ActiveRecord::Base"
else
connection_class.name
end
end
def disconnect!
ActiveSupport::ForkTracker.check!
return unless @pool
synchronize do
return unless @pool
@pool.automatic_reconnect = false
@pool.disconnect!
end
nil
end
def pool
ActiveSupport::ForkTracker.check!
@pool || synchronize { @pool ||= ConnectionAdapters::ConnectionPool.new(self) }
end
def discard_pool!
return unless @pool
synchronize do
return unless @pool
@pool.discard!
@pool = nil
end
end
end
end
end
ActiveSupport::ForkTracker.after_fork { ActiveRecord::ConnectionAdapters::PoolConfig.discard_pools! }