From 8268b44f3fe2ac6ff14d52855dd8a13eabe41907 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Thu, 5 Sep 2024 15:10:27 -0700 Subject: [PATCH] Switch default connection pool to timed_queue on Ruby 3.2+ The timed_queue connection pool was already the default on Ruby 3.4+. However, recent discussions have shown that it is much better than the threaded connection pool, at least in cases where a sampling profiler is used. This should be a drop-in replacement for most applications. However, the timed_queue connection pool does not support the available_connections and allocated accessors that the threaded connection pool supports. Those accessors are planned for removal in Sequel 6, but are not formally deprecated yet. Applications that are using those accessors should either stop, or specify pool_class: :threaded to continue using the threaded connection pool. --- CHANGELOG | 4 ++++ lib/sequel/connection_pool.rb | 4 ++-- spec/core/connection_pool_spec.rb | 8 ++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index cf1f62e1f..cf56885be 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +=== master + +* Switch default connection pool to timed_queue on Ruby 3.2+ (jeremyevans) + === 5.84.0 (2024-09-01) * Support creating VIRTUAL tables on SQLite via the create_table :using option (jeremyevans) diff --git a/lib/sequel/connection_pool.rb b/lib/sequel/connection_pool.rb index 52f047518..6940eef9f 100644 --- a/lib/sequel/connection_pool.rb +++ b/lib/sequel/connection_pool.rb @@ -70,13 +70,13 @@ def connection_pool_class(opts) else pc = if opts[:single_threaded] opts[:servers] ? :sharded_single : :single - # :nocov: - elsif RUBY_VERSION >= '3.4' # SEQUEL6 or maybe earlier switch to 3.2 + elsif RUBY_VERSION >= '3.2' opts[:servers] ? :sharded_timed_queue : :timed_queue # :nocov: else opts[:servers] ? :sharded_threaded : :threaded end + # :nocov: connection_pool_class(:pool_class=>pc) end diff --git a/spec/core/connection_pool_spec.rb b/spec/core/connection_pool_spec.rb index 0ace412d6..96ab1daa7 100644 --- a/spec/core/connection_pool_spec.rb +++ b/spec/core/connection_pool_spec.rb @@ -1271,12 +1271,12 @@ def @pool.acquire(_,_=nil) raise Sequel::DatabaseDisconnectError; end Sequel::ConnectionPool.send(:get_pool, db, :single_threaded=>true, :servers=>{}).pool_type.must_equal :sharded_single end - it "should default to :single without :single_threaded or :servers" do - Sequel::ConnectionPool.send(:get_pool, db, {}).pool_type.must_equal(RUBY_VERSION >= '3.4' ? :timed_queue : :threaded) + it "should default to :timed_queue/:threaded without :single_threaded or :servers" do + Sequel::ConnectionPool.send(:get_pool, db, {}).pool_type.must_equal(RUBY_VERSION >= '3.2' ? :timed_queue : :threaded) end - it "should default to :single without :single_threaded with :servers" do - Sequel::ConnectionPool.send(:get_pool, db, :servers=>{}).pool_type.must_equal(RUBY_VERSION >= '3.4' ? :sharded_timed_queue : :sharded_threaded) + it "should default to :sharded_timed_queue/:sharded_threaded without :single_threaded with :servers" do + Sequel::ConnectionPool.send(:get_pool, db, :servers=>{}).pool_type.must_equal(RUBY_VERSION >= '3.2' ? :sharded_timed_queue : :sharded_threaded) end end unless ENV['SEQUEL_DEFAULT_CONNECTION_POOL']