mysql_framework 2.1.3 → 2.1.4
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 +4 -4
- data/lib/mysql_framework/connector.rb +23 -16
- data/lib/mysql_framework/version.rb +1 -1
- data/spec/lib/mysql_framework/connector_spec.rb +12 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87c4479177595f0b81fd81122c23473b63416ecf3a6427620c3cef4ba25d3e58
|
4
|
+
data.tar.gz: c668c3276dfe1120f7be7f8d30238bd1b8123c5559221ec175a1d3d844cc8e80
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc44ac34b35b2c615ce87b8a1a627edc5d9f97c50f14ba3da1cae08e4089b0dcea405c495e48efbba95c852f2418334b3e1c32d029ae3fc4ec09f5d4028cf68d
|
7
|
+
data.tar.gz: 2eba7935da06a55d525b96126655f6a30415a62cf31225f184a0a305571f4e2f83a292a7214c446b63c01cbdd005b652bf30a2796bcb2e24f8644e8a8182830c
|
@@ -4,6 +4,7 @@ module MysqlFramework
|
|
4
4
|
class Connector
|
5
5
|
def initialize(options = {})
|
6
6
|
@options = default_options.merge(options)
|
7
|
+
@mutex = Mutex.new
|
7
8
|
|
8
9
|
Mysql2::Client.default_query_options.merge!(symbolize_keys: true, cast_booleans: true)
|
9
10
|
end
|
@@ -25,7 +26,7 @@ module MysqlFramework
|
|
25
26
|
|
26
27
|
until @connection_pool.empty?
|
27
28
|
conn = @connection_pool.pop(true)
|
28
|
-
conn
|
29
|
+
conn&.close
|
29
30
|
end
|
30
31
|
|
31
32
|
@connection_pool = nil
|
@@ -38,31 +39,37 @@ module MysqlFramework
|
|
38
39
|
|
39
40
|
# This method is called to fetch a client from the connection pool.
|
40
41
|
def check_out
|
41
|
-
|
42
|
+
@mutex.synchronize do
|
43
|
+
begin
|
44
|
+
return new_client unless connection_pool_enabled?
|
42
45
|
|
43
|
-
|
46
|
+
client = @connection_pool.pop(true)
|
44
47
|
|
45
|
-
|
48
|
+
client.ping if @options[:reconnect]
|
46
49
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
50
|
+
client
|
51
|
+
rescue ThreadError
|
52
|
+
if @created_connections < max_pool_size
|
53
|
+
client = new_client
|
54
|
+
@created_connections += 1
|
55
|
+
return client
|
56
|
+
end
|
54
57
|
|
55
|
-
|
58
|
+
MysqlFramework.logger.error { "[#{self.class}] - Database connection pool depleted." }
|
56
59
|
|
57
|
-
|
60
|
+
raise 'Database connection pool depleted.'
|
61
|
+
end
|
62
|
+
end
|
58
63
|
end
|
59
64
|
|
60
65
|
# This method is called to check a client back in to the connection when no longer needed.
|
61
66
|
def check_in(client)
|
62
|
-
|
67
|
+
@mutex.synchronize do
|
68
|
+
return client&.close unless connection_pool_enabled?
|
63
69
|
|
64
|
-
|
65
|
-
|
70
|
+
client = new_client if client&.closed?
|
71
|
+
@connection_pool.push(client)
|
72
|
+
end
|
66
73
|
end
|
67
74
|
|
68
75
|
# This method is called to use a client from the connection pool.
|
@@ -118,6 +118,12 @@ describe MysqlFramework::Connector do
|
|
118
118
|
end
|
119
119
|
|
120
120
|
describe '#check_out' do
|
121
|
+
it 'calls synchronize on the mutex' do
|
122
|
+
expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize)
|
123
|
+
|
124
|
+
subject.check_out
|
125
|
+
end
|
126
|
+
|
121
127
|
context 'when connection pooling is enabled' do
|
122
128
|
context 'when there are available connections' do
|
123
129
|
before do
|
@@ -203,6 +209,12 @@ describe MysqlFramework::Connector do
|
|
203
209
|
end
|
204
210
|
|
205
211
|
describe '#check_in' do
|
212
|
+
it 'calls synchronize on the mutex' do
|
213
|
+
expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize)
|
214
|
+
|
215
|
+
subject.check_out
|
216
|
+
end
|
217
|
+
|
206
218
|
context 'when connection pooling is enabled' do
|
207
219
|
it 'returns the provided client to the connection pool' do
|
208
220
|
expect(subject.connections).to receive(:push).with(client)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mysql_framework
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sage
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-03-
|
11
|
+
date: 2021-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|