Skip to content

Instantly share code, notes, and snippets.

View btihen's full-sized avatar

Bill Tihen btihen

  • Switzerland
View GitHub Profile
module RactorPool
# using the ForkedJoin pattern - https://en.wikipedia.org/wiki/Fork%E2%80%93join_model
# this creates a worker pool (of Ractors) as large as the number of input messages
# and then collects the results upon completion - at which point the Ractors are
# removed from the worker pool and available to be garbage collected
# note: the injected behavior must respond to #call(input)
# usage: define a behavior that responds to #call(input)
# class Fibonacci
# def call(n) = [n, fib(n)]
# def fib(n) = n < 2 ? 1 : fib(n-2) + fib(n-1)
module RactorPool
# static sized pool with supervision
# use multi_use at own risk - be sure to collect results before submitting more messages
class WorkerPool
attr_reader :behavior, :inputs, :input_queue, :multi_use, :worker_count, :workers_pool
def self.call(input, behavior, options = {})
new(input, behavior, options)
end