connection_pool 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f55833e24bd0a92578d10d974f63342be9abd52ed1736d0821feb0f48f05cabe
4
- data.tar.gz: cb5ad1fa9d0e79ec9eef583e6a8771f1e0208d5c0daa52d31a75523e3ccf3da1
3
+ metadata.gz: e87682a6e57e8b0214de9c0713a7466a2b06399dc52d700fbf182cdf9e5b6606
4
+ data.tar.gz: d12337513b62d4677663403c512afad165275bdf848987182ce03637a1de6482
5
5
  SHA512:
6
- metadata.gz: 37b6ee51a8563cd84846ac6647507103ff5a689a28e0a4daf203fdbfac5d7bb09a1416e7eaa71f674522ca9062a05557146182008af016130cbf7eda02eee21d
7
- data.tar.gz: bba4be5b638f77942e9978328b7a247f5638c4860c263dc58277d39a4239bf3520bfebf14547e3cc8436e85c1be1b88a24246f113bc4038f699229ce41890e04
6
+ metadata.gz: eca2c1f8ebe52039f00df70ddfe5525cc0408acd8ae9849a8d1412bec670e7d3a05d3609db933e3e906cd83307d39f754dd5f1d7b48b50b847090b5bf485b55a
7
+ data.tar.gz: fc9a62b4b0ba5a406543e8f6399bd566f2f31c6c9a6ce2772e3550c1c2a75945c8a34defeda10c83e23cbfe5fe50d0e8861f57ea23935d7c97540aa97b04a635
data/Changes.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # connection_pool Changelog
2
2
 
3
+ 2.4.0
4
+ ------
5
+
6
+ - Automatically drop all connections after fork [#166]
7
+
3
8
  2.3.0
4
9
  ------
5
10
 
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
 
@@ -1,3 +1,3 @@
1
1
  class ConnectionPool
2
- VERSION = "2.3.0"
2
+ VERSION = "2.4.0"
3
3
  end
@@ -44,6 +44,47 @@ class ConnectionPool
44
44
  Wrapper.new(options, &block)
45
45
  end
46
46
 
47
+ if Process.respond_to?(:fork)
48
+ INSTANCES = ObjectSpace::WeakMap.new
49
+ private_constant :INSTANCES
50
+
51
+ def self.after_fork
52
+ INSTANCES.values.each do |pool|
53
+ # We're on after fork, so we know all other threads are dead.
54
+ # All we need to do is to ensure the main thread doesn't have a
55
+ # checked out connection
56
+ pool.checkin(force: true)
57
+
58
+ pool.reload do |connection|
59
+ # Unfortunately we don't know what method to call to close the connection,
60
+ # so we try the most common one.
61
+ connection.close if connection.respond_to?(:close)
62
+ end
63
+ end
64
+ nil
65
+ end
66
+
67
+ if ::Process.respond_to?(:_fork) # MRI 3.1+
68
+ module ForkTracker
69
+ def _fork
70
+ pid = super
71
+ if pid == 0
72
+ ConnectionPool.after_fork
73
+ end
74
+ pid
75
+ end
76
+ end
77
+ Process.singleton_class.prepend(ForkTracker)
78
+ end
79
+ else
80
+ INSTANCES = nil
81
+ private_constant :INSTANCES
82
+
83
+ def self.after_fork
84
+ # noop
85
+ end
86
+ end
87
+
47
88
  def initialize(options = {}, &block)
48
89
  raise ArgumentError, "Connection pool requires a block" unless block
49
90
 
@@ -55,6 +96,7 @@ class ConnectionPool
55
96
  @available = TimedStack.new(@size, &block)
56
97
  @key = :"pool-#{@available.object_id}"
57
98
  @key_count = :"pool-#{@available.object_id}-count"
99
+ INSTANCES[self] = self if INSTANCES
58
100
  end
59
101
 
60
102
  def with(options = {})
@@ -81,16 +123,16 @@ class ConnectionPool
81
123
  end
82
124
  end
83
125
 
84
- def checkin
126
+ def checkin(force: false)
85
127
  if ::Thread.current[@key]
86
- if ::Thread.current[@key_count] == 1
128
+ if ::Thread.current[@key_count] == 1 || force
87
129
  @available.push(::Thread.current[@key])
88
130
  ::Thread.current[@key] = nil
89
131
  ::Thread.current[@key_count] = nil
90
132
  else
91
133
  ::Thread.current[@key_count] -= 1
92
134
  end
93
- else
135
+ elsif !force
94
136
  raise ConnectionPool::Error, "no connections are checked out"
95
137
  end
96
138
 
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.0
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-03-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  requirements: []
91
- rubygems_version: 3.2.32
91
+ rubygems_version: 3.4.7
92
92
  signing_key:
93
93
  specification_version: 4
94
94
  summary: Generic connection pool for Ruby