Class: LaunchDarkly::Impl::UnboundedPool

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/impl/unbounded_pool.rb

Overview

A simple thread safe generic unbounded resource pool abstraction

Since:

  • 5.5.0

Instance Method Summary collapse

Constructor Details

#initialize(instance_creator, instance_destructor) ⇒ UnboundedPool

Returns a new instance of UnboundedPool.

Since:

  • 5.5.0



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

#acquireObject

Since:

  • 5.5.0



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_allObject

Since:

  • 5.5.0



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

Since:

  • 5.5.0



22
23
24
# File 'lib/ldclient-rb/impl/unbounded_pool.rb', line 22

def release(instance)
  @lock.synchronize { @pool.push(instance) }
end