mperham / connection_pool

Generic connection pooling for Ruby
MIT License
1.63k stars 143 forks source link

define method when method missing #61

Closed davidqhr closed 10 years ago

davidqhr commented 10 years ago

We use connection_pool in our server.

$redis = ConnectionPool::Wrapper.new(size: 5, timeout: 3) { Redis.connect }
$redis.sadd('foo', 1)

In fact, the method named sadd is one of Redis.connect`s methods which is delegated by ConnectionPool::Wrappe#method_missing.

It's more efficient to define the method when method missing.

mperham commented 10 years ago

The inefficiency is there for a reason: Wrapper is a temporary scaffolding to allow you to port your code to use a real thread pool. It's not meant to be permanent.

davidqhr commented 10 years ago

Click the wrong button...

It is painful to modify all the code of redis connection.

$redis.zincrby(ChatLog::distinct_user_key(Date.today), 1, sender_id)
$redis.zincrby(ChatLog::message_count_key, 1, Date.today.strftime('%Y-%m-%d')) 

to

$redis.with do |conn|
  conn.zincrby(ChatLog::distinct_user_key(Date.today), 1, sender_id)
  conn.zincrby(ChatLog::message_count_key, 1, Date.today.strftime('%Y-%m-%d')) 
end

Since it provides a wrapper which is a sample way to use the gem without modifing a lot of code. why not an efficient one?

mperham commented 10 years ago

Again, the Wrapper is there to help port your code. It's not meant to be a high-performance replacement / stand-in for the real ConnectionPool API and is documented as such. I want there to be a cost so that people have a reason to use it only temporarily.

davidqhr commented 10 years ago

ok, I got it.