Class: MogileFS::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/mogilefs/pool.rb

Defined Under Namespace

Classes: BadObjectError

Instance Method Summary collapse

Constructor Details

#initialize(klass, *args) ⇒ Pool

Returns a new instance of Pool.



8
9
10
11
12
13
# File 'lib/mogilefs/pool.rb', line 8

def initialize(klass, *args)
  @args = args
  @klass = klass
  @queue = Queue.new
  @objects = []
end

Instance Method Details

#getObject



15
16
17
18
19
20
21
22
23
# File 'lib/mogilefs/pool.rb', line 15

def get
  begin
    object = @queue.pop true
  rescue ThreadError
    object = @klass.new(*@args)
    @objects << object
  end
  return object
end

#purgeObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/mogilefs/pool.rb', line 38

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

Raises:



25
26
27
28
29
# File 'lib/mogilefs/pool.rb', line 25

def put(o)
  raise BadObjectError unless @objects.include? o
  @queue.push o
  purge
end

#useObject



31
32
33
34
35
36
# File 'lib/mogilefs/pool.rb', line 31

def use
  object = get
  yield object
ensure
  put object
end