Class: Aerospike::Pool

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = 256, &block) ⇒ Pool

Returns a new instance of Pool.



28
29
30
31
32
33
34
# File 'lib/aerospike/utils/pool.rb', line 28

def initialize(max_size = 256, &block)
  @create_block = block
  @cleanup_block = nil

  @pool = Queue.new
  @max_size = max_size
end

Instance Attribute Details

#cleanup_blockObject

Returns the value of attribute cleanup_block.



26
27
28
# File 'lib/aerospike/utils/pool.rb', line 26

def cleanup_block
  @cleanup_block
end

#create_blockObject

Returns the value of attribute create_block.



26
27
28
# File 'lib/aerospike/utils/pool.rb', line 26

def create_block
  @create_block
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/aerospike/utils/pool.rb', line 55

def empty?
  @pool.length == 0
end

#lengthObject



59
60
61
# File 'lib/aerospike/utils/pool.rb', line 59

def length
  @pool.length
end

#offer(obj) ⇒ Object Also known as: <<



36
37
38
39
40
41
42
# File 'lib/aerospike/utils/pool.rb', line 36

def offer(obj)
  if @pool.length < @max_size
    @pool << obj
  elsif @cleanup_block
    @cleanup_block.call(obj)
  end
end

#poll(create_new = true) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/aerospike/utils/pool.rb', line 45

def poll(create_new=true)
  res = nil
  begin
    res = @pool.pop(true) # non_blocking
    return res
  rescue
    return @create_block.call if @create_block && create_new
  end
end