Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

ConnectionPool accepts spec key 'checkout_timeout' (Backport) #7684

Merged
merged 1 commit into from

3 participants

@jrochkind

Backport of applicable parts of #6441 cb6f839, in a backwards compatible way.

Previously, ConnectionPool uses connection spec key 'wait_timeout' (default 5 seconds) to decide how long a thread can block waiting for a connection to be avail before raising.

However, mysql2 adapter uses this same 'wait_timeout' key for an entirely different value -- setting the MySQL server's own 'wait timeout', how long the server will allow a connection to be idle before closing it. Default is several hours.

Extreme differences in default shows you often do not want these two values to be the same. But if you don't want them to be default, there is no supported API to change one without changing the other to be the same, since they both use 'wait_timeout'.

So this change maeks ConnectionPool use checkout_timeout in the spec, prefering it over wait_timeout. But wait_timeout IS (unlike in master) still used if there without checkout_timeout. If you want mysql2's wait_timeout to be differnet than ConnectionPool's timeout, you can (and have to) set them both -- mysql2 will use the wait_timeout, ConnectionPool will use the checkout_timeout. But prior to this patch there was no way to do that.

The actual code change is very straightforward. But I wasn't sure how to write the test. The test is a bit wonky, if someone has a suggestion of how to organize the test better, please do share. @rafaelfranca , any interest/feedback?

@steveklabnik
Collaborator

This no longer merges cleanly, and will need a rebase.

@jrochkind

had some trouble figuring out how to get this properly rebased, but I think I've done it right. thanks @steveklabnik

@steveklabnik
Collaborator

You have! :D

@jrochkind

so, um, what would it take to get this merged into 3-2-stable (or feedback given on why it's not suitable for such)? Anything I can do?

@steveklabnik
Collaborator

Needs someone from Rails Core to review. I'm not allowed to use my merge privledges.

I'd bet either @tenderlove or @jonleighton would be good people here.

@rafaelfranca

It seems fine to me. I just want to know if @tenderlove have something against this change.

@rafaelfranca

We will need a CHANGELOG entry

@jrochkind
@rafaelfranca

I'll merge it as soon you add a CHANGELOG.

@jrochkind jrochkind ConnectionPool accepts spec key 'checkout_timeout'
Backport of #6441 cb6f839 . Old 'wait_timeout' is still supported,
but conflicts with mysql2 using that spec key for different thing.
'checkout_timeout' can now be used taking precedence for ConnectionPool
over 'wait_timeout'.
3908706
@jrochkind

done. and yeah,I can rebase again if I need to just let me know.

@rafaelfranca rafaelfranca merged commit 8800aae into from
@rafaelfranca

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 24, 2012
  1. @jrochkind

    ConnectionPool accepts spec key 'checkout_timeout'

    jrochkind authored
    Backport of #6441 cb6f839 . Old 'wait_timeout' is still supported,
    but conflicts with mysql2 using that spec key for different thing.
    'checkout_timeout' can now be used taking precedence for ConnectionPool
    over 'wait_timeout'.
This page is out of date. Refresh to see the latest.
View
6 activerecord/CHANGELOG.md
@@ -1,5 +1,11 @@
## Rails 3.2.9 (unreleased)
+* ConnectionPool recognizes checkout_timeout spec key as taking
+ precedence over legacy wait_timeout spec key, can be used to avoid
+ conflict with mysql2 use of wait_timeout. Closes #7684.
+
+ *jrochkind*
+
* Rename field_changed? to _field_changed? so that users can create a field named field
*Akira Matsuda*, backported by *Steve Klabnik*
View
12 activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -54,8 +54,11 @@ module ConnectionAdapters
# your database connection configuration:
#
# * +pool+: number indicating size of connection pool (default 5)
- # * +wait_timeout+: number of seconds to block and wait for a connection
- # before giving up and raising a timeout error (default 5 seconds).
+ # * +checkout _timeout+: number of seconds to block and wait for a
+ # connection before giving up and raising a timeout error
+ # (default 5 seconds). ('wait_timeout' supported for backwards
+ # compatibility, but conflicts with key used for different purpose
+ # by mysql2 adapter).
class ConnectionPool
include MonitorMixin
@@ -77,7 +80,10 @@ def initialize(spec)
@reserved_connections = {}
@queue = new_cond
- @timeout = spec.config[:wait_timeout] || 5
+ # 'wait_timeout', the backward-compatible key, conflicts with spec key
+ # used by mysql2 for something entirely different, checkout_timeout
+ # preferred to avoid conflict and allow independent values.
+ @timeout = spec.config[:checkout_timeout] || spec.config[:wait_timeout] || 5
# default max pool size to 5
@size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
View
22 activerecord/test/cases/connection_pool_test.rb
@@ -151,6 +151,28 @@ def test_automatic_reconnect=
def test_pool_sets_connection_visitor
assert @pool.connection.visitor.is_a?(Arel::Visitors::ToSql)
end
+
+ def test_timeout_spec_keys
+ # 'wait_timeout' is supported for backwards compat,
+ # 'checkout_timeout' is preferred to avoid conflicting
+ # with mysql2 adapters key of name 'wait_timeout' but
+ # different meaning.
+ config = ActiveRecord::Base.connection_pool.spec.config.merge(:wait_timeout => nil, :connection_timeout => nil)
+ method = ActiveRecord::Base.connection_pool.spec.adapter_method
+
+ pool = ConnectionPool.new ActiveRecord::Base::ConnectionSpecification.new(config.merge(:wait_timeout => 1), method)
+ assert_equal 1, pool.instance_variable_get(:@timeout)
+ pool.disconnect!
+
+ pool = ConnectionPool.new ActiveRecord::Base::ConnectionSpecification.new(config.merge(:checkout_timeout => 1), method)
+ assert_equal 1, pool.instance_variable_get(:@timeout)
+ pool.disconnect!
+
+ pool = ConnectionPool.new ActiveRecord::Base::ConnectionSpecification.new(config.merge(:wait_timeout => 6000, :checkout_timeout => 1), method)
+ assert_equal 1, pool.instance_variable_get(:@timeout)
+ pool.disconnect!
+ end
+
end
end
end
View
4 activerecord/test/cases/pooled_connections_test.rb
@@ -17,7 +17,7 @@ def teardown
end
def checkout_connections
- ActiveRecord::Base.establish_connection(@connection.merge({:pool => 2, :wait_timeout => 0.3}))
+ ActiveRecord::Base.establish_connection(@connection.merge({:pool => 2, :checkout_timeout => 0.3}))
@connections = []
@timed_out = 0
@@ -42,7 +42,7 @@ def test_pooled_connection_checkout
end
def checkout_checkin_connections(pool_size, threads)
- ActiveRecord::Base.establish_connection(@connection.merge({:pool => pool_size, :wait_timeout => 0.5}))
+ ActiveRecord::Base.establish_connection(@connection.merge({:pool => pool_size, :checkout_timeout => 0.5}))
@connection_count = 0
@timed_out = 0
threads.times do
Something went wrong with that request. Please try again.