connection_pool 2.3.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f55833e24bd0a92578d10d974f63342be9abd52ed1736d0821feb0f48f05cabe
4
- data.tar.gz: cb5ad1fa9d0e79ec9eef583e6a8771f1e0208d5c0daa52d31a75523e3ccf3da1
3
+ metadata.gz: ea0776fcb09a3cc48ef4ca03774399e20b09e51039d0c47c1e4cb3bac621c52b
4
+ data.tar.gz: b955d6b4e984259f20ae8cf6414f59692f9a51848424231363643e0c16dd2a3f
5
5
  SHA512:
6
- metadata.gz: 37b6ee51a8563cd84846ac6647507103ff5a689a28e0a4daf203fdbfac5d7bb09a1416e7eaa71f674522ca9062a05557146182008af016130cbf7eda02eee21d
7
- data.tar.gz: bba4be5b638f77942e9978328b7a247f5638c4860c263dc58277d39a4239bf3520bfebf14547e3cc8436e85c1be1b88a24246f113bc4038f699229ce41890e04
6
+ metadata.gz: bf57d8b5547502d91f5550ca6ea0be16905604c90e61efb6741e5ec3ce607c7a65f0b31e1673c96c60a06a2f64f5239cab6e94d3a50095fb822ea9b1c1bb2f0a
7
+ data.tar.gz: 4b42aa5aa67b0e45bbbc8a9f29ca3a969efd8ade3b6dfca6cff082f526ec65a2a2e5c8fa17f512d33470b82535af8b675fc80903ccd75db26748e9845dd9a612
data/Changes.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # connection_pool Changelog
2
2
 
3
+ 2.4.1
4
+ ------
5
+
6
+ - New `auto_reload_after_fork` config option to disable auto-drop [#177, shayonj]
7
+
8
+ 2.4.0
9
+ ------
10
+
11
+ - Automatically drop all connections after fork [#166]
12
+
3
13
  2.3.0
4
14
  ------
5
15
 
data/README.md CHANGED
@@ -48,7 +48,7 @@ end
48
48
 
49
49
  This will only modify the resource-get timeout for this particular
50
50
  invocation.
51
- This is useful if you want to fail-fast on certain non critical
51
+ This is useful if you want to fail-fast on certain non-critical
52
52
  sections when a resource is not available, or conversely if you are comfortable blocking longer on a particular resource.
53
53
  This is not implemented in the `ConnectionPool::Wrapper` class.
54
54
 
@@ -19,4 +19,6 @@ Gem::Specification.new do |s|
19
19
  s.add_development_dependency "minitest", ">= 5.0.0"
20
20
  s.add_development_dependency "rake"
21
21
  s.required_ruby_version = ">= 2.5.0"
22
+
23
+ s.metadata = {"changelog_uri" => "https://github.com/mperham/connection_pool/blob/main/Changes.md", "rubygems_mfa_required" => "true"}
22
24
  end
@@ -1,3 +1,3 @@
1
1
  class ConnectionPool
2
- VERSION = "2.3.0"
2
+ VERSION = "2.4.1"
3
3
  end
@@ -36,14 +36,57 @@ end
36
36
  # Accepts the following options:
37
37
  # - :size - number of connections to pool, defaults to 5
38
38
  # - :timeout - amount of time to wait for a connection if none currently available, defaults to 5 seconds
39
+ # - :auto_reload_after_fork - automatically drop all connections after fork, defaults to true
39
40
  #
40
41
  class ConnectionPool
41
- DEFAULTS = {size: 5, timeout: 5}
42
+ DEFAULTS = {size: 5, timeout: 5, auto_reload_after_fork: true}
42
43
 
43
44
  def self.wrap(options, &block)
44
45
  Wrapper.new(options, &block)
45
46
  end
46
47
 
48
+ if Process.respond_to?(:fork)
49
+ INSTANCES = ObjectSpace::WeakMap.new
50
+ private_constant :INSTANCES
51
+
52
+ def self.after_fork
53
+ INSTANCES.values.each do |pool|
54
+ next unless pool.auto_reload_after_fork
55
+
56
+ # We're on after fork, so we know all other threads are dead.
57
+ # All we need to do is to ensure the main thread doesn't have a
58
+ # checked out connection
59
+ pool.checkin(force: true)
60
+ pool.reload do |connection|
61
+ # Unfortunately we don't know what method to call to close the connection,
62
+ # so we try the most common one.
63
+ connection.close if connection.respond_to?(:close)
64
+ end
65
+ end
66
+ nil
67
+ end
68
+
69
+ if ::Process.respond_to?(:_fork) # MRI 3.1+
70
+ module ForkTracker
71
+ def _fork
72
+ pid = super
73
+ if pid == 0
74
+ ConnectionPool.after_fork
75
+ end
76
+ pid
77
+ end
78
+ end
79
+ Process.singleton_class.prepend(ForkTracker)
80
+ end
81
+ else
82
+ INSTANCES = nil
83
+ private_constant :INSTANCES
84
+
85
+ def self.after_fork
86
+ # noop
87
+ end
88
+ end
89
+
47
90
  def initialize(options = {}, &block)
48
91
  raise ArgumentError, "Connection pool requires a block" unless block
49
92
 
@@ -51,10 +94,12 @@ class ConnectionPool
51
94
 
52
95
  @size = Integer(options.fetch(:size))
53
96
  @timeout = options.fetch(:timeout)
97
+ @auto_reload_after_fork = options.fetch(:auto_reload_after_fork)
54
98
 
55
99
  @available = TimedStack.new(@size, &block)
56
100
  @key = :"pool-#{@available.object_id}"
57
101
  @key_count = :"pool-#{@available.object_id}-count"
102
+ INSTANCES[self] = self if INSTANCES
58
103
  end
59
104
 
60
105
  def with(options = {})
@@ -81,16 +126,16 @@ class ConnectionPool
81
126
  end
82
127
  end
83
128
 
84
- def checkin
129
+ def checkin(force: false)
85
130
  if ::Thread.current[@key]
86
- if ::Thread.current[@key_count] == 1
131
+ if ::Thread.current[@key_count] == 1 || force
87
132
  @available.push(::Thread.current[@key])
88
133
  ::Thread.current[@key] = nil
89
134
  ::Thread.current[@key_count] = nil
90
135
  else
91
136
  ::Thread.current[@key_count] -= 1
92
137
  end
93
- else
138
+ elsif !force
94
139
  raise ConnectionPool::Error, "no connections are checked out"
95
140
  end
96
141
 
@@ -117,6 +162,8 @@ class ConnectionPool
117
162
 
118
163
  # Size of this connection pool
119
164
  attr_reader :size
165
+ # Automatically drop all connections after fork
166
+ attr_reader :auto_reload_after_fork
120
167
 
121
168
  # Number of pool entries available for checkout at this instant.
122
169
  def available
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: connection_pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Perham
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-09-15 00:00:00.000000000 Z
12
+ date: 2023-05-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -72,7 +72,9 @@ files:
72
72
  homepage: https://github.com/mperham/connection_pool
73
73
  licenses:
74
74
  - MIT
75
- metadata: {}
75
+ metadata:
76
+ changelog_uri: https://github.com/mperham/connection_pool/blob/main/Changes.md
77
+ rubygems_mfa_required: 'true'
76
78
  post_install_message:
77
79
  rdoc_options: []
78
80
  require_paths:
@@ -88,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
90
  - !ruby/object:Gem::Version
89
91
  version: '0'
90
92
  requirements: []
91
- rubygems_version: 3.2.32
93
+ rubygems_version: 3.4.7
92
94
  signing_key:
93
95
  specification_version: 4
94
96
  summary: Generic connection pool for Ruby