kino 0.7.1 → 0.8.0

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.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +5 -0
  3. data/CHANGELOG.md +286 -0
  4. data/Cargo.lock +209 -132
  5. data/README.md +269 -438
  6. data/doc/README.md +27 -2
  7. data/doc/architecture.md +862 -43
  8. data/doc/benchmarks.md +444 -214
  9. data/doc/configuration.md +215 -0
  10. data/doc/operations.md +258 -0
  11. data/doc/rails-on-ractors.md +161 -49
  12. data/doc/streaming.md +52 -0
  13. data/doc/websockets.md +469 -0
  14. data/doc/why-kino.md +6 -6
  15. data/doc/worker-loop.md +462 -0
  16. data/ext/kino/Cargo.toml +19 -3
  17. data/ext/kino/build.rs +32 -1
  18. data/ext/kino/clippy.toml +10 -0
  19. data/ext/kino/src/access_log.rs +70 -92
  20. data/ext/kino/src/control.rs +26 -685
  21. data/ext/kino/src/counters.rs +132 -0
  22. data/ext/kino/src/cpus.rs +1 -18
  23. data/ext/kino/src/dispatch/tests.rs +731 -0
  24. data/ext/kino/src/dispatch.rs +276 -0
  25. data/ext/kino/src/doorbell.rs +108 -0
  26. data/ext/kino/src/env_strings.rs +26 -30
  27. data/ext/kino/src/framing.rs +110 -0
  28. data/ext/kino/src/gvl.rs +292 -66
  29. data/ext/kino/src/hijack/tests.rs +416 -0
  30. data/ext/kino/src/hijack.rs +471 -0
  31. data/ext/kino/src/inline/answer.rs +488 -0
  32. data/ext/kino/src/inline/conn.rs +1124 -0
  33. data/ext/kino/src/inline/deadline.rs +421 -0
  34. data/ext/kino/src/inline/encode.rs +119 -0
  35. data/ext/kino/src/inline/io.rs +487 -0
  36. data/ext/kino/src/inline/mod.rs +766 -0
  37. data/ext/kino/src/inline/parity.rs +862 -0
  38. data/ext/kino/src/inline/parse.rs +131 -0
  39. data/ext/kino/src/inline/spin.rs +428 -0
  40. data/ext/kino/src/inline/testing.rs +628 -0
  41. data/ext/kino/src/io_shards.rs +240 -192
  42. data/ext/kino/src/lane_room.rs +99 -0
  43. data/ext/kino/src/lib.rs +139 -77
  44. data/ext/kino/src/listen.rs +180 -12
  45. data/ext/kino/src/log.rs +12 -47
  46. data/ext/kino/src/logsink.rs +3 -9
  47. data/ext/kino/src/mono.rs +12 -17
  48. data/ext/kino/src/pin.rs +3 -3
  49. data/ext/kino/src/queue/mod.rs +54 -0
  50. data/ext/kino/src/queue/take.rs +503 -0
  51. data/ext/kino/src/queue/testing.rs +107 -0
  52. data/ext/kino/src/queue/tests.rs +581 -0
  53. data/ext/kino/src/queue/worker.rs +501 -0
  54. data/ext/kino/src/registry.rs +168 -379
  55. data/ext/kino/src/request.rs +834 -235
  56. data/ext/kino/src/response.rs +532 -94
  57. data/ext/kino/src/server/accept.rs +322 -0
  58. data/ext/kino/src/server/config.rs +138 -0
  59. data/ext/kino/src/server/handle.rs +247 -0
  60. data/ext/kino/src/server/intake.rs +217 -0
  61. data/ext/kino/src/server/mod.rs +157 -0
  62. data/ext/kino/src/server/natives.rs +223 -0
  63. data/ext/kino/src/server/serve/intake_tests.rs +458 -0
  64. data/ext/kino/src/server/serve.rs +436 -0
  65. data/ext/kino/src/server/tests.rs +532 -0
  66. data/ext/kino/src/shared_gvl.rs +50 -0
  67. data/ext/kino/src/slot/lane.rs +138 -0
  68. data/ext/kino/src/slot/life.rs +111 -0
  69. data/ext/kino/src/slot/load.rs +193 -0
  70. data/ext/kino/src/slot/mod.rs +494 -0
  71. data/ext/kino/src/slot/park.rs +82 -0
  72. data/ext/kino/src/stats.rs +936 -0
  73. data/ext/kino/src/symbols.rs +66 -0
  74. data/ext/kino/src/test_support.rs +24 -14
  75. data/ext/kino/src/testing.rs +409 -0
  76. data/ext/kino/src/ticker.rs +87 -0
  77. data/ext/kino/src/timer.rs +4 -7
  78. data/ext/kino/src/tls.rs +101 -131
  79. data/ext/kino/src/typed_data.rs +131 -0
  80. data/ext/kino/src/upgrade.rs +77 -0
  81. data/ext/kino/src/ws/codec.rs +470 -0
  82. data/ext/kino/src/ws/drive.rs +600 -0
  83. data/ext/kino/src/ws/handshake.rs +267 -0
  84. data/ext/kino/src/ws/mod.rs +317 -0
  85. data/ext/kino/src/ws/server_tests.rs +759 -0
  86. data/ext/kino/src/ws/socket.rs +279 -0
  87. data/ext/kino/src/ws/stats.rs +88 -0
  88. data/ext/kino/src/ws/testing.rs +156 -0
  89. data/ext/kino/src/ws/topics.rs +550 -0
  90. data/lib/kino/cli.rb +100 -9
  91. data/lib/kino/configuration.rb +32 -10
  92. data/lib/kino/crash_backoff.rb +56 -0
  93. data/lib/kino/input.rb +36 -17
  94. data/lib/kino/log.rb +8 -1
  95. data/lib/kino/monitor.rb +1 -1
  96. data/lib/kino/pool.rb +169 -0
  97. data/lib/kino/pool_scaler.rb +12 -13
  98. data/lib/kino/quarantine_monitor.rb +3 -3
  99. data/lib/kino/ractor_supervisor.rb +201 -161
  100. data/lib/kino/server.rb +291 -185
  101. data/lib/kino/slot_bank.rb +21 -9
  102. data/lib/kino/stream.rb +2 -2
  103. data/lib/kino/teardown.rb +33 -0
  104. data/lib/kino/templates/kino.rb.tt +41 -6
  105. data/lib/kino/threaded_pool.rb +87 -102
  106. data/lib/kino/version.rb +1 -1
  107. data/lib/kino/web_socket.rb +125 -0
  108. data/lib/kino/worker/loop.rb +319 -0
  109. data/lib/kino/worker.rb +345 -79
  110. data/lib/kino/worker_hooks.rb +7 -1
  111. data/lib/kino/worker_row.rb +15 -0
  112. data/lib/kino.rb +27 -3
  113. data/lib/rackup/handler/kino.rb +1 -1
  114. data/sig/kino.rbs +93 -10
  115. metadata +66 -3
  116. data/ext/kino/src/queue.rs +0 -333
  117. data/ext/kino/src/server.rs +0 -1785
@@ -3,65 +3,26 @@
3
3
  module Kino
4
4
  # @private
5
5
  # Spawns worker ractors and keeps them alive. One supervisor thread per
6
- # ractor: it blocks in Ractor#value, and a crash (anything that kills the
7
- # ractor, Exception from app code included) wakes it to 500 the in-flight
8
- # requests and respawn. Clean exits (queue drained at shutdown, or the
9
- # worker retired by the pool scaler) end supervision.
6
+ # ractor: a crashed worker thread is replaced on a bank slot while its
7
+ # siblings serve on, and a dead ractor's requests get their 500 before it
8
+ # respawns, both paced by a CrashBackoff.
10
9
  #
11
- # Also the :ractor-mode pool behind PoolScaler: `grow` adds a worker,
12
- # `retire` sends one home, `groups` lists the ones that may be retired,
13
- # and `active_count` is what the control plane reports.
14
- class RactorSupervisor
15
- def initialize(server_id, app, workers:, threads:, batch: 1, hooks: nil, on_worker_exit: nil)
16
- @server_id = server_id
17
- @app = app
18
- @workers = workers
19
- @threads = threads
20
- @batch = batch
21
- @hooks = hooks
22
- @on_worker_exit = on_worker_exit
23
- @draining = false
24
- @lock = Mutex.new
10
+ # The :ractor-mode Pool.
11
+ class RactorSupervisor < Pool
12
+ MAIN_KILLED = "the worker ractor's main thread was killed"
13
+ # Seconds a worker ractor's main thread, ending early, gives its threads to end (.leave).
14
+ LEAVE = 1.0
15
+ # A worker thread's crash report, sent to its supervisor's port (.report_crash). The only
16
+ # other message there is Ractor#monitor's end notice (a Symbol up to Ruby 4.0, an Array from
17
+ # 4.1): a class of its own keeps the two apart.
18
+ Crash = Data.define(:position, :id, :error, :backtrace, :started_at, :crashed_at, :reply)
19
+
20
+ def initialize(...)
21
+ super
25
22
  @supervisor_threads = []
26
- @worker_slots = {}
27
- @slot_to_worker = {}
28
23
  @replaced = {}
29
- # Worker index => true while its ractor runs, and => true once the
30
- # scaler asked it to leave. Retired workers hand their slots back to
31
- # the bank for the next worker to take over.
24
+ # Worker index => true while its Ractor runs.
32
25
  @live = {}
33
- @retiring = {}
34
- @bank = SlotBank.new(server_id)
35
- # The first replacement's index; `replace` increments before using it,
36
- # so this starts one below the first free index (@workers).
37
- @next_worker_index = @workers - 1
38
- end
39
-
40
- def start
41
- @supervisor_threads = Array.new(@workers) { |index| supervise(index) }
42
- report_active
43
- self
44
- end
45
-
46
- # Flag the drain and join supervisors up to the (numeric) deadline;
47
- # callers wanting an unbounded wait use #join instead.
48
- def shutdown(timeout)
49
- @lock.synchronize { @draining = true }
50
- deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
51
- @lock.synchronize { @supervisor_threads.dup }.each do |thread|
52
- remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
53
- thread.join([remaining, 0.01].max)
54
- end
55
- end
56
-
57
- def done?
58
- @lock.synchronize { @supervisor_threads.dup }.none?(&:alive?)
59
- end
60
-
61
- # Block until the workers exit on their own (drain elsewhere): join
62
- # without flipping the draining flag.
63
- def join
64
- @lock.synchronize { @supervisor_threads.dup }.each(&:join)
65
26
  end
66
27
 
67
28
  # Ractors cannot be force-killed; their clients were already freed by
@@ -87,162 +48,241 @@ module Kino
87
48
  # yet is not listed until it has).
88
49
  def groups
89
50
  @lock.synchronize do
90
- @live.keys
91
- .reject { |index| @retiring.key?(index) || @replaced.key?(index) || !@worker_slots.key?(index) }
92
- .to_h { |index| [index, @worker_slots[index].dup] }
51
+ @slots.slice(*@live.keys).reject { |index, _| @retiring.key?(index) || @replaced.key?(index) }
93
52
  end
94
53
  end
95
54
 
96
- # Add one supervised worker; returns its index.
97
- def grow
98
- new_index = @lock.synchronize { @next_worker_index += 1 }
99
- thread = supervise(new_index)
100
- @lock.synchronize { @supervisor_threads << thread }
101
- report_active
102
- new_index
103
- end
104
-
105
- # Send a worker home. Its slots stop receiving work now; the worker
106
- # finishes what it holds, leaves at its next idle tick, and its slots
107
- # come back to the free list once the ractor has exited. Returns
108
- # false when there is no such live worker to retire.
109
- def retire(worker_index)
110
- slot_ids = @lock.synchronize do
111
- next nil unless @live.key?(worker_index) && !@retiring.key?(worker_index)
55
+ # Replace the Ractor on slot `worker_id` at `generation`: spawn a fresh one, then quarantine
56
+ # the old one's slots; the old Ractor leaks, unrespawned. False for a slot that moved on.
57
+ def replace(worker_id, generation)
58
+ # Checked and claimed at once, so a slot that moves on cannot get a healthy Ractor
59
+ # quarantined; the slots are marked then (see Pool). A stale snapshot replaces a Ractor once.
60
+ worker_index, slot_ids = @lock.synchronize do
61
+ index = @owner[worker_id]
62
+ next unless index && @bank.generation(worker_id) == generation && !@replaced.key?(index)
112
63
 
113
- @retiring[worker_index] = true
114
- @worker_slots[worker_index]
64
+ @replaced[index] = true
65
+ @slots[index].each { |id| @quarantined[id] = true }
66
+ [index, @slots[index].map { |id| [id, @bank.generation(id)] }]
115
67
  end
116
- return false unless slot_ids
117
-
118
- slot_ids.each { |id| Native.retire_slot(@server_id, id) }
119
- true
120
- end
121
-
122
- # Replace the ractor owning slot `worker_id`: spawn a fresh supervised
123
- # ractor, then quarantine the old ractor's slots. The old supervisor
124
- # thread stays blocked in ractor.value on the wedged ractor (it and the
125
- # ractor leak until process exit; a wedged ractor cannot be
126
- # force-killed). Returns true if a replacement was spawned.
127
- def replace(worker_id)
128
- worker_index = @lock.synchronize { @slot_to_worker[worker_id] }
129
68
  return false unless worker_index
130
69
 
131
- # Idempotent per ractor: a stale monitor snapshot can list two sibling
132
- # slots of the same ractor, only the first replaces it.
133
- claimed = @lock.synchronize do
134
- if @replaced.key?(worker_index)
135
- false
136
- else
137
- @replaced[worker_index] = true
138
- true
139
- end
140
- end
141
- return false unless claimed
142
-
143
- new_index = @lock.synchronize { @next_worker_index += 1 }
144
70
  thread =
145
71
  begin
146
- supervise(new_index) # spawn FIRST, nothing quarantined yet
72
+ supervise(next_index) # spawn FIRST, nothing quarantined yet
147
73
  rescue
148
74
  @lock.synchronize { @replaced.delete(worker_index) } # allow retry next tick
75
+ unmark(slot_ids.map(&:first))
149
76
  raise
150
77
  end
151
- slot_ids = @lock.synchronize { @worker_slots[worker_index] } || []
152
- slot_ids.each { |id| Native.quarantine_slot(@server_id, id) } # quarantine only after success
78
+ # Quarantine only after success; each slot's sockets close with 1011.
79
+ slot_ids.each { |id, slot_generation| quarantine(id, slot_generation) }
153
80
  @lock.synchronize { @supervisor_threads << thread }
154
81
  true
155
82
  end
156
83
 
84
+ # @private
85
+ # A worker Ractor's main thread, one worker thread per slot; a crashed one waits for a new slot.
86
+ # True once no worker thread is left; nil when its main thread was killed.
87
+ def self.run_worker(server_id, ids, app, batch, hooks, supervisor)
88
+ # Crashes are the supervisor's to log; no thread here prints one.
89
+ Thread.current.report_on_exception = false
90
+ ended = Thread::Queue.new
91
+ # The worker threads, and the threads they start.
92
+ workers = ThreadGroup.new
93
+ # Up once this thread leaves: its worker threads end with it, reporting nothing.
94
+ over = left = false
95
+ start = lambda do |id, position|
96
+ Thread.new do
97
+ Thread.current.report_on_exception = false
98
+ Thread.current.name = "thread-#{position + 1}" if ids.size > 1
99
+ started_at = Kino.monotonic
100
+ error = fresh = nil
101
+ done = false
102
+ begin
103
+ Worker.run(server_id, id, app, batch, hooks)
104
+ done = true
105
+ rescue Exception => e # rubocop:disable Lint/RescueException -- reported, then replaced
106
+ error = e
107
+ ensure
108
+ # Neither its end nor an exception: its thread was killed.
109
+ error ||= Error.new(KILLED) unless done || over
110
+ fresh = report_crash(supervisor, position, id, error, started_at) if error && !over
111
+ replaced = begin
112
+ fresh && !over && start.call(fresh, position)
113
+ rescue ThreadError
114
+ nil
115
+ end
116
+ # A respawn counts once its thread runs (as the main thread is killed, the ractor's).
117
+ if replaced
118
+ Native.record_respawn(server_id)
119
+ else
120
+ ended << position
121
+ end
122
+ end
123
+ end.tap { |thread| workers.add(thread) }
124
+ end
125
+ ids.each_with_index { |id, position| start.call(id, position) }
126
+ ids.size.times { ended.pop }
127
+ left = true
128
+ ensure
129
+ over = true
130
+ leave(workers) unless left
131
+ end
132
+
133
+ # The main thread ends with worker threads still running: they end first, while the ractor
134
+ # lives, as a teardown's ensures run after Ractor#value hands their slots on, and must not touch
135
+ # a Ractor::Port. One past LEAVE goes with the teardown, its handle holding its slot.
136
+ def self.leave(workers)
137
+ deadline = Kino.monotonic + LEAVE
138
+ workers.list.each(&:kill)
139
+ workers.list.each do |thread|
140
+ Teardown.join(thread, [deadline - Kino.monotonic, 0].max)
141
+ end
142
+ end
143
+ private_class_method :leave
144
+
145
+ # Report a crash and return the answer; the exception crosses as a copy, its backtrace aside.
146
+ def self.report_crash(supervisor, position, id, error, started_at)
147
+ crashed_at = Kino.monotonic
148
+ reply = Ractor::Port.new
149
+ crash = Crash.new(position, id, error, error.backtrace, started_at, crashed_at, reply)
150
+ begin
151
+ supervisor << crash
152
+ rescue TypeError, Ractor::Error
153
+ supervisor << crash.with(error: Error.new("#{error.class}: #{error.message}"))
154
+ end
155
+ reply.receive
156
+ ensure
157
+ reply&.close
158
+ end
159
+ private_class_method :report_crash
160
+
157
161
  private
158
162
 
163
+ def add_worker
164
+ index = next_index
165
+ thread = supervise(index)
166
+ @lock.synchronize { @supervisor_threads << thread }
167
+ index
168
+ end
169
+
170
+ def join_set = @lock.synchronize { @supervisor_threads.dup }
171
+
172
+ def member?(index) = @live.key?(index)
173
+
159
174
  def supervise(index)
160
175
  # Live from the moment it is asked for, not from when its thread gets
161
176
  # around to spawning: `grow` reports the count right after this.
162
177
  @lock.synchronize { @live[index] = true }
163
178
  Thread.new do
164
179
  Thread.current.name = "supervisor-#{index}"
165
- crashes = 0
180
+ backoff = CrashBackoff.new("worker-#{index}")
166
181
  loop do
167
- ractor, worker_ids = spawn_worker(index)
168
- begin
169
- ractor.value # blocks until the ractor terminates
170
- HookFire.fire(@on_worker_exit, "on_worker_exit", index, nil) # clean exit: drained or retired
171
- exited(index, worker_ids)
172
- break
182
+ port = Ractor::Port.new
183
+ ractor = spawn_worker(index, port)
184
+ backoff.started
185
+ tend(index, ractor, port)
186
+ cause = begin
187
+ # A ractor whose main thread was killed ends with no value.
188
+ Error.new(MAIN_KILLED) unless ractor.value
173
189
  rescue Ractor::Error => e
174
- # The ractor died mid-flight. Anything it was serving will never
175
- # be answered by Ruby: 500 those clients NOW (not when GC gets
176
- # around to dropping the dead heap), then decide on respawn.
177
- worker_ids.each { |id| Native.abort_inflight(@server_id, id) }
178
- cause = (e.respond_to?(:cause) && e.cause) ? e.cause : e
179
- HookFire.fire(@on_worker_exit, "on_worker_exit", index, cause)
180
- if draining?
181
- exited(index, nil)
182
- break
183
- end
190
+ (e.respond_to?(:cause) && e.cause) ? e.cause : e
191
+ end
192
+ unless cause
193
+ worker_exited(index, nil) # drained or retired
194
+ exited(index, clean: true)
195
+ break
196
+ end
184
197
 
185
- crashes += 1
186
- Native.record_respawn(@server_id)
187
- Log.error("worker-#{index} crashed (#{cause.class}: #{cause.message}); respawning")
188
- # A crashed worker that was on its way out respawns on fresh
189
- # slots like any other; the scaler retires it again when idle.
190
- @lock.synchronize { @retiring.delete(index) }
191
- # Policy (crash recovery): unlimited respawn
192
- # keeps the server up under rare crashes but turns a
193
- # crash-on-every-request bug into a busy loop. A circuit breaker
194
- # (give up / cool down after N crashes in T seconds) trades
195
- # availability for fail-fast. Current policy: respawn forever.
198
+ # The ractor died mid-flight. Anything it was serving will never
199
+ # be answered by Ruby: 500 those clients NOW (not when GC gets
200
+ # around to dropping the dead heap), then decide on respawn.
201
+ # Its sockets close too: their handlers died with it.
202
+ dead = @lock.synchronize { @slots[index] }
203
+ dead.each { |id| Native.abort_inflight(@server_id, id) }
204
+ # A replaced worker is not respawned; a leaving one is, and the scaler retires it again.
205
+ respawn = recover(index, dead, cause, backoff, leaving_respawns: true) { worker_exited(index, cause) }
206
+ unless respawn
207
+ exited(index, clean: false)
208
+ break
196
209
  end
210
+
211
+ Native.record_respawn(@server_id)
212
+ @lock.synchronize { @retiring.delete(index) }
197
213
  end
198
214
  end
199
215
  end
200
216
 
217
+ # Answer each Crash on its own thread (each waits only its own delay) until the Ractor exits.
218
+ def tend(index, ractor, port)
219
+ ractor.monitor(port) # the notice comes after any crash report
220
+ backoffs = Array.new(@threads) { |position| CrashBackoff.for(index, position, @threads) }
221
+ run = {over: false}
222
+ while (crash = port.receive).is_a?(Crash)
223
+ Thread.new(crash) { |report| answer(index, report, backoffs, run) }
224
+ end
225
+ ensure
226
+ @lock.synchronize { run[:over] = true } if run
227
+ port.close
228
+ end
229
+
230
+ def answer(index, crash, backoffs, run)
231
+ Thread.current.name = "supervisor-#{index}"
232
+ cause = crash.error
233
+ # A copied exception arrives without its backtrace.
234
+ cause.set_backtrace(crash.backtrace) if crash.backtrace && !cause.frozen?
235
+ backoff = backoffs[crash.position]
236
+ backoff.started(crash.started_at)
237
+ crash.reply << replace_thread(index, crash.id, cause, backoff, crash.crashed_at, run)
238
+ rescue Ractor::ClosedError
239
+ nil # the ractor is gone; the supervisor sees to its slots
240
+ end
241
+
242
+ # Release the crashed thread's slot (unless wedged); after its backoff, a fresh one, if due.
243
+ def replace_thread(index, dead_id, cause, backoff, crashed_at, run)
244
+ return unless recover(index, [dead_id], cause, backoff, crashed_at) { worker_exited(index, cause) }
245
+
246
+ # The replacement counts as a respawn once it runs (run_worker).
247
+ claim_slot(index) { run[:over] || @retiring.key?(index) || @replaced.key?(index) }
248
+ rescue => e
249
+ replacement_failed(index, e)
250
+ nil
251
+ end
252
+
253
+ # Unmap before the bank may hand the slots on, so no generation pairs with a later worker.
254
+ def forget_slots(index, ids)
255
+ @lock.synchronize { [releasable(unmap(index, ids)), @replaced.key?(index), @retiring.key?(index)] }
256
+ end
257
+
201
258
  # Fresh ractor on slots from the bank: fresh ones, or ones a retired
202
- # worker handed back.
203
- def spawn_worker(worker_index)
259
+ # worker handed back. It asks for replacement threads on `port`.
260
+ def spawn_worker(worker_index, port)
204
261
  worker_ids = Array.new(@threads) { @bank.claim }
205
262
  @lock.synchronize do
206
- @worker_slots[worker_index] = worker_ids
207
- worker_ids.each { |id| @slot_to_worker[id] = worker_index }
263
+ @slots[worker_index] = worker_ids
264
+ worker_ids.each { |id| @owner[id] = worker_index }
208
265
  end
209
266
  # Named so log lines from inside say which worker spoke: the ractor
210
267
  # alone for a single thread, `worker-N/thread-M` for more.
211
- ractor = Ractor.new(@server_id, worker_ids, @app, @batch, @hooks,
212
- name: "worker-#{worker_index}") do |server_id, ids, app, batch, hooks|
213
- ids.each_with_index.map do |id, position|
214
- Thread.new do
215
- # Crashes surface via Ractor#value in the supervisor; don't also
216
- # spray the backtrace to stderr from inside the dying ractor.
217
- Thread.current.report_on_exception = false
218
- Thread.current.name = "thread-#{position + 1}" if ids.size > 1
219
- Kino::Worker.run(server_id, id, app, batch, hooks)
220
- end
221
- end.each(&:join)
268
+ Ractor.new(@server_id, worker_ids, @app, @batch, @hooks, port, name: "worker-#{worker_index}") do |*worker|
269
+ Kino::RactorSupervisor.run_worker(*worker)
222
270
  end
223
- [ractor, worker_ids]
224
271
  end
225
272
 
226
273
  # Bookkeeping for a supervisor thread that is done: the worker is no
227
274
  # longer live, a retired worker's slots go back to the bank, and the
228
275
  # thread leaves the join set so a long-lived elastic pool does not
229
276
  # accumulate dead threads.
230
- def exited(index, worker_ids)
231
- retired = @lock.synchronize do
277
+ def exited(index, clean:)
278
+ released = @lock.synchronize do
232
279
  @live.delete(index)
233
280
  @supervisor_threads.delete(Thread.current)
234
- @retiring.delete(index)
281
+ retired = @retiring.delete(index)
282
+ (retired && clean) ? releasable(@slots[index]) : []
235
283
  end
236
- @bank.release(worker_ids) if retired && worker_ids
284
+ @bank.release(released)
237
285
  report_active
238
286
  end
239
-
240
- def report_active
241
- Native.set_active_workers(@server_id, active_count)
242
- end
243
-
244
- def draining?
245
- @lock.synchronize { @draining }
246
- end
247
287
  end
248
288
  end