Class: MogileFS::Pool
- Inherits:
-
Object
- Object
- MogileFS::Pool
- Defined in:
- lib/mogilefs/pool.rb
Defined Under Namespace
Classes: BadObjectError
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(klass, *args) ⇒ Pool
constructor
A new instance of Pool.
- #purge ⇒ Object
- #put(o) ⇒ Object
- #use ⇒ Object
Constructor Details
#initialize(klass, *args) ⇒ Pool
Returns a new instance of Pool.
9 10 11 12 13 14 |
# File 'lib/mogilefs/pool.rb', line 9 def initialize(klass, *args) @args = args @klass = klass @queue = Queue.new @objects = [] end |
Instance Method Details
#get ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/mogilefs/pool.rb', line 16 def get begin object = @queue.pop true rescue ThreadError object = @klass.new(*@args) @objects << object end object end |
#purge ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mogilefs/pool.rb', line 41 def purge return if @queue.length < 5 begin until @queue.length <= 2 do obj = @queue.pop true @objects.delete obj end rescue ThreadError end end |
#put(o) ⇒ Object
26 27 28 29 30 |
# File 'lib/mogilefs/pool.rb', line 26 def put(o) raise BadObjectError unless @objects.include? o @queue.push o purge end |
#use ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/mogilefs/pool.rb', line 32 def use object = get yield object nil ensure put object nil end |