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.



25
26
27
28
29
30
31
# File 'lib/aerospike/utils/pool.rb', line 25

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.



23
24
25
# File 'lib/aerospike/utils/pool.rb', line 23

def cleanup_block
  @cleanup_block
end

#create_blockObject

Returns the value of attribute create_block.



23
24
25
# File 'lib/aerospike/utils/pool.rb', line 23

def create_block
  @create_block
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/aerospike/utils/pool.rb', line 49

def empty?
  @pool.length == 0
end

#inspectObject



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

def inspect
  "#<Aerospike::Pool: size=#{size}>"
end

#lengthObject Also known as: size



53
54
55
# File 'lib/aerospike/utils/pool.rb', line 53

def length
  @pool.length
end

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



33
34
35
36
37
38
39
# File 'lib/aerospike/utils/pool.rb', line 33

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

#poll(create_new = true) ⇒ Object



42
43
44
45
46
47
# File 'lib/aerospike/utils/pool.rb', line 42

def poll(create_new=true)
  non_block = true
  @pool.pop(non_block)
rescue
  @create_block.call() if @create_block && create_new
end