Class: RactorPool
- Inherits:
-
Object
- Object
- RactorPool
- Defined in:
- lib/ractor-pool.rb,
lib/ractor-pool/version.rb,
sig/generated/ractor-pool.rbs,
sig/generated/ractor-pool/version.rbs
Overview
A thread-safe, lock-free pool of Ractor workers with coordinator or round-robin dispatch for distributing work.
RactorPool manages a fixed number of worker ractors that process work items in parallel.
The :coordinator strategy (the default) routes each work item to whichever worker is
currently idle via a dedicated coordinator Ractor, so a slow item on one worker does not
block faster items from being picked up by other workers. Use it when work items have
variable cost. The :round_robin strategy dispatches work to workers in turn, so a slow
item on one worker queues the next item destined for that worker behind it, even if other
workers are idle. Use it when work items have uniform cost. Results are collected and
passed to a result handler running in a separate thread.
Defined Under Namespace
Classes: EnqueuedWorkAfterShutdownError, Error
Constant Summary collapse
- STRATEGIES =
%i[coordinator round_robin].freeze
- SHUTDOWN =
:shutdown- VERSION =
"0.4.1"
Instance Method Summary collapse
-
#<<(work) ⇒ void
Queues a work item to be processed by an available worker.
-
#initialize(size: Etc.nprocessors, worker:, strategy: :coordinator, name: nil, on_error: nil) {|result| ... } ⇒ RactorPool
constructor
Creates a new RactorPool with the specified number of workers.
-
#shutdown ⇒ void
Shuts down the pool gracefully.
- #start_collector ⇒ Thread?
- #start_coordinator ⇒ Ractor
- #start_error_collector ⇒ Thread?
- #start_workers ⇒ Array[Ractor]
Constructor Details
#initialize(size: Etc.nprocessors, worker:, strategy: :coordinator, name: nil, on_error: nil) {|result| ... } ⇒ RactorPool
Creates a new RactorPool with the specified number of workers.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ractor-pool.rb', line 107 def initialize(size: Etc.nprocessors, worker:, strategy: :coordinator, name: nil, on_error: nil, &result_handler) raise ArgumentError, "size must be a positive Integer" unless size.is_a?(Integer) && size > 0 raise ArgumentError, "worker must be a Proc" unless worker.is_a?(Proc) raise ArgumentError, "strategy must be one of #{STRATEGIES.inspect}" unless STRATEGIES.include?(strategy) raise ArgumentError, "on_error must be a Proc" if on_error && !on_error.is_a?(Proc) @size = size @worker = Ractor.shareable_proc(&worker) @strategy = strategy @name = name @on_error = Ractor.shareable_proc(&on_error) if on_error @result_handler = result_handler @in_flight = Atom.new(0) @shutdown = Atom.new(false) @next_worker_index = Atom.new(-1) if size > 1 && strategy == :round_robin @result_port = Ractor::Port.new if result_handler @error_port = Ractor::Port.new unless on_error @coordinator = start_coordinator if size > 1 && strategy == :coordinator @workers = start_workers @collector = start_collector @error_collector = start_error_collector end |
Instance Method Details
#<<(work) ⇒ void
This method returns an undefined value.
Queues a work item to be processed by an available worker.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ractor-pool.rb', line 143 def <<(work) raise EnqueuedWorkAfterShutdownError if @shutdown.value @in_flight.swap { |count| count + 1 } if @shutdown.value @in_flight.swap { |count| count - 1 } raise EnqueuedWorkAfterShutdownError end target = if @coordinator @coordinator elsif @next_worker_index @workers[@next_worker_index.swap { |index| (index + 1) % @size }] else @workers.first end begin target.send(work, move: true) ensure @in_flight.swap { |count| count - 1 } end end |
#shutdown ⇒ void
This method returns an undefined value.
Shuts down the pool gracefully.
This method:
- Prevents new work from being queued
- Waits for all in-flight work submissions to complete
- Allows all queued work to complete
- Waits for all workers to finish
- Waits for all results and errors to be processed
This method is idempotent and can be called multiple times safely.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/ractor-pool.rb', line 185 def shutdown already_shutdown = false @shutdown.swap do |current| if current already_shutdown = true current else true end end return if already_shutdown Thread.pass until @in_flight.value.zero? if @coordinator @coordinator.send(SHUTDOWN, move: true) @workers.each(&:join) @coordinator.join else @workers.each { |worker| worker.send(SHUTDOWN, move: true) } @workers.each(&:join) @result_port&.send(SHUTDOWN, move: true) end @error_port&.send(SHUTDOWN, move: true) @error_collector&.join @collector&.join end |
#start_collector ⇒ Thread?
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/ractor-pool.rb', line 305 def start_collector return unless @result_handler thread_name = String.new("#{self.class.name} collector thread") thread_name << " for #{@name}" if @name Thread.new(@result_port, @result_handler, thread_name) do |result_port, result_handler, name| Thread.current.name = name loop do result = result_port.receive break if result == SHUTDOWN result_handler.call(result) end end end |
#start_coordinator ⇒ Ractor
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/ractor-pool.rb', line 216 def start_coordinator ractor_name = String.new("#{self.class.name} coordinator ractor") ractor_name << " for #{@name}" if @name thread_name = String.new("#{self.class.name} coordinator thread") thread_name << " for #{@name}" if @name Ractor.new(@size, @result_port, thread_name, name: ractor_name) do |worker_count, result_port, thread_name| Thread.current.name = thread_name work_queue = [] waiting_workers = [] shutdown_received = false workers_finished = 0 loop do case data = Ractor.receive when SHUTDOWN shutdown_received = true workers_finished += waiting_workers.size waiting_workers.each { |worker| worker.send(SHUTDOWN, move: true) } waiting_workers.clear if workers_finished == worker_count result_port&.send(SHUTDOWN, move: true) break end when Ractor ractor = data if work_queue.any? ractor.send(work_queue.shift, move: true) elsif shutdown_received ractor.send(SHUTDOWN, move: true) workers_finished += 1 if workers_finished == worker_count result_port&.send(SHUTDOWN, move: true) break end else waiting_workers << ractor end else work = data if waiting_workers.any? waiting_workers.shift.send(work, move: true) else work_queue << work end end end end end |
#start_error_collector ⇒ Thread?
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/ractor-pool.rb', line 324 def start_error_collector return if @on_error thread_name = String.new("#{self.class.name} error collector thread") thread_name << " for #{@name}" if @name Thread.new(@error_port, thread_name) do |error_port, name| Thread.current.name = name loop do = error_port.receive break if == SHUTDOWN warn end end end |
#start_workers ⇒ Array[Ractor]
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/ractor-pool.rb', line 275 def start_workers @size.times.map do |index| ractor_name = String.new("#{self.class.name} ractor #{index}") ractor_name << " for #{@name}" if @name thread_name = String.new("#{self.class.name} worker thread #{index}") thread_name << " for #{@name}" if @name Ractor.new(@worker, @on_error, @error_port, @coordinator, @result_port, thread_name, name: ractor_name) do |worker, on_error, error_port, coordinator, result_port, thread_name| Thread.current.name = thread_name loop do coordinator&.send(Ractor.current, move: true) work = Ractor.receive break if work == SHUTDOWN begin result = worker.call(work) result_port&.send(result, move: true) rescue => error on_error ? on_error.call(error) : error_port.send(error., move: true) end end end end end |