1
0
Fork 0
mirror of https://github.com/mperham/connection_pool synced 2023-03-27 23:22:21 -04:00

Deprecate #with_connection in favor of #with.

This commit is contained in:
Damian Janowski 2012-12-18 23:33:59 -03:00
parent 42614fa8a3
commit ae182c844c
3 changed files with 18 additions and 10 deletions

View file

@ -33,14 +33,14 @@ Create a pool of objects to share amongst the fibers or threads in your Ruby app
Then use the pool in your application: Then use the pool in your application:
``` ruby ``` ruby
@memcached.with_connection do |dalli| @memcached.with do |dalli|
dalli.get('some-count') dalli.get('some-count')
end end
``` ```
If all the objects in the connection pool are in use, `with_connection` will block If all the objects in the connection pool are in use, `with` will block
until one becomes available. If no object is available within `:timeout` seconds, until one becomes available. If no object is available within `:timeout` seconds,
`with_connection` will raise a `Timeout::Error`. `with` will raise a `Timeout::Error`.
You can use `ConnectionPool::Wrapper` to wrap a single global connection, making You can use `ConnectionPool::Wrapper` to wrap a single global connection, making
it easier to port your connection code over time: it easier to port your connection code over time:
@ -54,10 +54,10 @@ $redis.smembers('foo')
The Wrapper uses `method_missing` to checkout a connection, run the The Wrapper uses `method_missing` to checkout a connection, run the
requested method and then immediately check the connection back into the requested method and then immediately check the connection back into the
pool. It's **not** high-performance so you'll want to port your pool. It's **not** high-performance so you'll want to port your
performance sensitive code to use `with_connection` as soon as possible. performance sensitive code to use `with` as soon as possible.
``` ruby ``` ruby
$redis.with_connection do |conn| $redis.with do |conn|
conn.sadd('foo', 1) conn.sadd('foo', 1)
conn.smembers('foo') conn.smembers('foo')
end end

View file

@ -51,7 +51,11 @@ class ConnectionPool
checkin checkin
end end
end end
alias :with_connection :with
def with_connection(&block)
warn("ConnectionPool#with_connection is deprecated and will be removed in version 1.0. Upgrade your code to use ConnectionPool#with instead. (in #{caller[0]})")
with(&block)
end
def checkout def checkout
stack = ::Thread.current[@key] ||= [] stack = ::Thread.current[@key] ||= []
@ -76,7 +80,7 @@ class ConnectionPool
end end
class Wrapper < ::BasicObject class Wrapper < ::BasicObject
METHODS = [:with, :with_connection] METHODS = [:with]
def initialize(options = {}, &block) def initialize(options = {}, &block)
@pool = ::ConnectionPool.new(options, &block) @pool = ::ConnectionPool.new(options, &block)
@ -87,7 +91,11 @@ class ConnectionPool
ensure ensure
@pool.checkin @pool.checkin
end end
alias :with_connection :with
def with_connection(&block)
warn("ConnectionPool::Wrapper#with_connection is deprecated and will be removed in version 1.0. Upgrade your code to use ConnectionPool::Wrapper#with instead. (in #{caller[0]})")
with(&block)
end
def respond_to?(id, *args) def respond_to?(id, *args)
METHODS.include?(id) || @pool.with { |c| c.respond_to?(id, *args) } METHODS.include?(id) || @pool.with { |c| c.respond_to?(id, *args) }

View file

@ -34,7 +34,7 @@ class TestConnectionPool < MiniTest::Unit::TestCase
threads = [] threads = []
15.times do 15.times do
threads << Thread.new do threads << Thread.new do
pool.with_connection do |net| pool.with do |net|
net.do_something net.do_something
end end
end end
@ -84,7 +84,7 @@ class TestConnectionPool < MiniTest::Unit::TestCase
def test_return_value def test_return_value
pool = ConnectionPool.new(:timeout => 0.1, :size => 1) { NetworkConnection.new } pool = ConnectionPool.new(:timeout => 0.1, :size => 1) { NetworkConnection.new }
result = pool.with_connection do |net| result = pool.with do |net|
net.fast net.fast
end end
assert_equal 1, result assert_equal 1, result