kino 0.1.2-x86_64-linux → 0.2.0-x86_64-linux
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 +33 -0
- data/README.md +18 -1
- data/lib/kino/configuration.rb +6 -0
- data/lib/kino/kino.so +0 -0
- data/lib/kino/ractor_supervisor.rb +4 -3
- data/lib/kino/server.rb +111 -17
- data/lib/kino/templates/kino.rb.tt +8 -0
- data/lib/kino/version.rb +1 -1
- data/lib/kino/worker.rb +35 -11
- data/sig/kino.rbs +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 48359f37b2ea3ddb9d6535c05d0f75845f6723dfcd92ff35bd15e1b91b7641ae
|
|
4
|
+
data.tar.gz: 2fd57f5d5fc2e6bad3522a85d76c1fcf17cf43a8be24472aee9847e5bed63440
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3ea2c278e8feb0969c532e54ebd9aa0f5ce5774baf6b954c633c6afbc7605eb2f5ca46b0f879c337aeb8d25f2d97810a55b873b252a74b4bd2c2f8eed3d7b703
|
|
7
|
+
data.tar.gz: 2aea614aa9da23c4afa0882316de1baf88beb64cd50ec7a1b43b28a92c5c9578caf5d5c5c22c38469efd5478562947faa2a591825b16d2a69b8021b7c724f05e
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
+
## [0.2.0] - 2026-07-13
|
|
2
|
+
|
|
3
|
+
- Strip debug info from release builds.
|
|
4
|
+
- A panic in the native layer now raises a RuntimeError on the affected
|
|
5
|
+
worker (visible to `on_error` and the error log) instead of killing the
|
|
6
|
+
server process.
|
|
7
|
+
- The pidfile is claimed exclusively: starting refuses (instead of silently
|
|
8
|
+
overwriting) while the pidfile's owner is alive, a leftover file from a
|
|
9
|
+
dead process is replaced, symlinks are never followed, and shutdown
|
|
10
|
+
removes the file only while it still holds our pid.
|
|
11
|
+
- Zero-copy response bodies: bodies of 4 KB and up ride to the network
|
|
12
|
+
layer by reference instead of being copied at the FFI boundary, in both
|
|
13
|
+
dispatch modes. A 10 KB-body endpoint now serves at plaintext speed.
|
|
14
|
+
|
|
15
|
+
## [0.1.3] - 2026-07-04
|
|
16
|
+
|
|
17
|
+
- Non-String response header names and values (booleans, numbers, symbols)
|
|
18
|
+
are now serialized with `to_s`, matching Puma, instead of failing the
|
|
19
|
+
request with a `TypeError`. (Fixes [#3], reported by Max Erkin @rus-max)
|
|
20
|
+
- New `on_error` directive: a callable invoked with `(exception, env)` when
|
|
21
|
+
a worker catches an app or delivery error, after the client got its 500.
|
|
22
|
+
Delivery errors (unserializable header, a body that raised mid-stream)
|
|
23
|
+
happen after the middleware stack returned, so this hook is the only
|
|
24
|
+
place an error tracker can see them. Handler failures are logged and
|
|
25
|
+
swallowed; in :ractor mode the handler must be Ractor-shareable. (Fixes [#3], reported by Max Erkin @rus-max)
|
|
26
|
+
- Worker error log lines now include the exception backtrace (first 12
|
|
27
|
+
frames), not just the exception class and message.
|
|
28
|
+
- A streaming body that raises mid-stream now aborts the connection
|
|
29
|
+
instead of finishing the chunked response cleanly, so a client can no
|
|
30
|
+
longer mistake a truncated response for a complete one.
|
|
31
|
+
|
|
1
32
|
## [0.1.2] - 2026-06-22
|
|
2
33
|
|
|
3
34
|
- Drop a connection that has not sent its complete request headers
|
|
@@ -89,3 +120,5 @@ Initial release.
|
|
|
89
120
|
- Request timeouts: `request_timeout: seconds` (off by default) returns an
|
|
90
121
|
immediate 504 when the app misses the deadline; the late response is
|
|
91
122
|
dropped and the handler is never killed. Counted as `stats[:timeouts]`.
|
|
123
|
+
|
|
124
|
+
[#3]: https://github.com/yaroslav/kino/issues/3
|
data/README.md
CHANGED
|
@@ -14,13 +14,15 @@ and a threaded fallback mode runs everything else, Rails included.
|
|
|
14
14
|
* **Fast.** On a real 8-core server, every Kino mode is **1.5-2×**
|
|
15
15
|
ahead of a Puma fork cluster on I/O-light endpoints. Ractor mode also
|
|
16
16
|
wins on pure CPU, **30%+**. [Benchmarks](#benchmarks) below.
|
|
17
|
-
* **A fraction of the memory.**
|
|
17
|
+
* **A fraction of the memory.** About **~7×** on the simplistic bench
|
|
18
18
|
Ractor app, and about **4× less memory** than a Puma cluster serving Rails in fallback threaded mode.
|
|
19
19
|
* **Parallel without forking.** Ractor mode runs CPU work **more than
|
|
20
20
|
5× faster** than Kino's own GVL-bound threaded mode, in the same
|
|
21
21
|
small process.
|
|
22
22
|
* **Production plumbing included.** Graceful drain, crash supervision
|
|
23
23
|
and respawn, bounded queues with 503 backpressure, request timeouts,
|
|
24
|
+
hardened intake (slowloris and TLS-handshake deadlines, connection
|
|
25
|
+
and body-size caps), an `on_error` hook for your error tracker,
|
|
24
26
|
TLS (rustls), live stats, async access and app logging.
|
|
25
27
|
* **Tells you why.** `kino --check` lists exactly what blocks your app
|
|
26
28
|
from ractor mode, finding by finding, so you do not have to decode
|
|
@@ -224,6 +226,9 @@ server = Kino::Server.new(app,
|
|
|
224
226
|
queue_depth: 1024, # bounded queue; overflow → 503
|
|
225
227
|
queue_timeout: 5.0, # seconds before 503 on a full queue
|
|
226
228
|
request_timeout: nil, # seconds before a slow response becomes a 504 (nil = off)
|
|
229
|
+
max_connections: 8192, # cap concurrent connections; default: most of ulimit -n
|
|
230
|
+
max_body_size: 50 * 1024 * 1024, # bytes before a 413; nil = let a proxy handle it
|
|
231
|
+
on_error: ->(e, env) { ErrorTracker.capture(e) }, # after the client got its 500
|
|
227
232
|
shutdown_timeout: 30, # drain deadline
|
|
228
233
|
tls: { cert: "cert.pem", key: "key.pem" }, # file paths or inline PEM
|
|
229
234
|
)
|
|
@@ -303,6 +308,18 @@ is unsafe. A stuck handler still occupies its worker slot until it
|
|
|
303
308
|
returns, so set the deadline above your slowest legitimate endpoint and
|
|
304
309
|
watch `stats[:timeouts]`.
|
|
305
310
|
|
|
311
|
+
Timeouts guard your app; the network intake guards itself. New
|
|
312
|
+
connections past `max_connections` (default: most of `ulimit -n`) wait
|
|
313
|
+
in the kernel backlog; request bodies past `max_body_size` (default
|
|
314
|
+
50 MB, `nil` delegates to a fronting proxy) get a **413**; and fixed
|
|
315
|
+
deadlines drop slow-header clients (15 s), stalled TLS handshakes
|
|
316
|
+
(10 s), and uploads stalled mid-body (30 s). When a worker catches an
|
|
317
|
+
app or delivery error,
|
|
318
|
+
`on_error ->(error, env) { ErrorTracker.capture(error) }` is called
|
|
319
|
+
after the client got its 500—the only place a tracker sees errors
|
|
320
|
+
raised while the response was being written (in `:ractor` mode, build
|
|
321
|
+
the handler with `Ractor.shareable_proc`).
|
|
322
|
+
|
|
306
323
|
## Stats
|
|
307
324
|
|
|
308
325
|
`server.stats` returns a live snapshot: the configuration plus counters
|
data/lib/kino/configuration.rb
CHANGED
|
@@ -22,6 +22,7 @@ module Kino
|
|
|
22
22
|
batch: 1,
|
|
23
23
|
lanes: false,
|
|
24
24
|
log_requests: false,
|
|
25
|
+
on_error: nil,
|
|
25
26
|
shutdown_timeout: 30,
|
|
26
27
|
tokio_threads: nil,
|
|
27
28
|
tls: nil,
|
|
@@ -179,6 +180,11 @@ module Kino
|
|
|
179
180
|
# Native access log: one status-colored line per request to stdout.
|
|
180
181
|
def log_requests(enabled) = @config.set(:log_requests, !!enabled)
|
|
181
182
|
|
|
183
|
+
# Called with (exception, env) when a worker catches an app or
|
|
184
|
+
# delivery error; wire your error tracker here. Takes a callable
|
|
185
|
+
# or a block. Must be Ractor-shareable in :ractor mode.
|
|
186
|
+
def on_error(handler = nil, &block) = @config.set(:on_error, handler || block)
|
|
187
|
+
|
|
182
188
|
# Graceful-shutdown drain deadline in seconds.
|
|
183
189
|
def shutdown_timeout(seconds) = @config.set(:shutdown_timeout, seconds)
|
|
184
190
|
|
data/lib/kino/kino.so
CHANGED
|
Binary file
|
|
@@ -9,12 +9,13 @@ module Kino
|
|
|
9
9
|
class RactorSupervisor
|
|
10
10
|
attr_reader :respawns
|
|
11
11
|
|
|
12
|
-
def initialize(server_id, app, workers:, threads:, batch: 1)
|
|
12
|
+
def initialize(server_id, app, workers:, threads:, batch: 1, on_error: nil)
|
|
13
13
|
@server_id = server_id
|
|
14
14
|
@app = app
|
|
15
15
|
@workers = workers
|
|
16
16
|
@threads = threads
|
|
17
17
|
@batch = batch
|
|
18
|
+
@on_error = on_error
|
|
18
19
|
@respawns = 0
|
|
19
20
|
@draining = false
|
|
20
21
|
@lock = Mutex.new
|
|
@@ -83,13 +84,13 @@ module Kino
|
|
|
83
84
|
# old slot.
|
|
84
85
|
def spawn_worker
|
|
85
86
|
worker_ids = Array.new(@threads) { Native.register_worker(@server_id) }
|
|
86
|
-
ractor = Ractor.new(@server_id, worker_ids, @app, @batch) do |server_id, ids, app, batch|
|
|
87
|
+
ractor = Ractor.new(@server_id, worker_ids, @app, @batch, @on_error) do |server_id, ids, app, batch, on_error|
|
|
87
88
|
ids.map do |id|
|
|
88
89
|
Thread.new do
|
|
89
90
|
# Crashes surface via Ractor#value in the supervisor; don't also
|
|
90
91
|
# spray the backtrace to stderr from inside the dying ractor.
|
|
91
92
|
Thread.current.report_on_exception = false
|
|
92
|
-
Kino::Worker.run(server_id, id, app, batch)
|
|
93
|
+
Kino::Worker.run(server_id, id, app, batch, on_error)
|
|
93
94
|
end
|
|
94
95
|
end.each(&:join)
|
|
95
96
|
end
|
data/lib/kino/server.rb
CHANGED
|
@@ -41,6 +41,7 @@ module Kino
|
|
|
41
41
|
@bind = settings[:bind]
|
|
42
42
|
@requested_port = settings[:port]
|
|
43
43
|
@workers = Integer(settings[:workers])
|
|
44
|
+
@on_error = validate_on_error(settings[:on_error])
|
|
44
45
|
@mode = resolve_mode(settings[:mode])
|
|
45
46
|
# Default threads per mode: 1 in :ractor (threads inside a ractor
|
|
46
47
|
# share its lock; a measured +17% on fast handlers; raise `workers`
|
|
@@ -72,23 +73,35 @@ module Kino
|
|
|
72
73
|
def start
|
|
73
74
|
raise Error, "server already started" if @started
|
|
74
75
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
76
|
+
# Claim the pidfile before binding: refusing to start (another
|
|
77
|
+
# instance is alive) must not leave a booted native runtime behind.
|
|
78
|
+
write_pidfile if @pidfile
|
|
79
|
+
booted = false
|
|
80
|
+
begin
|
|
81
|
+
@id, @port = Native.server_start(
|
|
82
|
+
bind: @bind, port: @requested_port,
|
|
83
|
+
queue_depth: @queue_depth, queue_timeout_ms: @queue_timeout_ms,
|
|
84
|
+
request_timeout_ms: @request_timeout_ms,
|
|
85
|
+
max_connections: @max_connections,
|
|
86
|
+
max_body_size: @max_body_size,
|
|
87
|
+
tokio_threads: @tokio_threads,
|
|
88
|
+
tls_cert: @tls&.fetch(:cert), tls_key: @tls&.fetch(:key),
|
|
89
|
+
lanes: @lanes, log_requests: @log_requests
|
|
90
|
+
)
|
|
91
|
+
booted = true
|
|
92
|
+
ensure
|
|
93
|
+
remove_pidfile if @pidfile && !booted
|
|
94
|
+
end
|
|
95
|
+
# GC anchor for zero-copy response buffers: held for the server's
|
|
96
|
+
# lifetime so in-flight buffers survive even a worker ractor crash.
|
|
97
|
+
@pin_keeper = Native.pin_keeper(@id)
|
|
86
98
|
if @mode == :ractor
|
|
87
|
-
@supervisor = RactorSupervisor.new(@id, @app, workers: @workers, threads: @threads,
|
|
99
|
+
@supervisor = RactorSupervisor.new(@id, @app, workers: @workers, threads: @threads,
|
|
100
|
+
batch: @batch, on_error: @on_error).start
|
|
88
101
|
else
|
|
89
102
|
@worker_threads = (@workers * @threads).times.map do
|
|
90
103
|
worker_id = Native.register_worker(@id)
|
|
91
|
-
Thread.new { Worker.run(@id, worker_id, @app, @batch) }
|
|
104
|
+
Thread.new { Worker.run(@id, worker_id, @app, @batch, @on_error) }
|
|
92
105
|
end
|
|
93
106
|
end
|
|
94
107
|
@started = true
|
|
@@ -131,9 +144,12 @@ module Kino
|
|
|
131
144
|
end
|
|
132
145
|
|
|
133
146
|
Native.shutdown_runtime(@id, 1_000)
|
|
147
|
+
# The runtime is gone, so hyper has dropped every pinned buffer;
|
|
148
|
+
# the keeper (and the strings it marked) may now be collected.
|
|
149
|
+
@pin_keeper = nil
|
|
134
150
|
@worker_threads.clear
|
|
135
151
|
@started = false
|
|
136
|
-
|
|
152
|
+
remove_pidfile if @pidfile
|
|
137
153
|
nil
|
|
138
154
|
end
|
|
139
155
|
|
|
@@ -214,6 +230,15 @@ module Kino
|
|
|
214
230
|
{cert: String(tls[:cert]), key: String(tls[:key])}
|
|
215
231
|
end
|
|
216
232
|
|
|
233
|
+
def validate_on_error(handler)
|
|
234
|
+
return nil if handler.nil?
|
|
235
|
+
unless handler.respond_to?(:call)
|
|
236
|
+
raise ArgumentError, "on_error must respond to #call (got #{handler.class})"
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
handler
|
|
240
|
+
end
|
|
241
|
+
|
|
217
242
|
def monotonic_now
|
|
218
243
|
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
219
244
|
end
|
|
@@ -230,6 +255,67 @@ module Kino
|
|
|
230
255
|
[soft * 8 / 10, 64].max
|
|
231
256
|
end
|
|
232
257
|
|
|
258
|
+
# Claim the pidfile for this process. O_EXCL creation fails on ANY
|
|
259
|
+
# existing directory entry (regular file, symlink, even a dangling
|
|
260
|
+
# one), so a live instance's pidfile is never overwritten and a
|
|
261
|
+
# symlink is never followed. A leftover entry whose owner is gone is
|
|
262
|
+
# replaced; one that does not hold a pid is refused, not clobbered.
|
|
263
|
+
def write_pidfile
|
|
264
|
+
claim_pidfile
|
|
265
|
+
rescue Errno::EEXIST
|
|
266
|
+
refuse_unless_stale
|
|
267
|
+
begin
|
|
268
|
+
# Unlink removes the entry itself; a symlink's target is untouched.
|
|
269
|
+
File.unlink(@pidfile)
|
|
270
|
+
rescue Errno::ENOENT
|
|
271
|
+
# Vanished on its own; the claim below settles any remaining race.
|
|
272
|
+
end
|
|
273
|
+
begin
|
|
274
|
+
claim_pidfile
|
|
275
|
+
rescue Errno::EEXIST
|
|
276
|
+
raise Error, "lost the race for #{@pidfile}: another instance is starting"
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def claim_pidfile
|
|
281
|
+
File.open(@pidfile, File::WRONLY | File::CREAT | File::EXCL, 0o644) do |file|
|
|
282
|
+
file.write("#{Process.pid}\n")
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# @raise [Kino::Error] when the pidfile's owner is still alive, or the
|
|
287
|
+
# file does not look like a pidfile at all
|
|
288
|
+
def refuse_unless_stale
|
|
289
|
+
content = begin
|
|
290
|
+
File.read(@pidfile)
|
|
291
|
+
rescue Errno::ENOENT
|
|
292
|
+
return # already gone; nothing to refuse
|
|
293
|
+
end
|
|
294
|
+
pid = Integer(content.strip, exception: false)
|
|
295
|
+
unless pid&.positive?
|
|
296
|
+
raise Error, "refusing to overwrite #{@pidfile}: does not hold a pid"
|
|
297
|
+
end
|
|
298
|
+
raise Error, "already running (pid #{pid}, per #{@pidfile})" if process_alive?(pid)
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def process_alive?(pid)
|
|
302
|
+
Process.kill(0, pid)
|
|
303
|
+
true
|
|
304
|
+
rescue Errno::ESRCH
|
|
305
|
+
false
|
|
306
|
+
rescue Errno::EPERM
|
|
307
|
+
true # exists, just not ours to signal
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
# Delete only a pidfile that is still ours: by shutdown time the path
|
|
311
|
+
# may belong to a replacement instance, or an operator may have
|
|
312
|
+
# repointed it at something that is not a pidfile at all.
|
|
313
|
+
def remove_pidfile
|
|
314
|
+
File.unlink(@pidfile) if File.read(@pidfile) == "#{Process.pid}\n"
|
|
315
|
+
rescue Errno::ENOENT
|
|
316
|
+
nil
|
|
317
|
+
end
|
|
318
|
+
|
|
233
319
|
def join_workers(deadline)
|
|
234
320
|
if @supervisor
|
|
235
321
|
@supervisor.shutdown([deadline - monotonic_now, 0].max)
|
|
@@ -275,13 +361,21 @@ module Kino
|
|
|
275
361
|
"Ractor.shareable_proc endpoints); try Ractor.make_shareable(app) " \
|
|
276
362
|
"or mode: :threaded"
|
|
277
363
|
end
|
|
364
|
+
unless @on_error.nil? || Ractor.shareable?(@on_error)
|
|
365
|
+
raise Error,
|
|
366
|
+
"mode: :ractor requires a Ractor-shareable on_error handler " \
|
|
367
|
+
"(build it with Ractor.shareable_proc, or use mode: :threaded)"
|
|
368
|
+
end
|
|
278
369
|
:ractor
|
|
279
370
|
when :auto
|
|
280
|
-
if Ractor.shareable?(@app)
|
|
281
|
-
:ractor
|
|
282
|
-
else
|
|
371
|
+
if !Ractor.shareable?(@app)
|
|
283
372
|
warn "Kino: app is not Ractor-shareable; falling back to mode: :threaded"
|
|
284
373
|
:threaded
|
|
374
|
+
elsif !(@on_error.nil? || Ractor.shareable?(@on_error))
|
|
375
|
+
warn "Kino: on_error handler is not Ractor-shareable; falling back to mode: :threaded"
|
|
376
|
+
:threaded
|
|
377
|
+
else
|
|
378
|
+
:ractor
|
|
285
379
|
end
|
|
286
380
|
else
|
|
287
381
|
raise ArgumentError, "mode must be :auto, :ractor, or :threaded (got #{requested.inspect})"
|
|
@@ -79,6 +79,14 @@
|
|
|
79
79
|
# never saw, such as 503s. Recommended in development.
|
|
80
80
|
# log_requests false
|
|
81
81
|
|
|
82
|
+
# Called when a worker catches an app or delivery error, after the client
|
|
83
|
+
# got its 500. This is the only place that sees errors raised while the
|
|
84
|
+
# response was being written (bad header, body that died mid-stream):
|
|
85
|
+
# they happen after your middleware returned, so only the server can
|
|
86
|
+
# report them. Wire your error tracker here. In :ractor mode the handler
|
|
87
|
+
# must be Ractor-shareable (build it with Ractor.shareable_proc).
|
|
88
|
+
# on_error ->(error, env) { ExceptionService.capture(error) }
|
|
89
|
+
|
|
82
90
|
## Lifecycle
|
|
83
91
|
|
|
84
92
|
# On shutdown, give in-flight requests this many seconds to finish.
|
data/lib/kino/version.rb
CHANGED
data/lib/kino/worker.rb
CHANGED
|
@@ -18,13 +18,13 @@ module Kino
|
|
|
18
18
|
|
|
19
19
|
module_function
|
|
20
20
|
|
|
21
|
-
def run(server_id, worker_id, app, batch_size = 1)
|
|
21
|
+
def run(server_id, worker_id, app, batch_size = 1, on_error = nil)
|
|
22
22
|
if batch_size <= 1
|
|
23
23
|
env = Native.take_one(server_id, worker_id)
|
|
24
|
-
env = handle_one(env, server_id, worker_id, app) while env
|
|
24
|
+
env = handle_one(env, server_id, worker_id, app, on_error) while env
|
|
25
25
|
else
|
|
26
26
|
batch = Native.take_batch(server_id, worker_id, batch_size)
|
|
27
|
-
batch = process(batch, server_id, worker_id, app, batch_size) while batch
|
|
27
|
+
batch = process(batch, server_id, worker_id, app, batch_size, on_error) while batch
|
|
28
28
|
end
|
|
29
29
|
end
|
|
30
30
|
|
|
@@ -34,8 +34,8 @@ module Kino
|
|
|
34
34
|
NOT_FUSED = Object.new.freeze
|
|
35
35
|
|
|
36
36
|
# Handle one request; returns the next env (fused take) or nil.
|
|
37
|
-
def handle_one(env, server_id, worker_id, app)
|
|
38
|
-
result = serve(env, app) do |request, status, headers, chunks|
|
|
37
|
+
def handle_one(env, server_id, worker_id, app, on_error)
|
|
38
|
+
result = serve(env, app, on_error) do |request, status, headers, chunks|
|
|
39
39
|
request.respond_and_take_one(server_id, worker_id, status, headers, chunks)
|
|
40
40
|
end
|
|
41
41
|
result.equal?(NOT_FUSED) ? Native.take_one(server_id, worker_id) : result
|
|
@@ -43,10 +43,10 @@ module Kino
|
|
|
43
43
|
|
|
44
44
|
# Handle every env in the batch; returns the next batch (the last
|
|
45
45
|
# simple response rides the fused respond_and_take) or nil on shutdown.
|
|
46
|
-
def process(batch, server_id, worker_id, app, batch_size)
|
|
46
|
+
def process(batch, server_id, worker_id, app, batch_size, on_error)
|
|
47
47
|
last = batch.size - 1
|
|
48
48
|
batch.each_with_index do |env, index|
|
|
49
|
-
result = serve(env, app) do |request, status, headers, chunks|
|
|
49
|
+
result = serve(env, app, on_error) do |request, status, headers, chunks|
|
|
50
50
|
if index == last
|
|
51
51
|
request.respond_and_take(server_id, worker_id, batch_size,
|
|
52
52
|
status, headers, chunks)
|
|
@@ -66,7 +66,7 @@ module Kino
|
|
|
66
66
|
# here and return NOT_FUSED. App errors must never kill the worker;
|
|
67
67
|
# hard crashes (Exception) are the supervisor's job; and `abort` does
|
|
68
68
|
# the right thing whether or not the response head already went out.
|
|
69
|
-
def serve(env, app)
|
|
69
|
+
def serve(env, app, on_error)
|
|
70
70
|
request = env[KINO_REQUEST]
|
|
71
71
|
env[RACK_INPUT] ||= Input.new(request)
|
|
72
72
|
status, headers, body = app.call(env)
|
|
@@ -80,11 +80,32 @@ module Kino
|
|
|
80
80
|
NOT_FUSED
|
|
81
81
|
end
|
|
82
82
|
rescue => e
|
|
83
|
-
|
|
83
|
+
# Abort before the hook: the client's 500 must never wait on a
|
|
84
|
+
# reporting round-trip. The hook is the app's only window onto
|
|
85
|
+
# delivery errors (they happen after app.call returned, so no
|
|
86
|
+
# middleware can see them); its own failures are logged, not raised,
|
|
87
|
+
# because nothing may escape this block and kill the worker.
|
|
88
|
+
Native.log_error(error_log_line(e))
|
|
84
89
|
request.abort
|
|
90
|
+
if on_error
|
|
91
|
+
begin
|
|
92
|
+
on_error.call(e, env)
|
|
93
|
+
rescue => hook_error
|
|
94
|
+
Native.log_error("on_error hook raised #{hook_error.class}: #{hook_error.message}")
|
|
95
|
+
end
|
|
96
|
+
end
|
|
85
97
|
NOT_FUSED
|
|
86
98
|
end
|
|
87
99
|
|
|
100
|
+
# First frames only: the raise site is at the top, and Rails stacks
|
|
101
|
+
# run hundreds of middleware frames deep. Hooks get the full exception.
|
|
102
|
+
BACKTRACE_FRAMES = 12
|
|
103
|
+
|
|
104
|
+
def error_log_line(error)
|
|
105
|
+
["#{error.class}: #{error.message}",
|
|
106
|
+
*(error.backtrace || []).first(BACKTRACE_FRAMES)].join("\n ")
|
|
107
|
+
end
|
|
108
|
+
|
|
88
109
|
def deliver_streaming(request, status, headers, body, input)
|
|
89
110
|
request.send_headers(status, headers)
|
|
90
111
|
if body.respond_to?(:call) && !body.respond_to?(:each)
|
|
@@ -99,10 +120,13 @@ module Kino
|
|
|
99
120
|
end
|
|
100
121
|
else
|
|
101
122
|
# Enumerable body: chunked transfer unless the app set content-length.
|
|
123
|
+
# finish only on success: a body that raised must abort the
|
|
124
|
+
# connection (serve's rescue), not fake a clean end of stream that
|
|
125
|
+
# the client cannot tell from a complete response.
|
|
102
126
|
begin
|
|
103
127
|
body.each { |chunk| request.write_chunk(chunk) }
|
|
104
|
-
ensure
|
|
105
128
|
request.finish
|
|
129
|
+
ensure
|
|
106
130
|
body.close if body.respond_to?(:close)
|
|
107
131
|
end
|
|
108
132
|
end
|
|
@@ -119,6 +143,6 @@ module Kino
|
|
|
119
143
|
end
|
|
120
144
|
|
|
121
145
|
private_class_method :handle_one, :process, :serve, :deliver_streaming,
|
|
122
|
-
:join_chunks
|
|
146
|
+
:join_chunks, :error_log_line
|
|
123
147
|
end
|
|
124
148
|
end
|
data/sig/kino.rbs
CHANGED
|
@@ -97,6 +97,7 @@ module Kino
|
|
|
97
97
|
def batch: (int count) -> untyped
|
|
98
98
|
def lanes: (boolish enabled) -> untyped
|
|
99
99
|
def log_requests: (boolish enabled) -> untyped
|
|
100
|
+
def on_error: (?^(Exception, Hash[String, untyped]) -> void handler) ?{ (Exception, Hash[String, untyped]) -> void } -> untyped
|
|
100
101
|
def shutdown_timeout: (Numeric seconds) -> untyped
|
|
101
102
|
def tokio_threads: (int count) -> untyped
|
|
102
103
|
def tls: (cert: String, key: String) -> untyped
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: kino
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: x86_64-linux
|
|
6
6
|
authors:
|
|
7
7
|
- Yaroslav Markin
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-07-13 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: logger
|