Module: Restate::ServerContext::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



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/restate/server_context.rb', line 629

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.



624
625
626
627
# File 'lib/restate/server_context.rb', line 624

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