Class: Pod4::ConnectionPool::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/pod4/connection_pool.rb

Instance Method Summary collapse

Constructor Details

#initializePool

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

#_dumpObject



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_currentObject



32
33
34
# File 'lib/pod4/connection_pool.rb', line 32

def get_current 
  get(Thread.current.object_id)
end

#get_freeObject



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_oldestObject



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

#sizeObject



53
54
55
# File 'lib/pod4/connection_pool.rb', line 53

def size 
  @items.size
end