Class: Ruby::Rego::EnvironmentPool

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/rego/environment_pool.rb

Overview

Pools Environment instances for reuse across evaluations. Thread-safe for concurrent checkouts/checkins.

Instance Method Summary collapse

Constructor Details

#initialize(max_size: nil) ⇒ EnvironmentPool

:reek:ControlParameter

Parameters:

  • max_size (Integer, nil) (defaults to: nil)

    maximum pool size for reuse (nil means unbounded)



12
13
14
15
16
17
# File 'lib/ruby/rego/environment_pool.rb', line 12

def initialize(max_size: nil)
  pool = [] # @type var pool: Array[Environment]
  @pool = pool
  @max_size = normalized_max_size(max_size)
  @mutex = Mutex.new
end

Instance Method Details

#checkin(environment) ⇒ void

This method returns an undefined value.

Parameters:



32
33
34
35
36
37
38
39
40
# File 'lib/ruby/rego/environment_pool.rb', line 32

def checkin(environment)
  @mutex.synchronize do
    return nil if max_size_full?(@pool.length)

    environment.prepare_for_pool
    @pool << environment
  end
  nil
end

#checkout(state) ⇒ Environment

Parameters:

Returns:



21
22
23
24
25
26
27
28
# File 'lib/ruby/rego/environment_pool.rb', line 21

def checkout(state)
  @mutex.synchronize do
    environment = @pool.pop # @type var environment: Environment?
    return Environment.from_state(state) unless environment

    environment.reset!(state)
  end
end