farce 0.2.0-java → 0.4.0-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +91 -0
- data/README.md +117 -1451
- data/docs/compatibility.md +74 -0
- data/docs/concurrency.md +151 -0
- data/docs/data-structures.md +313 -0
- data/docs/installation.md +63 -0
- data/docs/integrations.md +124 -0
- data/docs/introduction.md +266 -0
- data/docs/modes.md +8 -5
- data/docs/scheduling.md +176 -0
- data/docs/scopes.md +3 -1
- data/docs/sharing.md +211 -0
- data/docs/shims.md +144 -0
- data/docs/utilities.md +93 -0
- data/docs/variants-and-scopes.md +92 -0
- data/docs/variants.md +18 -0
- data/lib/farce/abstract/executor.rb +130 -0
- data/lib/farce/abstract/flag.rb +6 -0
- data/lib/farce/abstract/pool.rb +334 -0
- data/lib/farce/abstract/scheduler.rb +539 -93
- data/lib/farce/backports.rb +274 -0
- data/lib/farce/engine/jruby/fiber_scheduler.rb +3 -0
- data/lib/farce/engine/jruby/scheduler_synchronization.rb +87 -0
- data/lib/farce/engine/jruby/signal.rb +7 -4
- data/lib/farce/engine/ruby/3.4/null_port.rb +21 -0
- data/lib/farce/engine/ruby/3.4/patch.rb +67 -0
- data/lib/farce/engine/ruby/3.4/port.rb +13 -12
- data/lib/farce/engine/ruby/3.4/ractor_methods.rb +0 -1
- data/lib/farce/engine/ruby/4.0/null_port.rb +36 -0
- data/lib/farce/engine/ruby/4.0/port.rb +1 -2
- data/lib/farce/engine/ruby/4.1/null_port.rb +5 -0
- data/lib/farce/engine/ruby.rb +7 -0
- data/lib/farce/engine/shared/main_scheduler.rb +2 -2
- data/lib/farce/engine/shared/null_port.rb +18 -0
- data/lib/farce/engine/shared/parallel_scheduler.rb +1 -1
- data/lib/farce/engine/shared/port.rb +41 -3
- data/lib/farce/engine/shared/proxy_owner.rb +1 -1
- data/lib/farce/engine/shared/queue.rb +4 -4
- data/lib/farce/engine/shared/ractor_methods.rb +2 -2
- data/lib/farce/engine/shared/ractor_selector.rb +73 -0
- data/lib/farce/engine/shared/unshared_atom.rb +9 -8
- data/lib/farce/engine/shared.rb +9 -0
- data/lib/farce/extension.rb +150 -0
- data/lib/farce/integrations/ractor_tmvar.rb +4 -1
- data/lib/farce/internal/fake_ractor.rb +10 -2
- data/lib/farce/internal/lease_initialization.rb +1 -1
- data/lib/farce/internal/local_executor_methods.rb +33 -0
- data/lib/farce/internal/local_scheduled_task.rb +27 -0
- data/lib/farce/internal/scheduled_task.rb +1 -1
- data/lib/farce/internal/scheduler_lifecycle.rb +7 -4
- data/lib/farce/internal/thread_pool_supervisor.rb +45 -0
- data/lib/farce/internal/unshared_pool_worker.rb +14 -0
- data/lib/farce/internal.rb +14 -0
- data/lib/farce/local/pool.rb +111 -0
- data/lib/farce/local/scheduler.rb +219 -0
- data/lib/farce/null_port.rb +12 -0
- data/lib/farce/pool.rb +2 -290
- data/lib/farce/proxy.rb +2 -2
- data/lib/farce/scheduler.rb +1 -502
- data/lib/farce/system.rb +10 -2
- data/lib/farce/task.rb +285 -0
- data/lib/farce/{thread_scheduler.rb → thread_executor.rb} +7 -7
- data/lib/farce/unshared/pool.rb +44 -0
- data/lib/farce/unshared/scheduler.rb +89 -0
- data/lib/farce/version.rb +1 -1
- data/lib/farce.rb +10 -18
- metadata +35 -3
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# shareable_constant_value: literal
|
|
3
|
+
# warn_indent: true
|
|
4
|
+
|
|
5
|
+
module Farce
|
|
6
|
+
module Abstract
|
|
7
|
+
# A task executor that can submit work or wait for it to finish.
|
|
8
|
+
#
|
|
9
|
+
# {#schedule} submits a task. {#execute} waits for the task to finish and may
|
|
10
|
+
# run it directly when execution is local. Concrete executors decide where
|
|
11
|
+
# tasks run and how their arguments are transferred.
|
|
12
|
+
#
|
|
13
|
+
# @abstract Use a scheduler or pool class.
|
|
14
|
+
#
|
|
15
|
+
# @!method schedule(*args, mode: :smart, auto_local: true)
|
|
16
|
+
# @abstract This method must be implemented by concrete executor subclasses.
|
|
17
|
+
#
|
|
18
|
+
# Schedules a block for execution.
|
|
19
|
+
# What actually happens depends on the concrete executor implementation.
|
|
20
|
+
# Some executors may ignore the `auto_local` option. Some might treat every mode as `:local`.
|
|
21
|
+
#
|
|
22
|
+
# The executor may choose to block until the task has been executed, but most will add it to a queue of pending
|
|
23
|
+
# tasks, only blocking when that queue's capacity has been reached.
|
|
24
|
+
#
|
|
25
|
+
# @!macro modes
|
|
26
|
+
#
|
|
27
|
+
# @param args [Array]
|
|
28
|
+
# Arguments to pass to the scheduled block.
|
|
29
|
+
# May be handled according to `mode`.
|
|
30
|
+
#
|
|
31
|
+
# @param mode [Symbol]
|
|
32
|
+
# The mode in which to schedule the task. Defaults to `:smart`.
|
|
33
|
+
# May be ignored by some executors.
|
|
34
|
+
#
|
|
35
|
+
# @param auto_local [Boolean]
|
|
36
|
+
# Whether to automatically use a local mode when possible. Defaults to `true`.
|
|
37
|
+
# May be ignored by some executors.
|
|
38
|
+
#
|
|
39
|
+
# @yield [*args] The block to be executed by the scheduler.
|
|
40
|
+
# @yieldparam args [Array] The arguments passed to the block.
|
|
41
|
+
# @yieldreceiver [BasicObject, nil]
|
|
42
|
+
# Local mode will not touch the block's binding (self stays the same), but other modes may change it to `nil`.
|
|
43
|
+
#
|
|
44
|
+
# @return [self]
|
|
45
|
+
#
|
|
46
|
+
# @!method close
|
|
47
|
+
# @abstract This method must be implemented by concrete executor subclasses.
|
|
48
|
+
# Closes the executor, releasing any resources it holds.
|
|
49
|
+
# Once closed, the executor cannot be used to schedule new tasks.
|
|
50
|
+
# Will drain any pending tasks before fully closing. If tasks are running indefinitely, it may block.
|
|
51
|
+
# @return [self]
|
|
52
|
+
#
|
|
53
|
+
# @!method closed?
|
|
54
|
+
# @abstract This method must be implemented by concrete executor subclasses.
|
|
55
|
+
# @return [Boolean] Whether the executor is closed.
|
|
56
|
+
#
|
|
57
|
+
# @!method state
|
|
58
|
+
# @abstract This method must be implemented by concrete executor subclasses.
|
|
59
|
+
# @return [Symbol]
|
|
60
|
+
# The current state of the executor.
|
|
61
|
+
#
|
|
62
|
+
# Common states include:
|
|
63
|
+
# * `:initialized`, `:launching` and `:setup` for the setup phase.
|
|
64
|
+
# * `:running` for the execution phase.
|
|
65
|
+
# * `:closing`, `:closed`, and `:error` for the termination phase.
|
|
66
|
+
#
|
|
67
|
+
# Not all executors will implement every state.
|
|
68
|
+
#
|
|
69
|
+
# @!method ractor_shareable?
|
|
70
|
+
# @abstract Executors should include {Shareable} or {Unshareable}, which implement this method.
|
|
71
|
+
# @return [Boolean] Whether the executor is safe to use across Ractors.
|
|
72
|
+
class Executor
|
|
73
|
+
include Internal::Noncopyable
|
|
74
|
+
|
|
75
|
+
# An error that occurred during the executor's operation, if any.
|
|
76
|
+
# This is primarily for scheduling errors, not execution errors.
|
|
77
|
+
# You can check this if the {#state} is `:error`.
|
|
78
|
+
def error = nil
|
|
79
|
+
|
|
80
|
+
# @param wait [Boolean] Whether to wait for the owner to be non-nil before checking if the executor is local.
|
|
81
|
+
# @return [Boolean] Whether the executor is local for the current Ractor.
|
|
82
|
+
def local?(wait: true) = false # rubocop:disable Lint/UnusedMethodArgument
|
|
83
|
+
|
|
84
|
+
# Attempts to submit a task. Queue-based executors return `nil` when full
|
|
85
|
+
# instead of waiting for space. Other executors behave like {#schedule}.
|
|
86
|
+
#
|
|
87
|
+
# @param (see #schedule)
|
|
88
|
+
# @return [self, nil] self when submitted, or nil when the queue is full.
|
|
89
|
+
def try_schedule(...) = schedule(...)
|
|
90
|
+
|
|
91
|
+
# Closes the executor in every scope.
|
|
92
|
+
# For executors without scopes, this behaves like {#close}.
|
|
93
|
+
#
|
|
94
|
+
# @return [self]
|
|
95
|
+
def close_all = close
|
|
96
|
+
|
|
97
|
+
# Like {#schedule}, with two differences:
|
|
98
|
+
# 1. If {#local?} returns true, and it would run in local mode (either due to `auto_local` or explicitly set
|
|
99
|
+
# `mode: :local`), it executes the block directly without enqueuing it.
|
|
100
|
+
# 2. It blocks until the block has been executed, either immediately in local mode or after being scheduled.
|
|
101
|
+
#
|
|
102
|
+
# @param (see #schedule)
|
|
103
|
+
# @return [self]
|
|
104
|
+
def execute(*, mode: :smart, auto_local: true, &callback)
|
|
105
|
+
raise LocalJumpError, "Cannot yield without a block" unless block_given?
|
|
106
|
+
raise SchedulerClosedError, "cannot execute task on a closed scheduler" if closed?
|
|
107
|
+
|
|
108
|
+
if (auto_local || mode == :local) && local?
|
|
109
|
+
raise SchedulerClosedError, "cannot execute task on a closed scheduler" if closed?
|
|
110
|
+
yield(*)
|
|
111
|
+
return self
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
ran = Farce::Flag.new(false)
|
|
115
|
+
signal = Signal.new
|
|
116
|
+
callback = Ractor.shareable_proc(&callback) unless Ractor.shareable?(callback)
|
|
117
|
+
|
|
118
|
+
schedule(callback, ran, signal, *, mode:, auto_local:) do |callback, ran, signal, *args|
|
|
119
|
+
callback.call(*args)
|
|
120
|
+
ensure
|
|
121
|
+
ran.set
|
|
122
|
+
signal.broadcast
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
signal.wait_until { ran.value }
|
|
126
|
+
self
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
data/lib/farce/abstract/flag.rb
CHANGED
|
@@ -59,6 +59,12 @@ module Farce
|
|
|
59
59
|
include Internal::ValueSerialization
|
|
60
60
|
include Internal::Inspect
|
|
61
61
|
|
|
62
|
+
# @return [Boolean] whether the flag is set
|
|
63
|
+
def set? = value
|
|
64
|
+
|
|
65
|
+
# @return [Boolean] whether the flag is unset
|
|
66
|
+
def unset? = !value
|
|
67
|
+
|
|
62
68
|
# Wait until the flag becomes true.
|
|
63
69
|
# @param timeout [Numeric, nil] the total seconds available
|
|
64
70
|
# @yield called when the timeout expires
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# shareable_constant_value: literal
|
|
3
|
+
# warn_indent: true
|
|
4
|
+
|
|
5
|
+
module Farce
|
|
6
|
+
module Abstract
|
|
7
|
+
# A task scheduler with a dynamically sized pool of workers.
|
|
8
|
+
#
|
|
9
|
+
# Pools accept work through {#schedule}. {#close} rejects new tasks and drains
|
|
10
|
+
# queued work. Concrete pools use either Ractor or Thread workers.
|
|
11
|
+
#
|
|
12
|
+
# @abstract Use {Farce::Pool}, {Unshared::Pool}, or {Local::Pool}.
|
|
13
|
+
class Pool < Executor
|
|
14
|
+
include Internal::Inspect
|
|
15
|
+
|
|
16
|
+
# @return [Float] Maximum queue wait before adding a worker.
|
|
17
|
+
attr_reader :grow_after
|
|
18
|
+
|
|
19
|
+
# @return [Integer, nil] Soft task limit for each worker.
|
|
20
|
+
attr_reader :max_inflight
|
|
21
|
+
|
|
22
|
+
# @return [Integer] Maximum worker count.
|
|
23
|
+
attr_reader :max_size
|
|
24
|
+
|
|
25
|
+
# @return [Integer] Minimum worker count.
|
|
26
|
+
attr_reader :min_size
|
|
27
|
+
|
|
28
|
+
# @return [Float, nil] Idle time before an extra worker retires.
|
|
29
|
+
attr_reader :shrink_after
|
|
30
|
+
|
|
31
|
+
# Create and start an elastic pool using the configured fiber scheduler.
|
|
32
|
+
# An explicit constructor block overrides the configured default.
|
|
33
|
+
#
|
|
34
|
+
# `max_inflight` is a soft limit. A worker may exceed it when its scheduler
|
|
35
|
+
# has no runnable fibers or when the pool has reached `max_size`.
|
|
36
|
+
#
|
|
37
|
+
# @param min_size [Integer] Workers started immediately and retained while open.
|
|
38
|
+
# @param max_size [Integer] Maximum workers allowed.
|
|
39
|
+
# @param max_inflight [Integer, nil] Soft task limit for each worker.
|
|
40
|
+
# @param grow_after [Numeric] Queue wait before adding another worker.
|
|
41
|
+
# @param shrink_after [Numeric, nil] Idle time before an extra worker retires.
|
|
42
|
+
# @param capacity [Integer, nil] Pending-task capacity.
|
|
43
|
+
# @param backend [Symbol] IO backend for the built-in scheduler.
|
|
44
|
+
# @yieldreturn [Object] Fiber scheduler constructed in each worker.
|
|
45
|
+
def initialize(min_size: 0, max_size: 4, max_inflight: 64,
|
|
46
|
+
grow_after: 0.005, shrink_after: 30, capacity: 1024,
|
|
47
|
+
backend: Internal::FROZEN_CONFIG.io_backend, &constructor)
|
|
48
|
+
constructor ||= Internal::FROZEN_CONFIG.fiber_scheduler_constructor
|
|
49
|
+
initialize_configuration(min_size:, max_size:, max_inflight:, grow_after:, shrink_after:, backend:)
|
|
50
|
+
@constructor = prepare_constructor(constructor)
|
|
51
|
+
@queue = new_queue(capacity: capacity && Integer(capacity))
|
|
52
|
+
@state = Internal::Atom.new(:running)
|
|
53
|
+
@error = Internal::Atom.new
|
|
54
|
+
@worker_count = Internal::Atom.new(0)
|
|
55
|
+
@pressure = Internal::Atom.new
|
|
56
|
+
@admission = Internal::Signal.new
|
|
57
|
+
super()
|
|
58
|
+
publish_pool
|
|
59
|
+
begin
|
|
60
|
+
@min_size.times { start_worker }
|
|
61
|
+
rescue Exception # rubocop:disable Lint/RescueException
|
|
62
|
+
close
|
|
63
|
+
raise
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Enqueue a task for any worker in the pool.
|
|
68
|
+
#
|
|
69
|
+
# Ractor pools reject local transfer. Thread pools preserve the block and
|
|
70
|
+
# arguments. With `auto_local: false`, they accept only local or smart modes.
|
|
71
|
+
#
|
|
72
|
+
# @overload schedule(*args, mode: :smart, auto_local: true)
|
|
73
|
+
# @!macro modes
|
|
74
|
+
# @param args [Array<Object>] Positional arguments passed to the task block.
|
|
75
|
+
# @param mode [Symbol] Argument transfer mode.
|
|
76
|
+
# @param auto_local [Boolean] Whether to use local transfer in a thread pool.
|
|
77
|
+
# @yield [*args] Task to execute.
|
|
78
|
+
# @return [Pool] self.
|
|
79
|
+
# @raise [PoolClosedError] If the pool is closing or closed.
|
|
80
|
+
def schedule(...) = enqueue_task(false, ...)
|
|
81
|
+
|
|
82
|
+
# Enqueues a task without waiting for queue space.
|
|
83
|
+
# Arguments and transfer modes follow {#schedule}.
|
|
84
|
+
#
|
|
85
|
+
# @overload try_schedule(*args, mode: :smart, auto_local: true)
|
|
86
|
+
# @param (see Farce::Abstract::Executor#schedule)
|
|
87
|
+
# @return [self, nil] self when submitted, or nil when the queue is full.
|
|
88
|
+
def try_schedule(...) = enqueue_task(true, ...)
|
|
89
|
+
|
|
90
|
+
# Stop accepting tasks and drain queued work.
|
|
91
|
+
#
|
|
92
|
+
# @return [Pool] self.
|
|
93
|
+
def close
|
|
94
|
+
return self if closed?
|
|
95
|
+
@state.compare_and_set(:running, :closing)
|
|
96
|
+
@queue.seal
|
|
97
|
+
admission_changed
|
|
98
|
+
if size.zero?
|
|
99
|
+
@queue.closed? ? finish_close : start_worker(drain: true)
|
|
100
|
+
end
|
|
101
|
+
self
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Return the first worker error, if any.
|
|
105
|
+
def error
|
|
106
|
+
value = @error.value
|
|
107
|
+
value.is_a?(Envelope) ? value.value : value
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Return the current number of running and starting workers.
|
|
111
|
+
def size = @worker_count.value
|
|
112
|
+
|
|
113
|
+
# Return the current pool state.
|
|
114
|
+
def state = @state.value
|
|
115
|
+
|
|
116
|
+
# Return whether the pool rejects new tasks.
|
|
117
|
+
def closed? = state != :running
|
|
118
|
+
|
|
119
|
+
# Return whether the pool is draining queued work.
|
|
120
|
+
def closing? = state == :closing
|
|
121
|
+
|
|
122
|
+
# Return whether closing workers have drained all admitted submissions.
|
|
123
|
+
# @api private
|
|
124
|
+
def drained? = closing? && @queue.closed?
|
|
125
|
+
|
|
126
|
+
# Return whether all configured workers have been started.
|
|
127
|
+
def at_max_size? = size >= @max_size
|
|
128
|
+
|
|
129
|
+
# Return the generation used by workers waiting for admission.
|
|
130
|
+
# @api private
|
|
131
|
+
def admission_generation = @admission.generation
|
|
132
|
+
|
|
133
|
+
# Wait for worker capacity or a pool state change.
|
|
134
|
+
# @api private
|
|
135
|
+
def wait_for_admission(generation) = @admission.wait(generation)
|
|
136
|
+
|
|
137
|
+
# Wake workers waiting at their soft admission limit.
|
|
138
|
+
# @api private
|
|
139
|
+
def admission_changed = @admission.broadcast
|
|
140
|
+
|
|
141
|
+
# Record that a task left the shared queue.
|
|
142
|
+
# @api private
|
|
143
|
+
def task_taken
|
|
144
|
+
return if @pressure.value.nil? || at_max_size?
|
|
145
|
+
return unless queue_empty?
|
|
146
|
+
@pressure.value = nil
|
|
147
|
+
arm_scaler if @queue.size.positive?
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Reserve a worker retirement above the minimum size.
|
|
151
|
+
# @api private
|
|
152
|
+
def retire_worker
|
|
153
|
+
retired = false
|
|
154
|
+
@worker_count.update do |count|
|
|
155
|
+
if state == :running && count > @min_size && queue_empty?
|
|
156
|
+
retired = true
|
|
157
|
+
count - 1
|
|
158
|
+
else
|
|
159
|
+
count
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
retired
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Remove a stopped worker and preserve its failure.
|
|
166
|
+
# @api private
|
|
167
|
+
def worker_stopped(error, replace: true)
|
|
168
|
+
@worker_count.update { |count| count - 1 }
|
|
169
|
+
@error.compare_and_set(nil, Envelope.new(error)) if error
|
|
170
|
+
if closing?
|
|
171
|
+
if size.zero?
|
|
172
|
+
error && !drained? ? start_worker(drain: true) : finish_close
|
|
173
|
+
end
|
|
174
|
+
elsif state == :running && replace
|
|
175
|
+
begin
|
|
176
|
+
start_worker while size < @min_size
|
|
177
|
+
rescue Exception # rubocop:disable Lint/RescueException
|
|
178
|
+
nil
|
|
179
|
+
end
|
|
180
|
+
arm_scaler if @queue.size.positive?
|
|
181
|
+
end
|
|
182
|
+
admission_changed
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Recheck delayed queue pressure and grow by one worker.
|
|
186
|
+
# @api private
|
|
187
|
+
def scale_if_needed(started)
|
|
188
|
+
return unless state == :running && @pressure.value == started
|
|
189
|
+
age = @queue.oldest_age
|
|
190
|
+
return clear_pressure(started) unless age
|
|
191
|
+
return reschedule_scaler(started, @grow_after - age) if age < @grow_after
|
|
192
|
+
|
|
193
|
+
start_worker unless at_max_size?
|
|
194
|
+
return clear_pressure(started) if queue_empty? || at_max_size?
|
|
195
|
+
|
|
196
|
+
next_started = @queue.generation
|
|
197
|
+
schedule_scaler(next_started) if @pressure.compare_and_set(started, next_started)
|
|
198
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
199
|
+
@error.compare_and_set(nil, Envelope.new(e))
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# @api private
|
|
203
|
+
def inspect_with(inspector)
|
|
204
|
+
super do
|
|
205
|
+
inspector.attribute(:state, state)
|
|
206
|
+
inspector.attribute(:backend, @backend)
|
|
207
|
+
inspector.attribute(:size, size)
|
|
208
|
+
inspector.attribute(:min_size, @min_size)
|
|
209
|
+
inspector.attribute(:max_size, @max_size)
|
|
210
|
+
inspector.attribute(:max_inflight, @max_inflight) if @max_inflight
|
|
211
|
+
yield if block_given?
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
private
|
|
216
|
+
|
|
217
|
+
def enqueue_task(non_block, *args, mode: :smart, auto_local: true, &block)
|
|
218
|
+
raise PoolClosedError, "cannot schedule task on a closed pool" if closed?
|
|
219
|
+
raise LocalJumpError, "Cannot schedule without a block" unless block
|
|
220
|
+
|
|
221
|
+
task = prepare_task(args, block, mode, auto_local)
|
|
222
|
+
return unless non_block ? @queue.try_push(task) : @queue.push(task)
|
|
223
|
+
arm_scaler
|
|
224
|
+
self
|
|
225
|
+
rescue ClosedQueueError
|
|
226
|
+
raise unless closed?
|
|
227
|
+
raise PoolClosedError, "cannot schedule task on a closed pool"
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def initialize_configuration(min_size:, max_size:, max_inflight:, grow_after:, shrink_after:, backend:)
|
|
231
|
+
@min_size = Integer(min_size)
|
|
232
|
+
@max_size = Integer(max_size)
|
|
233
|
+
@max_inflight = max_inflight && Integer(max_inflight)
|
|
234
|
+
@grow_after = duration(grow_after, :grow_after)
|
|
235
|
+
@shrink_after = duration(shrink_after, :shrink_after, nil_allowed: true)
|
|
236
|
+
@backend = backend
|
|
237
|
+
validate_sizes
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def prepare_constructor(constructor)
|
|
241
|
+
return constructor if !constructor || Ractor.shareable?(constructor)
|
|
242
|
+
Ractor.shareable_proc(&constructor)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def prepare_task(args, block, mode, _auto_local)
|
|
246
|
+
raise ArgumentError, "local transfer is not supported by a pool" if mode == :local
|
|
247
|
+
Internal::ScheduledTask.new(args, block, mode)
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def new_queue(capacity:) = Internal::Queue.new(capacity:, track_age: true)
|
|
251
|
+
def new_worker = Internal::PoolWorker.new(self)
|
|
252
|
+
def new_scheduler(**, &) = Farce::Scheduler.new(**, &)
|
|
253
|
+
def launch_scheduler(scheduler) = scheduler.launch_ractor
|
|
254
|
+
|
|
255
|
+
def validate_sizes
|
|
256
|
+
raise ArgumentError, "min_size must be non-negative" if @min_size.negative?
|
|
257
|
+
raise ArgumentError, "max_size must be positive" unless @max_size.positive?
|
|
258
|
+
raise ArgumentError, "min_size must not exceed max_size" if @min_size > @max_size
|
|
259
|
+
raise ArgumentError, "max_inflight must be positive" if @max_inflight && !@max_inflight.positive?
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def duration(value, name, nil_allowed: false)
|
|
263
|
+
return if value.nil? && nil_allowed
|
|
264
|
+
value = Float(value)
|
|
265
|
+
raise ArgumentError, "#{name} must be finite and non-negative" if !value.finite? || value.negative?
|
|
266
|
+
value
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def start_worker(drain: false, only_if_empty: false)
|
|
270
|
+
reserved = false
|
|
271
|
+
@worker_count.update do |count|
|
|
272
|
+
can_start = state == :running || (drain && closing? && !queue_empty?)
|
|
273
|
+
if can_start && count < @max_size && (!only_if_empty || count.zero?)
|
|
274
|
+
reserved = true
|
|
275
|
+
count + 1
|
|
276
|
+
else
|
|
277
|
+
count
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
return false unless reserved
|
|
281
|
+
|
|
282
|
+
worker = new_worker
|
|
283
|
+
scheduler = new_scheduler(
|
|
284
|
+
backend: @backend,
|
|
285
|
+
queue: @queue,
|
|
286
|
+
pool_worker: worker,
|
|
287
|
+
queue_owner: false,
|
|
288
|
+
&@constructor
|
|
289
|
+
)
|
|
290
|
+
launch_scheduler(scheduler)
|
|
291
|
+
admission_changed if at_max_size?
|
|
292
|
+
true
|
|
293
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
294
|
+
worker ? worker.stopped(e, replace: false) : @worker_count.update { |count| count - 1 }
|
|
295
|
+
raise
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def arm_scaler
|
|
299
|
+
return if at_max_size? || queue_empty? || state != :running
|
|
300
|
+
start_worker(only_if_empty: true) if size.zero?
|
|
301
|
+
return if at_max_size?
|
|
302
|
+
|
|
303
|
+
started = @queue.generation
|
|
304
|
+
age = @queue.oldest_age
|
|
305
|
+
|
|
306
|
+
return unless age
|
|
307
|
+
return unless @pressure.compare_and_set(nil, started)
|
|
308
|
+
|
|
309
|
+
remaining = @grow_after - age
|
|
310
|
+
remaining = 0 if remaining.negative?
|
|
311
|
+
|
|
312
|
+
schedule_scaler(started, remaining)
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
def schedule_scaler(started, delay = @grow_after)
|
|
316
|
+
Internal::PoolSupervisor.schedule(self, started, delay)
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def reschedule_scaler(started, delay)
|
|
320
|
+
next_started = @queue.generation
|
|
321
|
+
schedule_scaler(next_started, delay) if @pressure.compare_and_set(started, next_started)
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def clear_pressure(started)
|
|
325
|
+
return unless @pressure.compare_and_set(started, nil)
|
|
326
|
+
arm_scaler if @queue.size.positive?
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
def queue_empty? = @queue.size.zero? # rubocop:disable Style/ZeroLengthPredicate
|
|
330
|
+
|
|
331
|
+
def finish_close = @state.compare_and_set(:closing, :closed)
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
end
|