Module: Restate::Server::Context::BackgroundPool

Defined in:
lib/restate/server/context.rb

Overview

A simple fixed-size thread pool for background: true runs. Avoids creating a new Thread per call (~1ms + ~1MB stack each). Workers are daemon threads that do not prevent process exit.

Constant Summary collapse

POOL_SIZE =
Integer(ENV.fetch('RESTATE_BACKGROUND_POOL_SIZE', 8))

Class Method Summary collapse

Class Method Details

.ensure_startedObject



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'lib/restate/server/context.rb', line 764

def ensure_started
  return if @size >= POOL_SIZE

  @mutex.synchronize do
    while @size < POOL_SIZE
      @size += 1
      worker = Thread.new do
        Kernel.loop do
          job = @queue.pop
          break if job == :shutdown

          job.call
        end
      end
      worker.name = "restate-bg-#{@size}"
      # Daemon thread: does not prevent the process from exiting.
      worker.report_on_exception = false
      @workers << worker
    end
  end
end

.submit(&block) ⇒ Object

Submit a block to be executed by a pool worker.



759
760
761
762
# File 'lib/restate/server/context.rb', line 759

def submit(&block)
  ensure_started
  @queue.push(block)
end