Module: Extlib::Pooling
- Defined in:
- lib/extlib/pooling.rb
Overview
Notes
Provides pooling support to class it got included in.
Pooling of objects is a faster way of aquiring instances of objects compared to regular allocation and initialization because instances are keeped in memory reused.
Classes that include Pooling module have re-defined new method that returns instances acquired from pool.
Term resource is used for any type of poolable objects and should NOT be thought as DataMapper Resource or ActiveResource resource and such.
In Data Objects connections are pooled so that it is unnecessary to allocate and initialize connection object each time connection is needed, like per request in a web application.
Pool obviously has to be thread safe because state of object is reset when it is released.
Defined Under Namespace
Classes: CrossPoolError, OrphanedObjectError, Pool, ThreadStopError
Class Method Summary collapse
- .append_pool(pool) ⇒ Object
- .included(target) ⇒ Object
- .lock ⇒ Object
- .pools ⇒ Object
- .scavenger ⇒ Object
- .scavenger_interval ⇒ Object
Instance Method Summary collapse
Class Method Details
.append_pool(pool) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/extlib/pooling.rb', line 57 def self.append_pool(pool) lock.synchronize do pools << pool end Extlib::Pooling::scavenger end |
.included(target) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/extlib/pooling.rb', line 77 def self.included(target) target.class_eval do class << self alias __new new end @__pools = Hash.new { |h,k| __pool_lock.synchronize { h[k] = Pool.new(target.pool_size, target, k) } } @__pool_lock = Mutex.new def self.__pool_lock @__pool_lock end def self.new(*args) @__pools[args].new end def self.__pools @__pools end def self.pool_size 8 end end end |
.lock ⇒ Object
64 65 66 |
# File 'lib/extlib/pooling.rb', line 64 def self.lock @lock ||= Mutex.new end |
.pools ⇒ Object
53 54 55 |
# File 'lib/extlib/pooling.rb', line 53 def self.pools @pools ||= Set.new end |
.scavenger ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/extlib/pooling.rb', line 28 def self.scavenger @scavenger || begin @scavenger = Thread.new do loop do lock.synchronize do pools.each do |pool| # This is a useful check, but non-essential, and right now it breaks lots of stuff. # if pool.expired? pool.lock.synchronize do if pool.reserved_count == 0 pool.dispose end end # end end end sleep(scavenger_interval) end # loop end @scavenger.priority = -10 @scavenger end end |
.scavenger_interval ⇒ Object
229 230 231 |
# File 'lib/extlib/pooling.rb', line 229 def self.scavenger_interval 60 end |
Instance Method Details
#release ⇒ Object
104 105 106 |
# File 'lib/extlib/pooling.rb', line 104 def release @__pool.release(self) unless @__pool.nil? end |