Class: Farce::Pool

Inherits:
Abstract::Pool show all
Includes:
Shareable::Unfreezable
Defined in:
lib/farce/pool.rb

Overview

A dynamically sized pool of Ractor schedulers.

Tasks wait in one shared queue. An empty pool starts its first worker immediately when work arrives. A task that remains queued for grow_after causes the pool to add a worker until max_size is reached. Workers above min_size retire after shrink_after without queued or running work. By default, the pool starts with no workers and can shrink back to zero.

Worker limits, task limits, scaling delays, and queue capacity can change while the pool runs. Lowering max_size retires extra workers as they become idle. Busy workers keep queued work runnable. Raising min_size starts workers immediately. Queued tasks are retained.

Most importantly, it exposes a Abstract::Pool#schedule method compatible with Abstract::Scheduler#schedule.

Examples:

# Create a new pool
pool = Farce::Pool.new

# Schedule some work on the pool.
pool.schedule do
  # The work runs in a non-blocking fiber, multiple tasks can run concurrently even on the same worker.
  loop do
    sleep 5
    MyClass.recurring_work
  end
end

Custom Fiber scheduler

require "farce"
require "carbon_fiber"

# At least two Ractors, up to four. Launch a new one if a task waits longer than 100 milliseconds.
# Use the fiber scheduler from the carbon_fiber gem.
pool = Farce::Pool.new(min_size: 2, max_size: 4, grow_after: 0.1) { CarbonFiber::Scheduler.new }

# Schedule some work
pool.schedule { MyClass.do_something }

Method Summary

Methods included from Shareable

#initialize, #ractor_shareable?

Methods inherited from Abstract::Pool

#at_max_size?, #capacity, #capacity=, #close, #closed?, #closing?, #error, #grow_after, #grow_after=, #initialize, #max_inflight, #max_inflight=, #max_size, #max_size=, #min_size, #min_size=, #schedule, #shrink_after, #shrink_after=, #size, #state, #try_schedule

Methods inherited from Abstract::Executor

#close, #close_all, #closed?, #error, #execute, #local?, #ractor_shareable?, #schedule, #state, #try_schedule

Methods included from Internal::Noncopyable

#duplicable?