Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update connection_pool to subclass BasicObject

This makes it a more effective proxy.
  • Loading branch information...
commit 3ac11a9206904eb23d556a88d4134af085f1fc48 1 parent 74ce90a
@mperham authored
Showing with 21 additions and 11 deletions.
  1. +5 −0 Changes.md
  2. +7 −1 README.md
  3. +9 −10 lib/connection_pool.rb
View
5 Changes.md
@@ -0,0 +1,5 @@
+HEAD
+--------
+
+- More precise timeouts
+- ConnectionPool now subclasses BasicObject so `method_missing` is more effective.
View
8 README.md
@@ -5,6 +5,12 @@ Generic connection pooling for Ruby.
MongoDB has its own connection pool. ActiveRecord has its own connection pool. This is a generic connection pool that can be used with anything, e.g. Redis, Dalli and other Ruby network clients.
+Requirements
+--------------
+
+connection_pool requires Ruby 1.9 because it uses BasicObject.
+
+
Install
------------
@@ -27,4 +33,4 @@ Then use the pool in your application:
Author
--------------
-Mike Perham, [@mperham](https://twitter.com/mperham), <http://mikeperham.com>
+Mike Perham, [@mperham](https://twitter.com/mperham), <http://mikeperham.com>
View
19 lib/connection_pool.rb
@@ -23,18 +23,17 @@
# - :size - number of connections to pool, defaults to 5
# - :timeout - amount of time to wait for a connection if none currently available, defaults to 5 seconds
#
-class ConnectionPool
+class ConnectionPool < BasicObject
DEFAULTS = { :size => 5, :timeout => 5 }
- undef :type if defined?(type)
+ def initialize(options={}, &block)
+ ::Kernel.raise ::ArgumentError, 'Connection pool requires a block' unless block
- def initialize(options={})
- raise ArgumentError, 'Connection pool requires a block' unless block_given?
-
- @available = TimedQueue.new
+ @available = ::TimedQueue.new
+ @oid = @available.object_id
@options = DEFAULTS.merge(options)
@options[:size].times do
- @available << yield
+ @available << block.call
end
end
@@ -54,14 +53,14 @@ def method_missing(name, *args)
private
def checkout
- Thread.current[:"current-#{self.object_id}"] ||= begin
+ ::Thread.current[:"current-#{@oid}"] ||= begin
@available.timed_pop(@options[:timeout])
end
end
def checkin
- conn = Thread.current[:"current-#{self.object_id}"]
- Thread.current[:"current-#{self.object_id}"] = nil
+ conn = ::Thread.current[:"current-#{@oid}"]
+ ::Thread.current[:"current-#{@oid}"] = nil
return unless conn
@available << conn
nil
Please sign in to comment.
Something went wrong with that request. Please try again.