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



717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/restate/server/context.rb', line 717

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.



712
713
714
715
# File 'lib/restate/server/context.rb', line 712

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