Module Extlib::Pooling
In: lib/extlib/pooling.rb
Hash SimpleSet StandardError InvalidResourceError Logger Pool lib/extlib/logger.rb lib/extlib/simple_set.rb Inflection ClassMethods Hook Assertions lib/extlib/pooling.rb Pooling Extlib dot/m_27_0.png

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.

Methods

Classes and Modules

Class Extlib::Pooling::InvalidResourceError
Class Extlib::Pooling::Pool

External Aliases

new -> __new

Public Class methods

[Source]

    # File lib/extlib/pooling.rb, line 95
95:         def self.__pool_lock
96:           @__pool_lock
97:         end

[Source]

     # File lib/extlib/pooling.rb, line 99
 99:         def self.__pool_wait
100:           @__pool_wait
101:         end

[Source]

     # File lib/extlib/pooling.rb, line 107
107:         def self.__pools
108:           @__pools
109:         end

[Source]

    # File lib/extlib/pooling.rb, line 71
71:     def self.append_pool(pool)
72:       lock.synchronize do
73:         pools << pool
74:       end
75:       Extlib::Pooling::scavenger
76:     end

[Source]

     # File lib/extlib/pooling.rb, line 85
 85:     def self.included(target)
 86:       target.class_eval do
 87:         class << self
 88:           alias __new new
 89:         end
 90: 
 91:         @__pools     = {}
 92:         @__pool_lock = Mutex.new
 93:         @__pool_wait = ConditionVariable.new
 94: 
 95:         def self.__pool_lock
 96:           @__pool_lock
 97:         end
 98: 
 99:         def self.__pool_wait
100:           @__pool_wait
101:         end
102: 
103:         def self.new(*args)
104:           (@__pools[args] ||= __pool_lock.synchronize { Pool.new(self.pool_size, self, args) }).new
105:         end
106: 
107:         def self.__pools
108:           @__pools
109:         end
110: 
111:         def self.pool_size
112:           8
113:         end
114:       end
115:     end

[Source]

    # File lib/extlib/pooling.rb, line 78
78:     def self.lock
79:       @lock ||= Mutex.new
80:     end

[Source]

     # File lib/extlib/pooling.rb, line 103
103:         def self.new(*args)
104:           (@__pools[args] ||= __pool_lock.synchronize { Pool.new(self.pool_size, self, args) }).new
105:         end

[Source]

     # File lib/extlib/pooling.rb, line 111
111:         def self.pool_size
112:           8
113:         end

[Source]

    # File lib/extlib/pooling.rb, line 67
67:     def self.pools
68:       @pools ||= Set.new
69:     end

[Source]

    # File lib/extlib/pooling.rb, line 32
32:     def self.scavenger
33:       if @scavenger.nil? || !@scavenger.alive?
34:         @scavenger = Thread.new do
35:           running = true
36:           while running do
37:             # Sleep before we actually start doing anything.
38:             # Otherwise we might clean up something we just made
39:             sleep(scavenger_interval)
40: 
41:             lock.synchronize do
42:               pools.each do |pool|
43:                 # This is a useful check, but non-essential, and right now it breaks lots of stuff.
44:                 # if pool.expired?
45:                 pool.lock.synchronize do
46:                   if pool.expired?
47:                     pool.dispose
48:                   end
49:                 end
50:                 # end
51:               end
52: 
53:               # The pool is empty, we stop the scavenger
54:               # It wil be restarted if new resources are added again
55:               if pools.empty?
56:                 running = false
57:               end
58:             end
59:           end # loop
60:         end
61:       end
62: 
63:       @scavenger.priority = -10
64:       @scavenger
65:     end

[Source]

    # File lib/extlib/pooling.rb, line 28
28:     def self.scavenger?
29:       @scavenger && @scavenger.alive?
30:     end

[Source]

     # File lib/extlib/pooling.rb, line 227
227:     def self.scavenger_interval
228:       60
229:     end

Public Instance methods

[Source]

     # File lib/extlib/pooling.rb, line 117
117:     def release
118:       @__pool.release(self) unless @__pool.nil?
119:     end

[Validate]