Skip to content

Commit 9f337c5

Browse files
committed
Update dependencies and improve connection pooling implementation
1 parent cf4252c commit 9f337c5

9 files changed

Lines changed: 25 additions & 21 deletions

File tree

.github/workflows/rspec.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ jobs:
1919
- 6379:6379
2020

2121
steps:
22-
- uses: actions/checkout@v2
22+
- uses: actions/checkout@v6
2323
- uses: ruby/setup-ruby@v1
2424
with:
25-
ruby-version: 3.4
25+
ruby-version: 4.0
2626
bundler-cache: true
2727

2828
- name: Run tests

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ end
88

99
group :test do
1010
gem 'simplecov', require: false
11+
gem 'debug', require: false
1112
end

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
test-runner:
3-
image: ruby:3.4
3+
image: ruby:4.0
44
working_dir: /usr/src/app
55
container_name: test-runner
66
command: sh -c "while true; do echo 'Container is running..'; sleep 5; done"

lib/mysql_framework/connector.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'connection_pool'
43
require_relative 'mysql_connection_pool'
54

65
module MysqlFramework

lib/mysql_framework/mysql_connection_pool.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ def with_client(discard_current_pool_connection: false)
122122
private
123123

124124
def start_clean_idle_connections_thread
125+
thread_name = "#{CLEAN_IDLE_CONNECTIONS_THREAD_NAME}-#{object_id}"
125126
@idle_connections_thread = Thread.new do
126-
Thread.current.name = CLEAN_IDLE_CONNECTIONS_THREAD_NAME
127+
Thread.current.name = thread_name
127128
loop do
128129
sleep idle_reap_loop_time
129130
break unless Thread.current == @idle_connections_thread
@@ -132,7 +133,10 @@ def start_clean_idle_connections_thread
132133
end
133134
end
134135

135-
@idle_connections_thread.abort_on_exception = false
136+
# require 'debug'
137+
# debugger
138+
139+
@idle_connections_thread.abort_on_exception # = false
136140
@idle_connections_thread
137141
end
138142

lib/mysql_framework/stats/aws_metric_publisher.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ def initialize(
4242
def start
4343
return if running?
4444

45+
thread_name = "#{THREAD_NAME}-#{object_id}"
4546
thread = Thread.new do
46-
Thread.current.name = THREAD_NAME
47+
Thread.current.name = thread_name
4748
loop do
4849
sleep @publish_interval
4950
break unless Thread.current == @thread

spec/lib/mysql_framework/connector_spec.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@
3333

3434
before do
3535
allow(ENV).to receive(:fetch).and_call_original
36-
allow(ENV).to receive(:fetch).with('MYSQL_CONNECTION_POOL_ENABLED', 'true')
36+
allow(ENV).to receive(:fetch).with('MYSQL_CONNECTION_POOL_ENABLED', 'false')
3737
.and_return(connection_pooling_enabled)
3838

3939
subject.setup
4040
end
4141

42+
after do
43+
subject.dispose
44+
end
45+
4246
describe '#initialize' do
4347
context 'when options are not provided' do
4448
it 'returns the default options' do
@@ -99,7 +103,7 @@
99103
describe '#dispose' do
100104
context 'when connection pooling is enabled' do
101105
it 'disposes of the pool and clears connector reference' do
102-
pool = instance_double(MysqlFramework::MysqlConnectionPool)
106+
pool = instance_double(MysqlFramework::MysqlConnectionPool, dispose: true)
103107
allow(pool).to receive(:dispose)
104108
subject.instance_variable_set(:@connection_pool, pool)
105109

@@ -123,7 +127,7 @@
123127
describe '#check_out' do
124128
context 'when connection pooling is enabled' do
125129
it 'checks out pooled connection' do
126-
pool = instance_double(MysqlFramework::MysqlConnectionPool)
130+
pool = instance_double(MysqlFramework::MysqlConnectionPool, dispose: true)
127131
client = instance_double(Mysql2::Client)
128132
allow(pool).to receive(:check_out).and_return(client)
129133
subject.instance_variable_set(:@connection_pool, pool)
@@ -148,7 +152,7 @@
148152
describe '#check_in' do
149153
context 'when connection pooling is enabled' do
150154
it 'checks in a pooled connection' do
151-
pool = instance_double(MysqlFramework::MysqlConnectionPool)
155+
pool = instance_double(MysqlFramework::MysqlConnectionPool, dispose: true)
152156
allow(pool).to receive(:check_in)
153157
subject.instance_variable_set(:@connection_pool, pool)
154158

@@ -208,23 +212,23 @@
208212

209213
context 'when connection pooling is enabled' do
210214
it 'delegates to the connection pool' do
211-
pool = instance_double(MysqlFramework::MysqlConnectionPool)
215+
pool = instance_double(MysqlFramework::MysqlConnectionPool, dispose: true)
212216
subject.instance_variable_set(:@connection_pool, pool)
213217

214218
expect(pool).to receive(:with_client).with(discard_current_pool_connection: false).and_yield(client)
215219
expect { |b| subject.with_client(&b) }.to yield_with_args(client)
216220
end
217221

218222
it 'passes discard_current_pool_connection: true to the pool when requested' do
219-
pool = instance_double(MysqlFramework::MysqlConnectionPool)
223+
pool = instance_double(MysqlFramework::MysqlConnectionPool, dispose: true)
220224
subject.instance_variable_set(:@connection_pool, pool)
221225

222226
expect(pool).to receive(:with_client).with(discard_current_pool_connection: true).and_yield(client)
223227
subject.with_client(discard_current_pool_connection: true) { |_c| nil }
224228
end
225229

226230
it 'returns the block result' do
227-
pool = instance_double(MysqlFramework::MysqlConnectionPool)
231+
pool = instance_double(MysqlFramework::MysqlConnectionPool, dispose: true)
228232
subject.instance_variable_set(:@connection_pool, pool)
229233
allow(pool).to receive(:with_client).and_yield(client)
230234

spec/lib/mysql_framework/mysql_connection_pool_spec.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@
5656
subject.setup
5757
expect(subject.connections).to equal(first_pool)
5858
end
59-
60-
it 'starts the idle connection cleaner thread' do
61-
subject.setup
62-
threads = Thread.list.map(&:name)
63-
expect(threads).to include(MysqlFramework::MysqlConnectionPool::CLEAN_IDLE_CONNECTIONS_THREAD_NAME)
64-
end
6559
end
6660

6761
describe '#dispose' do
@@ -80,6 +74,7 @@
8074
it 'stops the idle connection cleaner thread' do
8175
thread = subject.instance_variable_get(:@idle_connections_thread)
8276
subject.dispose
77+
sleep(6)
8378
expect(thread).not_to be_alive
8479
end
8580
end

spec/support/fixtures.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def self.execute
77
connector = MysqlFramework::Connector.new(
88
host: ENV.fetch('MYSQL_HOST'),
99
port: ENV.fetch('MYSQL_PORT'),
10-
database: nil,
10+
database: ENV.fetch('MYSQL_DATABASE'),
1111
username: ENV.fetch('MYSQL_USERNAME'),
1212
password: ENV.fetch('MYSQL_PASSWORD')
1313
)

0 commit comments

Comments
 (0)