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.
- checksums.yaml +4 -4
- data/.yardopts +5 -0
- data/CHANGELOG.md +286 -0
- data/Cargo.lock +209 -132
- data/README.md +269 -438
- data/doc/README.md +27 -2
- data/doc/architecture.md +862 -43
- data/doc/benchmarks.md +444 -214
- data/doc/configuration.md +215 -0
- data/doc/operations.md +258 -0
- data/doc/rails-on-ractors.md +161 -49
- data/doc/streaming.md +52 -0
- data/doc/websockets.md +469 -0
- data/doc/why-kino.md +6 -6
- data/doc/worker-loop.md +462 -0
- data/ext/kino/Cargo.toml +19 -3
- data/ext/kino/build.rs +32 -1
- data/ext/kino/clippy.toml +10 -0
- data/ext/kino/src/access_log.rs +70 -92
- data/ext/kino/src/control.rs +26 -685
- data/ext/kino/src/counters.rs +132 -0
- data/ext/kino/src/cpus.rs +1 -18
- data/ext/kino/src/dispatch/tests.rs +731 -0
- data/ext/kino/src/dispatch.rs +276 -0
- data/ext/kino/src/doorbell.rs +108 -0
- data/ext/kino/src/env_strings.rs +26 -30
- data/ext/kino/src/framing.rs +110 -0
- data/ext/kino/src/gvl.rs +292 -66
- data/ext/kino/src/hijack/tests.rs +416 -0
- data/ext/kino/src/hijack.rs +471 -0
- data/ext/kino/src/inline/answer.rs +488 -0
- data/ext/kino/src/inline/conn.rs +1124 -0
- data/ext/kino/src/inline/deadline.rs +421 -0
- data/ext/kino/src/inline/encode.rs +119 -0
- data/ext/kino/src/inline/io.rs +487 -0
- data/ext/kino/src/inline/mod.rs +766 -0
- data/ext/kino/src/inline/parity.rs +862 -0
- data/ext/kino/src/inline/parse.rs +131 -0
- data/ext/kino/src/inline/spin.rs +428 -0
- data/ext/kino/src/inline/testing.rs +628 -0
- data/ext/kino/src/io_shards.rs +240 -192
- data/ext/kino/src/lane_room.rs +99 -0
- data/ext/kino/src/lib.rs +139 -77
- data/ext/kino/src/listen.rs +180 -12
- data/ext/kino/src/log.rs +12 -47
- data/ext/kino/src/logsink.rs +3 -9
- data/ext/kino/src/mono.rs +12 -17
- data/ext/kino/src/pin.rs +3 -3
- data/ext/kino/src/queue/mod.rs +54 -0
- data/ext/kino/src/queue/take.rs +503 -0
- data/ext/kino/src/queue/testing.rs +107 -0
- data/ext/kino/src/queue/tests.rs +581 -0
- data/ext/kino/src/queue/worker.rs +501 -0
- data/ext/kino/src/registry.rs +168 -379
- data/ext/kino/src/request.rs +834 -235
- data/ext/kino/src/response.rs +532 -94
- data/ext/kino/src/server/accept.rs +322 -0
- data/ext/kino/src/server/config.rs +138 -0
- data/ext/kino/src/server/handle.rs +247 -0
- data/ext/kino/src/server/intake.rs +217 -0
- data/ext/kino/src/server/mod.rs +157 -0
- data/ext/kino/src/server/natives.rs +223 -0
- data/ext/kino/src/server/serve/intake_tests.rs +458 -0
- data/ext/kino/src/server/serve.rs +436 -0
- data/ext/kino/src/server/tests.rs +532 -0
- data/ext/kino/src/shared_gvl.rs +50 -0
- data/ext/kino/src/slot/lane.rs +138 -0
- data/ext/kino/src/slot/life.rs +111 -0
- data/ext/kino/src/slot/load.rs +193 -0
- data/ext/kino/src/slot/mod.rs +494 -0
- data/ext/kino/src/slot/park.rs +82 -0
- data/ext/kino/src/stats.rs +936 -0
- data/ext/kino/src/symbols.rs +66 -0
- data/ext/kino/src/test_support.rs +24 -14
- data/ext/kino/src/testing.rs +409 -0
- data/ext/kino/src/ticker.rs +87 -0
- data/ext/kino/src/timer.rs +4 -7
- data/ext/kino/src/tls.rs +101 -131
- data/ext/kino/src/typed_data.rs +131 -0
- data/ext/kino/src/upgrade.rs +77 -0
- data/ext/kino/src/ws/codec.rs +470 -0
- data/ext/kino/src/ws/drive.rs +600 -0
- data/ext/kino/src/ws/handshake.rs +267 -0
- data/ext/kino/src/ws/mod.rs +317 -0
- data/ext/kino/src/ws/server_tests.rs +759 -0
- data/ext/kino/src/ws/socket.rs +279 -0
- data/ext/kino/src/ws/stats.rs +88 -0
- data/ext/kino/src/ws/testing.rs +156 -0
- data/ext/kino/src/ws/topics.rs +550 -0
- data/lib/kino/cli.rb +100 -9
- data/lib/kino/configuration.rb +32 -10
- data/lib/kino/crash_backoff.rb +56 -0
- data/lib/kino/input.rb +36 -17
- data/lib/kino/log.rb +8 -1
- data/lib/kino/monitor.rb +1 -1
- data/lib/kino/pool.rb +169 -0
- data/lib/kino/pool_scaler.rb +12 -13
- data/lib/kino/quarantine_monitor.rb +3 -3
- data/lib/kino/ractor_supervisor.rb +201 -161
- data/lib/kino/server.rb +291 -185
- data/lib/kino/slot_bank.rb +21 -9
- data/lib/kino/stream.rb +2 -2
- data/lib/kino/teardown.rb +33 -0
- data/lib/kino/templates/kino.rb.tt +41 -6
- data/lib/kino/threaded_pool.rb +87 -102
- data/lib/kino/version.rb +1 -1
- data/lib/kino/web_socket.rb +125 -0
- data/lib/kino/worker/loop.rb +319 -0
- data/lib/kino/worker.rb +345 -79
- data/lib/kino/worker_hooks.rb +7 -1
- data/lib/kino/worker_row.rb +15 -0
- data/lib/kino.rb +27 -3
- data/lib/rackup/handler/kino.rb +1 -1
- data/sig/kino.rbs +93 -10
- metadata +66 -3
- data/ext/kino/src/queue.rs +0 -333
- data/ext/kino/src/server.rs +0 -1785
data/lib/kino/server.rb
CHANGED
|
@@ -59,6 +59,8 @@ module Kino
|
|
|
59
59
|
# @example
|
|
60
60
|
# Kino::Server.new(app, config_file: "kino.rb", port: 3000)
|
|
61
61
|
def initialize(app, config_file: nil, **options)
|
|
62
|
+
# Internal: false serves worker_loop connections through hyper, for specs of that path.
|
|
63
|
+
@inline = options.delete(:_inline) != false
|
|
62
64
|
config = Configuration.new
|
|
63
65
|
config.load_file(config_file) if config_file
|
|
64
66
|
config.merge!(options)
|
|
@@ -67,81 +69,20 @@ module Kino
|
|
|
67
69
|
@app = app
|
|
68
70
|
@bind = settings[:bind]
|
|
69
71
|
@requested_port = settings[:port]
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
end
|
|
76
|
-
@scale_down_after = settings[:scale_down_after].nil? ? 30.0 : Float(settings[:scale_down_after])
|
|
77
|
-
raise ArgumentError, "scale_down_after must be positive" unless @scale_down_after.positive?
|
|
78
|
-
if !settings[:scale_down_after].nil? && !elastic?
|
|
79
|
-
Log.warn("scale_down_after has no effect unless max_workers is above workers")
|
|
80
|
-
end
|
|
81
|
-
@on_error = validate_hook(settings[:on_error], :on_error)
|
|
82
|
-
@after_worker_boot = validate_hook(settings[:after_worker_boot], :after_worker_boot)
|
|
83
|
-
@after_request_complete = validate_hook(settings[:after_request_complete], :after_request_complete)
|
|
84
|
-
@after_boot = validate_hook(settings[:after_boot], :after_boot)
|
|
85
|
-
@on_worker_exit = validate_hook(settings[:on_worker_exit], :on_worker_exit)
|
|
86
|
-
@mode = resolve_mode(settings[:mode])
|
|
87
|
-
@worker_hooks = WorkerHooks.new(
|
|
88
|
-
on_error: @on_error,
|
|
89
|
-
after_worker_boot: @after_worker_boot,
|
|
90
|
-
after_request_complete: @after_request_complete,
|
|
91
|
-
# The access log's GC and allocation figures come from the VM's
|
|
92
|
-
# process-wide counters, so they are measured only where one
|
|
93
|
-
# request at a time can own them: the GVL serializes :threaded
|
|
94
|
-
# mode, and a single ractor (a pool that can never grow past one)
|
|
95
|
-
# has nothing to race.
|
|
96
|
-
access_timing: !!settings[:log_requests] && (@mode == :threaded || @max_workers == 1)
|
|
97
|
-
)
|
|
98
|
-
# Default threads per mode: 1 in :ractor (threads inside a ractor
|
|
99
|
-
# share its lock; a measured +17% on fast handlers; raise `workers`
|
|
100
|
-
# for I/O concurrency instead), 3 in :threaded (threads ARE the
|
|
101
|
-
# concurrency there).
|
|
102
|
-
@threads = Integer(settings[:threads] || ((@mode == :ractor) ? 1 : 3))
|
|
103
|
-
@queue_depth = Integer(settings[:queue_depth])
|
|
104
|
-
@queue_timeout_ms = (Float(settings[:queue_timeout]) * 1000).round
|
|
105
|
-
@request_timeout_ms = settings[:request_timeout] ? (Float(settings[:request_timeout]) * 1000).round : 0
|
|
106
|
-
@max_connections = settings[:max_connections] ? Integer(settings[:max_connections]) : default_max_connections
|
|
107
|
-
@max_body_size = Integer(settings[:max_body_size] || 0)
|
|
108
|
-
@batch = [Integer(settings[:batch]), 1].max
|
|
109
|
-
@lanes = !!settings[:lanes]
|
|
110
|
-
@log_requests = !!settings[:log_requests]
|
|
111
|
-
@shutdown_timeout = settings[:shutdown_timeout]
|
|
112
|
-
@io_shards = !!settings[:io_shards]
|
|
113
|
-
@io_threads = Integer(settings[:io_threads]) unless settings[:io_threads].nil?
|
|
114
|
-
if @io_threads && @io_threads < 1
|
|
115
|
-
raise ArgumentError, "io_threads must be >= 1"
|
|
116
|
-
end
|
|
117
|
-
Log.warn("io_threads has no effect unless io_shards is true") if @io_threads && !@io_shards
|
|
118
|
-
@tokio_threads = settings[:tokio_threads]
|
|
119
|
-
@tls = validate_tls(settings[:tls])
|
|
120
|
-
if @tls && unix?
|
|
121
|
-
raise ArgumentError, "TLS is not supported on a unix socket bind; terminate TLS at the proxy in front"
|
|
122
|
-
end
|
|
123
|
-
@http2 = settings.fetch(:http2, true) ? true : false
|
|
124
|
-
@pidfile = settings[:pidfile]
|
|
125
|
-
@control_bind = settings[:control_bind]&.to_s
|
|
126
|
-
@control_token = settings[:control_token]&.to_s
|
|
127
|
-
# An empty token (e.g. control_token ENV["KINO_CONTROL_TOKEN"] with the
|
|
128
|
-
# var unset) must not half-disable auth: treat it as auth off, not as
|
|
129
|
-
# "require a zero-length Bearer token".
|
|
130
|
-
@control_token = nil if @control_token && @control_token.empty?
|
|
131
|
-
@quarantine_timeout_ms = settings[:quarantine_timeout] ? (Float(settings[:quarantine_timeout]) * 1000).round : nil
|
|
132
|
-
@quarantine_max =
|
|
133
|
-
if settings[:quarantine_max]
|
|
134
|
-
Integer(settings[:quarantine_max])
|
|
135
|
-
elsif @mode == :ractor
|
|
136
|
-
@workers
|
|
137
|
-
else
|
|
138
|
-
@workers * @threads
|
|
139
|
-
end
|
|
140
|
-
@supervisor = nil
|
|
141
|
-
@threaded_pool = nil
|
|
72
|
+
configure_pool(settings)
|
|
73
|
+
configure_limits(settings)
|
|
74
|
+
configure_io(settings)
|
|
75
|
+
configure_quarantine(settings)
|
|
76
|
+
@pool = nil
|
|
142
77
|
@quarantine_monitor = nil
|
|
143
78
|
@pool_scaler = nil
|
|
144
79
|
@started = false
|
|
80
|
+
# Under @lifecycle: @stopping while a shutdown runs, @shutdown_error
|
|
81
|
+
# from the last one that failed; @shutdown_ended signals each end.
|
|
82
|
+
@lifecycle = Mutex.new
|
|
83
|
+
@shutdown_ended = ConditionVariable.new
|
|
84
|
+
@stopping = false
|
|
85
|
+
@shutdown_error = nil
|
|
145
86
|
end
|
|
146
87
|
|
|
147
88
|
# Bind, boot the native front-end, and spawn the worker pool.
|
|
@@ -167,8 +108,11 @@ module Kino
|
|
|
167
108
|
io_threads: @io_threads,
|
|
168
109
|
tokio_threads: @tokio_threads,
|
|
169
110
|
tls_cert: @tls&.fetch(:cert), tls_key: @tls&.fetch(:key),
|
|
170
|
-
http2: @http2,
|
|
171
|
-
|
|
111
|
+
http2: @http2, lanes: @lanes, worker_loop: @worker_loop, log_requests: @log_requests,
|
|
112
|
+
# With one usable CPU a spinner would hold the CPU its client's request needs.
|
|
113
|
+
inline: @inline, inline_spin: Kino.available_parallelism >= 2,
|
|
114
|
+
websocket_max_message: @websocket_max_message, websocket_max: @websocket_max,
|
|
115
|
+
websocket_ping_interval_ms: @websocket_ping_interval_ms,
|
|
172
116
|
mode: @mode.to_s, workers: @workers, max_workers: @max_workers,
|
|
173
117
|
threads: @threads, batch: @batch,
|
|
174
118
|
control_bind: @control_bind, control_token: @control_token
|
|
@@ -180,85 +124,85 @@ module Kino
|
|
|
180
124
|
# GC anchor for zero-copy response buffers: held for the server's
|
|
181
125
|
# lifetime so in-flight buffers survive even a worker ractor crash.
|
|
182
126
|
@pin_keeper = Native.pin_keeper(@id)
|
|
183
|
-
if @mode == :ractor
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
batch: @batch, hooks: @worker_hooks, on_worker_exit: @on_worker_exit).start
|
|
187
|
-
else
|
|
188
|
-
@threaded_pool = ThreadedPool.new(@id, @app, threads: @threads, batch: @batch,
|
|
189
|
-
hooks: @worker_hooks, on_worker_exit: @on_worker_exit).start(@workers)
|
|
190
|
-
end
|
|
127
|
+
warn_scheduler_cap if @mode == :ractor
|
|
128
|
+
@pool = ((@mode == :ractor) ? RactorSupervisor : ThreadedPool).new(@id, @app, workers: @workers,
|
|
129
|
+
threads: @threads, batch: @batch, hooks: @worker_hooks, on_worker_exit: @on_worker_exit).start
|
|
191
130
|
start_quarantine_monitor if @quarantine_timeout_ms
|
|
192
131
|
start_pool_scaler if elastic?
|
|
193
132
|
Native.control_ready(@id)
|
|
194
133
|
HookFire.fire(@after_boot, "after_boot")
|
|
195
|
-
@
|
|
134
|
+
@lifecycle.synchronize do
|
|
135
|
+
@shutdown_error = nil
|
|
136
|
+
@started = true
|
|
137
|
+
end
|
|
196
138
|
self
|
|
197
139
|
end
|
|
198
140
|
|
|
199
|
-
# Graceful shutdown: stop accepting,
|
|
200
|
-
# deadline, then escalate: abort remaining clients (500), interrupt
|
|
141
|
+
# Graceful shutdown: stop accepting, close WebSockets (1001, after on_shutdown), drain
|
|
142
|
+
# in-flight work up to the deadline, then escalate: abort remaining clients (500), interrupt
|
|
201
143
|
# blocked workers, kill stragglers; and tear down the runtime. Always
|
|
202
|
-
# returns by ~deadline + a small epsilon; idempotent.
|
|
144
|
+
# returns by ~deadline + a small epsilon; idempotent. One runs at a
|
|
145
|
+
# time: a call made during another waits for it to end, then returns
|
|
146
|
+
# as #wait does, or shuts down itself if the server still serves. A
|
|
147
|
+
# failure before it stops accepting leaves the server serving; a later
|
|
148
|
+
# one leaves it stopped. Either way the error is raised here and by
|
|
149
|
+
# #wait.
|
|
203
150
|
#
|
|
204
|
-
# @param timeout [Numeric, nil] drain deadline in seconds
|
|
151
|
+
# @param timeout [Numeric, nil] drain deadline in seconds, not negative;
|
|
152
|
+
# Float::INFINITY waits for the drain however long it takes (default:
|
|
205
153
|
# the configured shutdown_timeout)
|
|
206
154
|
# @return [nil]
|
|
155
|
+
# @raise [ArgumentError] when timeout is NaN, negative or not a number,
|
|
156
|
+
# whether the server runs or not
|
|
207
157
|
def shutdown(timeout: nil)
|
|
158
|
+
seconds = timeout.nil? ? @shutdown_timeout : drain_seconds(timeout, :timeout)
|
|
208
159
|
return unless @started
|
|
160
|
+
return wait unless claim_shutdown
|
|
209
161
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
join_workers(deadline)
|
|
226
|
-
|
|
227
|
-
unless workers_done?
|
|
228
|
-
# Past the deadline with stuck handlers: free the clients first,
|
|
229
|
-
# then try to unblock and reap the workers.
|
|
230
|
-
Native.abort_all_inflight(@id)
|
|
231
|
-
Native.interrupt_all_workers(@id)
|
|
232
|
-
join_workers(monotonic_now + 0.2)
|
|
233
|
-
kill_stragglers
|
|
162
|
+
stopped_accepting = false
|
|
163
|
+
failure = nil
|
|
164
|
+
begin
|
|
165
|
+
deadline = Kino.monotonic + seconds
|
|
166
|
+
@pool_scaler&.stop
|
|
167
|
+
@quarantine_monitor&.stop
|
|
168
|
+
Native.stop_accepting(@id)
|
|
169
|
+
stopped_accepting = true
|
|
170
|
+
drain(deadline)
|
|
171
|
+
stop_workers(deadline)
|
|
172
|
+
tear_down
|
|
173
|
+
rescue Exception => failure # rubocop:disable Lint/RescueException -- recorded for #wait, then re-raised
|
|
174
|
+
raise
|
|
175
|
+
ensure
|
|
176
|
+
end_shutdown(failure, stopped: stopped_accepting)
|
|
234
177
|
end
|
|
235
|
-
|
|
236
|
-
Native.shutdown_runtime(@id, 1_000)
|
|
237
|
-
# The control thread reports "draining" for the whole drain and stops
|
|
238
|
-
# only now, once there is nothing left to report.
|
|
239
|
-
Native.control_stop(@id)
|
|
240
|
-
# The runtime is gone, so hyper has dropped every pinned buffer;
|
|
241
|
-
# the keeper (and the strings it marked) may now be collected.
|
|
242
|
-
@pin_keeper = nil
|
|
243
|
-
@started = false
|
|
244
|
-
remove_pidfile if @pidfile
|
|
245
178
|
nil
|
|
246
179
|
end
|
|
247
180
|
|
|
248
|
-
# Block until
|
|
181
|
+
# Block until a #shutdown, from a signal or any thread, has ended; at
|
|
182
|
+
# once when the server is not running.
|
|
183
|
+
#
|
|
249
184
|
# @return [void]
|
|
185
|
+
# @raise [Exception] what the shutdown raised, when it failed
|
|
250
186
|
def wait
|
|
251
|
-
|
|
187
|
+
failure = @lifecycle.synchronize do
|
|
188
|
+
@shutdown_ended.wait(@lifecycle) while @started && !@shutdown_error
|
|
189
|
+
@shutdown_error
|
|
190
|
+
end
|
|
191
|
+
raise failure if failure
|
|
252
192
|
end
|
|
253
193
|
|
|
254
194
|
# Production entry point: build the server and {#run} it. The `kino`
|
|
255
195
|
# CLI funnels into this too (CLI#serve).
|
|
256
196
|
#
|
|
257
197
|
# @param app [#call] a Rack 3 application
|
|
198
|
+
# @param credits [Boolean] print the opening credits (the CLI prints them
|
|
199
|
+
# itself, ahead of loading the app, and passes false)
|
|
200
|
+
# @param on_ready [#call, nil] called once the ready block is out; the
|
|
201
|
+
# CLI's seam for replaying held-back app output
|
|
258
202
|
# @param opts [Hash] see #initialize
|
|
259
203
|
# @return [Kino::Server] the (stopped) server, after shutdown
|
|
260
|
-
def self.run(app, **opts)
|
|
261
|
-
new(app, **opts).run
|
|
204
|
+
def self.run(app, credits: true, on_ready: nil, **opts)
|
|
205
|
+
new(app, **opts).run(credits: credits, on_ready: on_ready)
|
|
262
206
|
end
|
|
263
207
|
|
|
264
208
|
# Serve until shut down: start, print the banner, trap INT/TERM for
|
|
@@ -266,17 +210,19 @@ module Kino
|
|
|
266
210
|
# The Rack handler calls this on a server it built itself.
|
|
267
211
|
#
|
|
268
212
|
# @return [self] after shutdown
|
|
269
|
-
|
|
213
|
+
# @raise [Exception] what a failed shutdown raised
|
|
214
|
+
def run(credits: true, on_ready: nil)
|
|
270
215
|
# Startup output must land immediately even when stdout is a pipe or
|
|
271
216
|
# file (process supervisors, `kino > server.log`, `rails server`
|
|
272
217
|
# under Docker); block buffering would hold the banner back until
|
|
273
218
|
# exit.
|
|
274
219
|
$stdout.sync = true
|
|
275
|
-
CLI.opening_credits
|
|
220
|
+
CLI.opening_credits if credits
|
|
276
221
|
start
|
|
277
222
|
CLI.action!(self)
|
|
278
223
|
CLI.fin_at_exit
|
|
279
224
|
self.class.trap_signals(self)
|
|
225
|
+
on_ready&.call
|
|
280
226
|
wait
|
|
281
227
|
self
|
|
282
228
|
end
|
|
@@ -299,56 +245,172 @@ module Kino
|
|
|
299
245
|
signaled = true
|
|
300
246
|
Log.warn("draining (signal again to force exit)")
|
|
301
247
|
# Trap context forbids mutexes; do the real work on a thread.
|
|
302
|
-
|
|
248
|
+
# Its error is reported once: #wait raises it on the main thread.
|
|
249
|
+
Thread.new do
|
|
250
|
+
Thread.current.report_on_exception = false
|
|
251
|
+
server.shutdown
|
|
252
|
+
end
|
|
303
253
|
end
|
|
304
254
|
end
|
|
305
255
|
end
|
|
306
256
|
|
|
307
257
|
# Live snapshot. Counters come from the native layer (one relaxed
|
|
308
258
|
# atomic per request); config echo makes the line self-describing.
|
|
259
|
+
# Keys in /stats's order; :responses_cut and :websockets cover the whole process.
|
|
309
260
|
#
|
|
310
|
-
# @return [Hash{Symbol => Object}]
|
|
311
|
-
#
|
|
312
|
-
#
|
|
313
|
-
# lanes mode) once started
|
|
261
|
+
# @return [Hash{Symbol => Object}] while it serves, every /stats key but state
|
|
262
|
+
# and version; otherwise the config echo: mode, lanes, workers, threads, batch,
|
|
263
|
+
# respawns, max_workers, active_workers, scale_ups and scale_downs
|
|
314
264
|
def stats
|
|
315
|
-
|
|
316
|
-
mode: @mode, lanes: @lanes, workers: @workers,
|
|
317
|
-
|
|
318
|
-
active_workers: @workers, scale_ups: 0, scale_downs: 0
|
|
265
|
+
(@started && Native.stats(@id)) || {
|
|
266
|
+
mode: @mode, lanes: @lanes, workers: @workers, threads: @threads, batch: @batch, respawns: 0,
|
|
267
|
+
max_workers: @max_workers, active_workers: @workers, scale_ups: 0, scale_downs: 0
|
|
319
268
|
}
|
|
320
|
-
return base unless @started
|
|
321
|
-
|
|
322
|
-
queued, in_flight, served, rejected, timeouts, respawns, lane_depths = Native.server_stats(@id)
|
|
323
|
-
base.merge!(queued:, in_flight:, served:, rejected:, timeouts:, respawns:)
|
|
324
|
-
base[:lane_depths] = lane_depths if lane_depths
|
|
325
|
-
active_workers, _max_workers, scale_ups, scale_downs = Native.pool_stats(@id)
|
|
326
|
-
base.merge!(active_workers:, scale_ups:, scale_downs:)
|
|
327
|
-
rows = Native.worker_stats(@id)
|
|
328
|
-
base[:worker_status] = rows.map do |index, served, in_flight, busy_ms, quarantined, retired|
|
|
329
|
-
{index:, served:, in_flight:, busy_ms:, quarantined:, retired:}
|
|
330
|
-
end
|
|
331
|
-
base[:quarantined] = rows.count { |row| row[4] }
|
|
332
|
-
count, sum_seconds = Native.queue_time(@id)
|
|
333
|
-
base[:queue_time] = {count:, sum_seconds:}
|
|
334
|
-
base
|
|
335
269
|
end
|
|
336
270
|
|
|
337
271
|
private
|
|
338
272
|
|
|
339
|
-
#
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
@
|
|
273
|
+
# Say goodbye to the sockets, then wait for queued and in-flight work, up to `deadline`.
|
|
274
|
+
def drain(deadline)
|
|
275
|
+
# Each socket's on_shutdown, then its 1001, spread over at most a second and half of what
|
|
276
|
+
# is left of the drain. Seconds as they are: a deadline may be infinite.
|
|
277
|
+
Native.ws_leave_all(@id, deadline - Kino.monotonic)
|
|
278
|
+
|
|
279
|
+
# Drain: wait for queued + in-flight to reach zero, bounded by deadline.
|
|
280
|
+
# A socket counts until its on_close is with its worker; hijacked connections close below.
|
|
281
|
+
until Kino.monotonic >= deadline
|
|
282
|
+
queued, in_flight, sockets = Native.queue_stats(@id)
|
|
283
|
+
break if queued.zero? && in_flight.zero? && sockets.zero?
|
|
284
|
+
|
|
285
|
+
sleep 0.01
|
|
286
|
+
end
|
|
344
287
|
end
|
|
345
288
|
|
|
346
|
-
#
|
|
289
|
+
# Close the queues and join the pool; past `deadline`, free the clients and reap the workers.
|
|
290
|
+
def stop_workers(deadline)
|
|
291
|
+
# Idle workers see the closed queue and exit their loops.
|
|
292
|
+
# Switched hijacked connections close too, so an app serving one sees EOF.
|
|
293
|
+
Native.close_queue(@id)
|
|
294
|
+
join_workers(deadline)
|
|
295
|
+
|
|
296
|
+
unless @pool.done?
|
|
297
|
+
# Past the deadline with stuck handlers: free the clients first,
|
|
298
|
+
# then try to unblock and reap the workers.
|
|
299
|
+
Native.abort_all_inflight(@id)
|
|
300
|
+
Native.interrupt_all_workers(@id)
|
|
301
|
+
join_workers(Kino.monotonic + 0.2)
|
|
302
|
+
@pool.kill_stragglers
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def tear_down
|
|
307
|
+
# Only now the other hijacked requests: an unanswered long poll gets a 503.
|
|
308
|
+
Native.close_bridges(@id)
|
|
309
|
+
Native.shutdown_runtime(@id, 1_000)
|
|
310
|
+
# The control thread reports "draining" for the whole drain and stops
|
|
311
|
+
# only now, once there is nothing left to report.
|
|
312
|
+
Native.control_stop(@id)
|
|
313
|
+
# The runtime is gone, so hyper has dropped every pinned buffer;
|
|
314
|
+
# the keeper (and the strings it marked) may now be collected.
|
|
315
|
+
@pin_keeper = nil
|
|
316
|
+
remove_pidfile if @pidfile
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# The pool's shape: hooks, mode, workers and their ceiling, lanes, threads.
|
|
320
|
+
def configure_pool(settings)
|
|
321
|
+
worker_hooks = WORKER_HOOKS.to_h { |name| [name, validate_hook(settings[name], name)] }
|
|
322
|
+
@after_boot = validate_hook(settings[:after_boot], :after_boot)
|
|
323
|
+
@on_worker_exit = validate_hook(settings[:on_worker_exit], :on_worker_exit)
|
|
324
|
+
@worker_loop = !worker_hooks[:worker_loop].nil?
|
|
325
|
+
# Unset, lanes follow worker_loop, which runs on them.
|
|
326
|
+
@lanes = settings[:lanes].nil? ? @worker_loop : !!settings[:lanes]
|
|
327
|
+
raise ArgumentError, "worker_loop needs lanes; drop `lanes false`" if @worker_loop && !@lanes
|
|
328
|
+
@mode = resolve_mode(settings[:mode], worker_hooks)
|
|
329
|
+
@workers = Integer(settings[:workers] || default_workers)
|
|
330
|
+
# The pool ceiling; equal to the floor for a fixed pool.
|
|
331
|
+
@max_workers = settings[:max_workers].nil? ? @workers : Integer(settings[:max_workers])
|
|
332
|
+
if @max_workers < @workers
|
|
333
|
+
raise ArgumentError, "max_workers (#{@max_workers}) must be at least workers (#{@workers})"
|
|
334
|
+
end
|
|
335
|
+
@scale_down_after = settings[:scale_down_after].nil? ? 30.0 : Float(settings[:scale_down_after])
|
|
336
|
+
raise ArgumentError, "scale_down_after must be positive" unless @scale_down_after.positive?
|
|
337
|
+
if !settings[:scale_down_after].nil? && !elastic?
|
|
338
|
+
Log.warn("scale_down_after has no effect unless max_workers is above workers")
|
|
339
|
+
end
|
|
340
|
+
@worker_hooks = WorkerHooks.new(
|
|
341
|
+
**worker_hooks,
|
|
342
|
+
# The access log's GC and allocation figures come from the VM's
|
|
343
|
+
# process-wide counters, so they are measured only where one
|
|
344
|
+
# request at a time can own them: the GVL serializes :threaded
|
|
345
|
+
# mode, and a single ractor (a pool that can never grow past one)
|
|
346
|
+
# has nothing to race. A worker_loop interleaves its requests.
|
|
347
|
+
access_timing: !!settings[:log_requests] && !@worker_loop && (@mode == :threaded || @max_workers == 1)
|
|
348
|
+
)
|
|
349
|
+
# Default threads per mode: 1 in :ractor (threads inside a ractor
|
|
350
|
+
# share its lock; a measured +17% on fast handlers; raise `workers`
|
|
351
|
+
# for I/O concurrency instead), 3 in :threaded (threads ARE the
|
|
352
|
+
# concurrency there).
|
|
353
|
+
@threads = Integer(settings[:threads] || ((@mode == :ractor) ? 1 : 3))
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
# Queue, timeouts and sizes, the WebSocket budgets, and logging.
|
|
357
|
+
def configure_limits(settings)
|
|
358
|
+
# 0 would hand requests only to a worker already waiting, and 503 the rest.
|
|
359
|
+
@queue_depth = positive_integer(settings[:queue_depth], :queue_depth)
|
|
360
|
+
@queue_timeout_ms = whole_ms(settings[:queue_timeout])
|
|
361
|
+
@request_timeout_ms = settings[:request_timeout] ? whole_ms(settings[:request_timeout]) : 0
|
|
362
|
+
@max_connections = settings[:max_connections] ? Integer(settings[:max_connections]) : default_max_connections
|
|
363
|
+
@max_body_size = Integer(settings[:max_body_size] || 0)
|
|
364
|
+
@batch = [Integer(settings[:batch]), 1].max
|
|
365
|
+
Log.warn("batch has no effect with worker_loop") if @worker_loop && @batch > 1
|
|
366
|
+
@websocket_max_message = positive_integer(settings[:websocket_max_message], :websocket_max_message)
|
|
367
|
+
@websocket_max = settings[:websocket_max] && positive_integer(settings[:websocket_max], :websocket_max)
|
|
368
|
+
@websocket_ping_interval_ms = milliseconds(settings[:websocket_ping_interval], :websocket_ping_interval)
|
|
369
|
+
@log_requests = !!settings[:log_requests]
|
|
370
|
+
@shutdown_timeout = drain_seconds(settings[:shutdown_timeout], :shutdown_timeout)
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
# The I/O runtime, TLS, HTTP/2, the pidfile and the control plane.
|
|
374
|
+
def configure_io(settings)
|
|
375
|
+
@io_shards = !!settings[:io_shards]
|
|
376
|
+
@io_threads = Integer(settings[:io_threads]) unless settings[:io_threads].nil?
|
|
377
|
+
if @io_threads && @io_threads < 1
|
|
378
|
+
raise ArgumentError, "io_threads must be >= 1"
|
|
379
|
+
end
|
|
380
|
+
Log.warn("io_threads has no effect unless io_shards is true") if @io_threads && !@io_shards
|
|
381
|
+
@tokio_threads = settings[:tokio_threads]
|
|
382
|
+
@tls = validate_tls(settings[:tls])
|
|
383
|
+
if @tls && unix?
|
|
384
|
+
raise ArgumentError, "TLS is not supported on a unix socket bind; terminate TLS at the proxy in front"
|
|
385
|
+
end
|
|
386
|
+
@http2 = settings.fetch(:http2, true) ? true : false
|
|
387
|
+
@pidfile = settings[:pidfile]
|
|
388
|
+
@control_bind = settings[:control_bind]&.to_s
|
|
389
|
+
@control_token = settings[:control_token]&.to_s
|
|
390
|
+
# An empty token (e.g. control_token ENV["KINO_CONTROL_TOKEN"] with the
|
|
391
|
+
# var unset) must not half-disable auth: treat it as auth off, not as
|
|
392
|
+
# "require a zero-length Bearer token".
|
|
393
|
+
@control_token = nil if @control_token && @control_token.empty?
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
def configure_quarantine(settings)
|
|
397
|
+
@quarantine_timeout_ms = settings[:quarantine_timeout] && whole_ms(settings[:quarantine_timeout])
|
|
398
|
+
@quarantine_max =
|
|
399
|
+
if settings[:quarantine_max]
|
|
400
|
+
Integer(settings[:quarantine_max])
|
|
401
|
+
elsif @mode == :ractor
|
|
402
|
+
@workers
|
|
403
|
+
else
|
|
404
|
+
@workers * @threads
|
|
405
|
+
end
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
# Both pools are quarantine replacers: replace(worker_id, generation) spawns a
|
|
347
409
|
# replacement worker, then quarantines the wedged slot.
|
|
348
410
|
def start_quarantine_monitor
|
|
349
411
|
@quarantine_monitor = QuarantineMonitor.new(
|
|
350
412
|
server_id: @id, timeout_ms: @quarantine_timeout_ms,
|
|
351
|
-
max: @quarantine_max, replacer: pool
|
|
413
|
+
max: @quarantine_max, replacer: @pool
|
|
352
414
|
).start
|
|
353
415
|
end
|
|
354
416
|
|
|
@@ -357,16 +419,22 @@ module Kino
|
|
|
357
419
|
# timeslices instead of adding parallelism, and a fresh ractor can wait
|
|
358
420
|
# seconds for its first one while the others are CPU-bound.
|
|
359
421
|
def warn_scheduler_cap
|
|
360
|
-
cap =
|
|
422
|
+
cap = Kino.ruby_max_cpu
|
|
361
423
|
return if @max_workers <= cap
|
|
362
424
|
|
|
363
425
|
Log.warn("#{@max_workers} ractor workers exceed RUBY_MAX_CPU=#{cap}: only #{cap} can run " \
|
|
364
426
|
"Ruby code at once; set RUBY_MAX_CPU=#{@max_workers} for CPU-bound apps")
|
|
365
427
|
end
|
|
366
428
|
|
|
429
|
+
# Threads are not limited by RUBY_MAX_CPU, ractors are.
|
|
430
|
+
def default_workers
|
|
431
|
+
cpus = Kino.available_parallelism
|
|
432
|
+
(@mode == :ractor) ? [cpus, Kino.ruby_max_cpu].min.clamp(1..) : cpus
|
|
433
|
+
end
|
|
434
|
+
|
|
367
435
|
def start_pool_scaler
|
|
368
436
|
@pool_scaler = PoolScaler.new(
|
|
369
|
-
server_id: @id, pool: pool, floor: @workers, ceiling: @max_workers,
|
|
437
|
+
server_id: @id, pool: @pool, floor: @workers, ceiling: @max_workers,
|
|
370
438
|
scale_down_after: @scale_down_after
|
|
371
439
|
).start
|
|
372
440
|
end
|
|
@@ -380,6 +448,33 @@ module Kino
|
|
|
380
448
|
{cert: String(tls[:cert]), key: String(tls[:key])}
|
|
381
449
|
end
|
|
382
450
|
|
|
451
|
+
def positive_integer(value, name)
|
|
452
|
+
number = Integer(value, exception: false)
|
|
453
|
+
return number if number&.positive?
|
|
454
|
+
|
|
455
|
+
raise ArgumentError, "#{name} must be a positive Integer (got #{value.inspect})"
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
# Seconds to drain: infinity is no deadline; negative and NaN (no clock passes it) are refused.
|
|
459
|
+
def drain_seconds(value, name)
|
|
460
|
+
number = Float(value, exception: false)
|
|
461
|
+
return number if number && !number.nan? && !number.negative?
|
|
462
|
+
|
|
463
|
+
raise ArgumentError, "#{name} must be a non-negative number of seconds (got #{value.inspect})"
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
def whole_ms(seconds) = (Float(seconds) * 1000).round
|
|
467
|
+
|
|
468
|
+
# Seconds as whole milliseconds: nil and 0 mean off (0); anything above zero is at least 1.
|
|
469
|
+
def milliseconds(value, name)
|
|
470
|
+
seconds = value.nil? ? 0.0 : Float(value, exception: false)
|
|
471
|
+
unless seconds&.finite? && !seconds.negative?
|
|
472
|
+
raise ArgumentError, "#{name} must be a finite, non-negative number of seconds or nil (got #{value.inspect})"
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
seconds.zero? ? 0 : [whole_ms(seconds), 1].max
|
|
476
|
+
end
|
|
477
|
+
|
|
383
478
|
def validate_hook(handler, name)
|
|
384
479
|
return nil if handler.nil?
|
|
385
480
|
unless handler.respond_to?(:call)
|
|
@@ -389,10 +484,6 @@ module Kino
|
|
|
389
484
|
handler
|
|
390
485
|
end
|
|
391
486
|
|
|
392
|
-
def monotonic_now
|
|
393
|
-
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
394
|
-
end
|
|
395
|
-
|
|
396
487
|
# A pool that can grow: the ceiling is above the floor.
|
|
397
488
|
def elastic?
|
|
398
489
|
@max_workers > @workers
|
|
@@ -471,16 +562,40 @@ module Kino
|
|
|
471
562
|
nil
|
|
472
563
|
end
|
|
473
564
|
|
|
474
|
-
|
|
475
|
-
|
|
565
|
+
# Wait out a shutdown under way, then claim the next; false once stopped.
|
|
566
|
+
def claim_shutdown
|
|
567
|
+
@lifecycle.synchronize do
|
|
568
|
+
@shutdown_ended.wait(@lifecycle) while @stopping
|
|
569
|
+
next false unless @started
|
|
570
|
+
|
|
571
|
+
@stopping = true
|
|
572
|
+
@shutdown_error = nil
|
|
573
|
+
true
|
|
574
|
+
end
|
|
476
575
|
end
|
|
477
576
|
|
|
478
|
-
def
|
|
479
|
-
|
|
577
|
+
def end_shutdown(failure, stopped:)
|
|
578
|
+
@lifecycle.synchronize do
|
|
579
|
+
@stopping = false
|
|
580
|
+
@started = false if stopped
|
|
581
|
+
@shutdown_error = failure
|
|
582
|
+
@shutdown_ended.broadcast
|
|
583
|
+
end
|
|
480
584
|
end
|
|
481
585
|
|
|
482
|
-
|
|
483
|
-
|
|
586
|
+
JOIN_SLICE = 0.1
|
|
587
|
+
private_constant :JOIN_SLICE
|
|
588
|
+
|
|
589
|
+
# Join the workers up to the monotonic `deadline`; a worker_loop's a slice at a time, with
|
|
590
|
+
# Worker::Loop.warn_lingering between slices.
|
|
591
|
+
def join_workers(deadline)
|
|
592
|
+
return @pool.shutdown([deadline - Kino.monotonic, 0].max) unless @worker_loop
|
|
593
|
+
|
|
594
|
+
loop do
|
|
595
|
+
@pool.shutdown((deadline - Kino.monotonic).clamp(0, JOIN_SLICE))
|
|
596
|
+
Worker::Loop.warn_lingering(@id)
|
|
597
|
+
break if @pool.done? || Kino.monotonic >= deadline
|
|
598
|
+
end
|
|
484
599
|
end
|
|
485
600
|
|
|
486
601
|
# Policy (mode resolution): when is an app safe for ractor
|
|
@@ -489,7 +604,7 @@ module Kino
|
|
|
489
604
|
# :ractor with an unshareable app is an error, and we never
|
|
490
605
|
# make_shareable the user's app behind their back (deep-freezing
|
|
491
606
|
# someone's object graph is not a server's call to make).
|
|
492
|
-
def resolve_mode(requested)
|
|
607
|
+
def resolve_mode(requested, worker_hooks)
|
|
493
608
|
case requested
|
|
494
609
|
when :threaded
|
|
495
610
|
:threaded
|
|
@@ -500,7 +615,7 @@ module Kino
|
|
|
500
615
|
"Ractor.shareable_proc endpoints); try Ractor.make_shareable(app) " \
|
|
501
616
|
"or mode: :threaded"
|
|
502
617
|
end
|
|
503
|
-
if (name =
|
|
618
|
+
if (name = unshareable_hook_name(worker_hooks))
|
|
504
619
|
raise Error,
|
|
505
620
|
"mode: :ractor requires a Ractor-shareable #{name} hook " \
|
|
506
621
|
"(build it with Ractor.shareable_proc, or use mode: :threaded)"
|
|
@@ -510,7 +625,7 @@ module Kino
|
|
|
510
625
|
if !Ractor.shareable?(@app)
|
|
511
626
|
Log.warn("app is not Ractor-shareable; falling back to mode: :threaded")
|
|
512
627
|
:threaded
|
|
513
|
-
elsif (name =
|
|
628
|
+
elsif (name = unshareable_hook_name(worker_hooks))
|
|
514
629
|
Log.warn("#{name} hook is not Ractor-shareable; falling back to mode: :threaded")
|
|
515
630
|
:threaded
|
|
516
631
|
else
|
|
@@ -521,21 +636,12 @@ module Kino
|
|
|
521
636
|
end
|
|
522
637
|
end
|
|
523
638
|
|
|
524
|
-
# The
|
|
525
|
-
# and so must be Ractor-shareable there. after_boot and on_worker_exit
|
|
526
|
-
# run on the main thread and are exempt.
|
|
527
|
-
def worker_context_hooks
|
|
528
|
-
[[:on_error, @on_error], [:after_worker_boot, @after_worker_boot],
|
|
529
|
-
[:after_request_complete, @after_request_complete]]
|
|
530
|
-
end
|
|
531
|
-
|
|
532
|
-
# The name of the first worker-context hook that is set but not
|
|
639
|
+
# The name of the first of `hooks` (name => hook) that is set but not
|
|
533
640
|
# Ractor-shareable, or nil if all set ones are. Used by both the
|
|
534
641
|
# :ractor raise and the :auto warn/fallback branches, so "first
|
|
535
642
|
# offender" is defined once.
|
|
536
|
-
def
|
|
537
|
-
|
|
538
|
-
bad&.first
|
|
643
|
+
def unshareable_hook_name(hooks)
|
|
644
|
+
hooks.find { |_name, hook| !(hook.nil? || Ractor.shareable?(hook)) }&.first
|
|
539
645
|
end
|
|
540
646
|
end
|
|
541
647
|
end
|