From b2bd3053e2bfec5173b8a42658e498570ce8ebf5 Mon Sep 17 00:00:00 2001 From: Ian Oxley Date: Thu, 11 Mar 2021 19:49:52 +0000 Subject: [PATCH 1/3] Add synchronize blocks to #check_out and #check_in Create a mutex, and use `#synchronize` in the `#check_out` and `#check_in` methods. --- lib/mysql_framework/connector.rb | 37 +++++++++++++--------- spec/lib/mysql_framework/connector_spec.rb | 12 +++++++ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/lib/mysql_framework/connector.rb b/lib/mysql_framework/connector.rb index d97afeb..564ed24 100644 --- a/lib/mysql_framework/connector.rb +++ b/lib/mysql_framework/connector.rb @@ -4,6 +4,7 @@ module MysqlFramework class Connector def initialize(options = {}) @options = default_options.merge(options) + @mutex = Mutex.new Mysql2::Client.default_query_options.merge!(symbolize_keys: true, cast_booleans: true) end @@ -38,31 +39,37 @@ def connections # This method is called to fetch a client from the connection pool. def check_out - return new_client unless connection_pool_enabled? + @mutex.synchronize do + begin + return new_client unless connection_pool_enabled? - client = @connection_pool.pop(true) + client = @connection_pool.pop(true) - client.ping if @options[:reconnect] + client.ping if @options[:reconnect] - client - rescue ThreadError - if @created_connections < max_pool_size - client = new_client - @created_connections += 1 - return client - end + client + rescue ThreadError + if @created_connections < max_pool_size + client = new_client + @created_connections += 1 + return client + end - MysqlFramework.logger.error { "[#{self.class}] - Database connection pool depleted." } + MysqlFramework.logger.error { "[#{self.class}] - Database connection pool depleted." } - raise 'Database connection pool depleted.' + raise 'Database connection pool depleted.' + end + end end # This method is called to check a client back in to the connection when no longer needed. def check_in(client) - return client&.close unless connection_pool_enabled? + @mutex.synchronize do + return client&.close unless connection_pool_enabled? - client = new_client if client.nil? || client.closed? - @connection_pool.push(client) + client = new_client if client.nil? || client.closed? + @connection_pool.push(client) + end end # This method is called to use a client from the connection pool. diff --git a/spec/lib/mysql_framework/connector_spec.rb b/spec/lib/mysql_framework/connector_spec.rb index d388dd4..f50c147 100644 --- a/spec/lib/mysql_framework/connector_spec.rb +++ b/spec/lib/mysql_framework/connector_spec.rb @@ -118,6 +118,12 @@ end describe '#check_out' do + it 'calls synchronize on the mutex' do + expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize) + + subject.check_out + end + context 'when connection pooling is enabled' do context 'when there are available connections' do before do @@ -203,6 +209,12 @@ end describe '#check_in' do + it 'calls synchronize on the mutex' do + expect(subject.instance_variable_get(:@mutex)).to receive(:synchronize) + + subject.check_out + end + context 'when connection pooling is enabled' do it 'returns the provided client to the connection pool' do expect(subject.connections).to receive(:push).with(client) From 6524ef5f1e7daf4839dfd8b3df2ec4e1296a05ab Mon Sep 17 00:00:00 2001 From: Ian Oxley Date: Thu, 11 Mar 2021 19:51:11 +0000 Subject: [PATCH 2/3] Bump the version number to 2.1.4 --- lib/mysql_framework/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mysql_framework/version.rb b/lib/mysql_framework/version.rb index de8e55a..89fcc3f 100644 --- a/lib/mysql_framework/version.rb +++ b/lib/mysql_framework/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module MysqlFramework - VERSION = '2.1.3' + VERSION = '2.1.4' end From c7983fe7ce588aed1905922df77727ce75725a17 Mon Sep 17 00:00:00 2001 From: Ian Oxley Date: Fri, 12 Mar 2021 09:49:01 +0000 Subject: [PATCH 3/3] Reduce cognitive complexity of #check_in Reduce cognitive complexity to fix the CI build. --- lib/mysql_framework/connector.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mysql_framework/connector.rb b/lib/mysql_framework/connector.rb index 564ed24..d3ac319 100644 --- a/lib/mysql_framework/connector.rb +++ b/lib/mysql_framework/connector.rb @@ -26,7 +26,7 @@ def dispose until @connection_pool.empty? conn = @connection_pool.pop(true) - conn.close + conn&.close end @connection_pool = nil @@ -67,7 +67,7 @@ def check_in(client) @mutex.synchronize do return client&.close unless connection_pool_enabled? - client = new_client if client.nil? || client.closed? + client = new_client if client&.closed? @connection_pool.push(client) end end