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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38653f6e3d1d849566574e58eb8386b4c32890370404cb3eade583b0070f5041
4
- data.tar.gz: 5534eca9f224d13580cfd09f4e2478208857e5b353c9a664ed7b772bf8a088e1
3
+ metadata.gz: 87c4479177595f0b81fd81122c23473b63416ecf3a6427620c3cef4ba25d3e58
4
+ data.tar.gz: c668c3276dfe1120f7be7f8d30238bd1b8123c5559221ec175a1d3d844cc8e80
5
5
  SHA512:
6
- metadata.gz: aa4e726ac90ac8b1ecd8ca20a64b4c33e81d003c395b49c340f712df500fc7879956f5479f9c23e53fe20ca947d41d2798ad42732c596a03ab219129f02a7dbd
7
- data.tar.gz: ba2080bb5f7595b82996a48a0316ed70cf7560b5687abb9d5c9768c73f5e2b296e42378aa9139c80e2d5f705a8fa7bdb08167a740c019a6d5d0a0825436873e7
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.close
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
- return new_client unless connection_pool_enabled?
42
+ @mutex.synchronize do
43
+ begin
44
+ return new_client unless connection_pool_enabled?
42
45
 
43
- client = @connection_pool.pop(true)
46
+ client = @connection_pool.pop(true)
44
47
 
45
- client.ping if @options[:reconnect]
48
+ client.ping if @options[:reconnect]
46
49
 
47
- client
48
- rescue ThreadError
49
- if @created_connections < max_pool_size
50
- client = new_client
51
- @created_connections += 1
52
- return client
53
- end
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
- MysqlFramework.logger.error { "[#{self.class}] - Database connection pool depleted." }
58
+ MysqlFramework.logger.error { "[#{self.class}] - Database connection pool depleted." }
56
59
 
57
- raise 'Database connection pool depleted.'
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
- return client&.close unless connection_pool_enabled?
67
+ @mutex.synchronize do
68
+ return client&.close unless connection_pool_enabled?
63
69
 
64
- client = new_client if client.nil? || client.closed?
65
- @connection_pool.push(client)
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.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MysqlFramework
4
- VERSION = '2.1.3'
4
+ VERSION = '2.1.4'
5
5
  end
@@ -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.3
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 00:00:00.000000000 Z
11
+ date: 2021-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake