mperham / connection_pool

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

Ensure pool size is integer #107

Closed ncavig closed 6 years ago

ncavig commented 6 years ago

We ran in to an issue with using a string value as the connection pool size and it not behaving as we expected or throw an error. The issue is that we have an environment variable dictate the connection pool size, which is a string. After inspection we realized new connections were being returned each time and no obvious error or warning that our setting was incorrect.

To reproduce,

pool = ConnectionPool.new(size: '8') do
  Object.new
end

9.times { Thread.new { puts pool.checkout.object_id } }
# will output 9 different object_ids and thus unique objects

The expected behavior would be a Timeout::Error because all the connections should be checked out. You can also use ConnectionPool#available which will throw an error.

I'm not a fan of introducing code that could break existing implementations, but it seems to me that other people might be using connection pooling, and similarly passing in a string value (perhaps also using environment variables as we do). I think this is a nice noninvasive solution to enforcing an integer size for the connection pool object.