Class: Hyperion::Pool
- Inherits:
-
Object
- Object
- Hyperion::Pool
- Defined in:
- lib/hyperion/pool.rb
Overview
Single-thread object pool with a maximum size. Acquire returns an existing object (mutated for reuse) or constructs a new one. Release returns the object to the pool unless the pool is full.
Not thread-safe. Each Hyperion worker process runs one fiber scheduler on one thread, so a per-process pool is contention-free.
Instance Method Summary collapse
- #acquire ⇒ Object
-
#initialize(max_size:, factory:, reset: nil) ⇒ Pool
constructor
A new instance of Pool.
- #release(obj) ⇒ Object
-
#size ⇒ Object
rubocop:disable Rails/Delegate.
Constructor Details
#initialize(max_size:, factory:, reset: nil) ⇒ Pool
Returns a new instance of Pool.
11 12 13 14 15 16 |
# File 'lib/hyperion/pool.rb', line 11 def initialize(max_size:, factory:, reset: nil) @max_size = max_size @factory = factory @reset = reset @free = [] end |
Instance Method Details
#acquire ⇒ Object
18 19 20 21 22 |
# File 'lib/hyperion/pool.rb', line 18 def acquire obj = @free.pop || @factory.call @reset&.call(obj) obj end |
#release(obj) ⇒ Object
24 25 26 27 28 |
# File 'lib/hyperion/pool.rb', line 24 def release(obj) return if @free.size >= @max_size @free.push(obj) end |
#size ⇒ Object
rubocop:disable Rails/Delegate
30 31 32 |
# File 'lib/hyperion/pool.rb', line 30 def size # rubocop:disable Rails/Delegate @free.size end |