Class: Pod4::ConnectionPool::Pool
- Inherits:
-
Object
- Object
- Pod4::ConnectionPool::Pool
- Defined in:
- lib/pod4/connection_pool.rb
Instance Method Summary collapse
- #<<(cl) ⇒ Object
- #_dump ⇒ Object
- #get(id) ⇒ Object
- #get_current ⇒ Object
- #get_free ⇒ Object
- #get_oldest ⇒ Object
-
#initialize ⇒ Pool
constructor
A new instance of Pool.
- #release(id = nil) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize ⇒ Pool
Returns a new instance of Pool.
17 18 19 20 |
# File 'lib/pod4/connection_pool.rb', line 17 def initialize @items = [] @mutex = Mutex.new end |
Instance Method Details
#<<(cl) ⇒ Object
22 23 24 25 26 |
# File 'lib/pod4/connection_pool.rb', line 22 def <<(cl) @mutex.synchronize do @items << PoolItem.new(cl, Thread.current.object_id, Time.now) end end |
#_dump ⇒ Object
57 58 59 60 61 |
# File 'lib/pod4/connection_pool.rb', line 57 def _dump @mutex.synchronize do @items end end |
#get(id) ⇒ Object
28 29 30 |
# File 'lib/pod4/connection_pool.rb', line 28 def get(id) @items.find{|x| x.thread_id == id } end |
#get_current ⇒ Object
32 33 34 |
# File 'lib/pod4/connection_pool.rb', line 32 def get_current get(Thread.current.object_id) end |
#get_free ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/pod4/connection_pool.rb', line 40 def get_free @mutex.synchronize do pi = @items.find{|x| x.thread_id.nil? } pi.thread_id = Thread.current.object_id if pi pi end end |
#get_oldest ⇒ Object
36 37 38 |
# File 'lib/pod4/connection_pool.rb', line 36 def get_oldest @items.sort{|a,b| a.stamp <=> b.stamp}.first end |
#release(id = nil) ⇒ Object
48 49 50 51 |
# File 'lib/pod4/connection_pool.rb', line 48 def release(id=nil) pi = id.nil? ? get_current : get(id) pi.thread_id = nil if pi end |
#size ⇒ Object
53 54 55 |
# File 'lib/pod4/connection_pool.rb', line 53 def size @items.size end |