Class: LaunchDarkly::Impl::UnboundedPool
- Inherits:
-
Object
- Object
- LaunchDarkly::Impl::UnboundedPool
- Defined in:
- lib/ldclient-rb/impl/unbounded_pool.rb
Overview
A simple thread safe generic unbounded resource pool abstraction
Instance Method Summary collapse
- #acquire ⇒ Object
- #dispose_all ⇒ Object
-
#initialize(instance_creator, instance_destructor) ⇒ UnboundedPool
constructor
A new instance of UnboundedPool.
- #release(instance) ⇒ Object
Constructor Details
#initialize(instance_creator, instance_destructor) ⇒ UnboundedPool
Returns a new instance of UnboundedPool.
5 6 7 8 9 10 |
# File 'lib/ldclient-rb/impl/unbounded_pool.rb', line 5 def initialize(instance_creator, instance_destructor) @pool = Array.new @lock = Mutex.new @instance_creator = instance_creator @instance_destructor = instance_destructor end |
Instance Method Details
#acquire ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/ldclient-rb/impl/unbounded_pool.rb', line 12 def acquire @lock.synchronize { if @pool.length == 0 @instance_creator.call() else @pool.pop() end } end |
#dispose_all ⇒ Object
26 27 28 29 30 31 |
# File 'lib/ldclient-rb/impl/unbounded_pool.rb', line 26 def dispose_all @lock.synchronize { @pool.map { |instance| @instance_destructor.call(instance) } unless @instance_destructor.nil? @pool.clear() } end |
#release(instance) ⇒ Object
22 23 24 |
# File 'lib/ldclient-rb/impl/unbounded_pool.rb', line 22 def release(instance) @lock.synchronize { @pool.push(instance) } end |