mirror of
https://github.com/mperham/connection_pool
synced 2023-03-27 23:22:21 -04:00
Update connection_pool to subclass BasicObject
This makes it a more effective proxy.
This commit is contained in:
parent
74ce90a770
commit
3ac11a9206
3 changed files with 21 additions and 11 deletions
5
Changes.md
Normal file
5
Changes.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
HEAD
|
||||||
|
--------
|
||||||
|
|
||||||
|
- More precise timeouts
|
||||||
|
- ConnectionPool now subclasses BasicObject so `method_missing` is more effective.
|
|
@ -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.
|
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
|
Install
|
||||||
------------
|
------------
|
||||||
|
|
||||||
|
|
|
@ -23,18 +23,17 @@ require 'connection_pool/timed_queue'
|
||||||
# - :size - number of connections to pool, defaults to 5
|
# - :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
|
# - :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 }
|
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={})
|
@available = ::TimedQueue.new
|
||||||
raise ArgumentError, 'Connection pool requires a block' unless block_given?
|
@oid = @available.object_id
|
||||||
|
|
||||||
@available = TimedQueue.new
|
|
||||||
@options = DEFAULTS.merge(options)
|
@options = DEFAULTS.merge(options)
|
||||||
@options[:size].times do
|
@options[:size].times do
|
||||||
@available << yield
|
@available << block.call
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -54,14 +53,14 @@ class ConnectionPool
|
||||||
private
|
private
|
||||||
|
|
||||||
def checkout
|
def checkout
|
||||||
Thread.current[:"current-#{self.object_id}"] ||= begin
|
::Thread.current[:"current-#{@oid}"] ||= begin
|
||||||
@available.timed_pop(@options[:timeout])
|
@available.timed_pop(@options[:timeout])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def checkin
|
def checkin
|
||||||
conn = Thread.current[:"current-#{self.object_id}"]
|
conn = ::Thread.current[:"current-#{@oid}"]
|
||||||
Thread.current[:"current-#{self.object_id}"] = nil
|
::Thread.current[:"current-#{@oid}"] = nil
|
||||||
return unless conn
|
return unless conn
|
||||||
@available << conn
|
@available << conn
|
||||||
nil
|
nil
|
||||||
|
|
Loading…
Reference in a new issue