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.
|
||||
|
||||
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>
|
||||
|
|
|
@ -23,18 +23,17 @@ require 'connection_pool/timed_queue'
|
|||
# - :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 @@ class ConnectionPool
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue