kino 0.7.1-aarch64-linux → 0.8.0-aarch64-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/.yardopts +5 -0
- data/CHANGELOG.md +286 -0
- 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/clippy.toml +10 -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/kino.so +0 -0
- 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 +14 -2
data/README.md
CHANGED
|
@@ -9,16 +9,19 @@ Ruby threads cannot run Ruby code in parallel, so production setups fork
|
|
|
9
9
|
a process per core and pay for each copy in memory. Kino runs your code
|
|
10
10
|
on every core in **one small process**. A **Rust** (tokio + hyper)
|
|
11
11
|
front-end owns the network, parallel **Ractors** run your Rack 3 app,
|
|
12
|
-
and a threaded fallback mode runs everything else
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
and a threaded fallback mode runs everything else. Rails main partially
|
|
13
|
+
runs on Ractors too (see [Rails on Ractors](doc/rails-on-ractors.md)).
|
|
14
|
+
|
|
15
|
+
* **Fast.** On a real 8-core server, Kino's ractor mode is **1.5-1.6×**
|
|
16
|
+
ahead of a Puma fork cluster on I/O-light endpoints (threaded mode
|
|
17
|
+
1.1-1.4×)—with native **HTTP/2** adding another **+41%** over
|
|
18
|
+
HTTP/1.1 on the same server (+58% over TLS).
|
|
19
|
+
Ractor mode also wins on pure CPU, **+14%**.
|
|
18
20
|
[Benchmarks](#benchmarks) below.
|
|
19
|
-
* **A fraction of the memory.** About **~
|
|
20
|
-
Ractor app
|
|
21
|
-
Rails in
|
|
21
|
+
* **A fraction of the memory.** About **~7×** on the simplistic bench
|
|
22
|
+
Ractor app (14× on a Ruby 4.1 development build), and about **6.7×
|
|
23
|
+
less memory** than a Puma cluster serving Rails (measured in threaded
|
|
24
|
+
mode, before Rails could run on Ractors).
|
|
22
25
|
* **Parallel without forking.** Ractor mode runs CPU work **more than
|
|
23
26
|
5× faster** than Kino's own GVL-bound threaded mode, in the same
|
|
24
27
|
small process.
|
|
@@ -46,11 +49,17 @@ and a threaded fallback mode runs everything else, Rails included.
|
|
|
46
49
|
- [Config file and CLI](#config-file-and-cli)
|
|
47
50
|
- [`kino --check`](#kino---check)
|
|
48
51
|
- [Request timeouts](#request-timeouts)
|
|
52
|
+
- [Lifecycle hooks](#lifecycle-hooks)
|
|
53
|
+
- [Elastic pool](#elastic-pool-experimental)
|
|
54
|
+
- [Stuck-worker quarantine](#stuck-worker-quarantine)
|
|
49
55
|
- [Stats](#stats)
|
|
50
56
|
- [Logging](#logging)
|
|
51
57
|
- [Timer waits](#timer-waits)
|
|
52
58
|
- [Rack 3 compliance](#rack-3-compliance)
|
|
53
|
-
- [
|
|
59
|
+
- [Streaming responses](#streaming-responses)
|
|
60
|
+
- [WebSockets](#websockets-experimental)
|
|
61
|
+
- [Fibers inside Kino workers](#fibers-inside-kino-workers-experimental)
|
|
62
|
+
- [Rails](#rails-1)
|
|
54
63
|
|
|
55
64
|
## Why
|
|
56
65
|
|
|
@@ -67,142 +76,151 @@ notes live in [doc/architecture.md](doc/architecture.md).
|
|
|
67
76
|
|
|
68
77
|
## Benchmarks
|
|
69
78
|
|
|
70
|
-
Measured on a real server: AWS **
|
|
79
|
+
Measured on a real server: AWS **c8a.2xlarge** (8-core AMD EPYC 9R45,
|
|
71
80
|
16 GB, Amazon Linux 2023). This is a realistic app-server size.
|
|
72
81
|
|
|
73
82
|
**These tables run a tiny synthetic Rack app**—plaintext, a 10 KB body,
|
|
74
83
|
a CPU-bound `fib`, a 5 ms wait—deliberately small, to measure the server
|
|
75
84
|
rather than an app. It is Ractor-shareable, so Kino runs it in `:ractor`
|
|
76
85
|
mode (and `:threaded` for comparison). **A real Rails app is a different
|
|
77
|
-
story:**
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
86
|
+
story:** the Rails numbers were measured in Kino's `:threaded` mode,
|
|
87
|
+
before Rails could run on Ractors, and are reported separately—see
|
|
88
|
+
[Rails](#rails) below.
|
|
89
|
+
Ruby 4.0.9 with YJIT (re-measured 2026-11), every server at 8 workers:
|
|
90
|
+
Puma forks 8 workers × 3 threads, Kino stays in one process
|
|
81
91
|
(8 workers; 1 thread each in ractor modes, 3 in threaded). Numbers are
|
|
82
|
-
req/s by wrk (8-second windows, 64 connections, same host
|
|
83
|
-
|
|
92
|
+
req/s by wrk (8-second windows, 64 connections, same host, no CPU
|
|
93
|
+
pinning), the median of 3 boots.
|
|
94
|
+
[Methodology](doc/benchmarks.md#methodology).
|
|
84
95
|
|
|
85
96
|
| endpoint | Kino :ractor | + lanes | :ractor, `workers 32`² | Kino :threaded | Puma (cluster) |
|
|
86
97
|
|-------------|-------------:|--------:|-----------------------:|---------------:|---------------:|
|
|
87
|
-
| /plaintext |
|
|
88
|
-
| /10k |
|
|
89
|
-
| /cpu (fib) |
|
|
90
|
-
| /io (5 ms) | 1,
|
|
91
|
-
| /io_native | 1,
|
|
92
|
-
|
|
93
|
-
Memory
|
|
94
|
-
(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
than the Puma cluster—the gap stays large because a trivial app is almost
|
|
99
|
-
all private per-worker heap, which copy-on-write can't share:
|
|
98
|
+
| /plaintext | 465,055 | **525,155** | 245,612 | 394,053 | 291,409 |
|
|
99
|
+
| /10k | 375,470 | **409,085** | 203,154 | 285,270 | 251,357 |
|
|
100
|
+
| /cpu (fib) | **118,095**¹| 111,751 | 83,713 | 20,187 | 103,653 |
|
|
101
|
+
| /io (5 ms) | 1,553 | 1,566 | **5,721** | 4,738 | 4,729 |
|
|
102
|
+
| /io_native | 1,578 | 1,579 | **6,264** | 4,740 | 4,729 |
|
|
103
|
+
|
|
104
|
+
Memory, by **PSS** (proportional set size;
|
|
105
|
+
[why](doc/benchmarks.md#methodology)) after sustained load. On the tiny
|
|
106
|
+
app Kino is **~7× lighter in :ractor mode, ~11× in :threaded** than the
|
|
107
|
+
Puma cluster—a trivial app is almost all private per-worker heap, which
|
|
108
|
+
copy-on-write can't share:
|
|
100
109
|
|
|
101
110
|
| tiny app, Kino | Kino (one process) | Puma cluster (8 workers) | ratio |
|
|
102
111
|
|-----------------|-------------------:|-------------------------:|------:|
|
|
103
|
-
| :ractor (8×1) | **
|
|
104
|
-
| :threaded (8×3) | **
|
|
112
|
+
| :ractor (8×1) | **167 MB** | 1,171 MB | ~7× |
|
|
113
|
+
| :threaded (8×3) | **110 MB**³| 1,171 MB | ~11× |
|
|
105
114
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
115
|
+
On a real Rails app (in `:threaded` mode, [below](#rails)) the gap is
|
|
116
|
+
**~6.7×**: Puma's forks share the Rails framework copy-on-write, but
|
|
117
|
+
each fork keeps its own heap:
|
|
109
118
|
|
|
110
119
|
| Rails hello-world | Kino :threaded | Puma cluster (8 workers) | ratio |
|
|
111
120
|
|-------------------|---------------:|-------------------------:|------:|
|
|
112
|
-
| **PSS** | **
|
|
113
|
-
|
|
114
|
-
"+ lanes" is the experimental per-worker-queue dispatcher (`lanes true`)
|
|
115
|
-
|
|
116
|
-
[
|
|
117
|
-
|
|
118
|
-
¹ Stock settings, no tuning
|
|
119
|
-
CPU by +
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
needs. See
|
|
134
|
-
[doc/benchmarks.md](doc/benchmarks.md#why-io-lags-in-ractor-mode-on-linux).
|
|
135
|
-
|
|
136
|
-
³ With `MALLOC_ARENA_MAX=2` (the standard Ruby deployment setting;
|
|
137
|
-
Heroku's default). Without it, 24 threads churning 10 KB responses
|
|
138
|
-
through one glibc heap balloon to ~647 MB—an arena-fragmentation
|
|
139
|
-
footgun, not a leak, and ractor mode sidesteps it. See
|
|
140
|
-
[doc/benchmarks.md](doc/benchmarks.md#memory-under-load-and-the-glibc-arena-footgun).
|
|
121
|
+
| **PSS** | **96 MB** | **640 MB** | ~6.7× |
|
|
122
|
+
|
|
123
|
+
"+ lanes" is the experimental per-worker-queue dispatcher (`lanes true`),
|
|
124
|
+
the fastest plaintext/10k of any configuration here
|
|
125
|
+
([lanes](doc/benchmarks.md#lane-dispatch-experimental-lanes-true)).
|
|
126
|
+
|
|
127
|
+
¹ Stock settings, no tuning: ractor mode beats the fork cluster on pure
|
|
128
|
+
CPU by +14% (+8% with lanes); threaded mode shows the GVL ceiling that
|
|
129
|
+
every single-process Ruby server hits
|
|
130
|
+
([CPU tuning](doc/benchmarks.md#cpu-bound-tuning)).
|
|
131
|
+
|
|
132
|
+
² Wait-bound throughput is slots ÷ wait, and Kino slots are threads, not
|
|
133
|
+
processes: `workers 32` is **+21% over the cluster on /io (+32% via
|
|
134
|
+
`Kino.sleep`)** in one small process, at 16-19% behind it on /plaintext,
|
|
135
|
+
/10k and /cpu on Ruby 4.0.9 (ahead on every row on
|
|
136
|
+
[Ruby 4.1](#ruby-41-development-build);
|
|
137
|
+
[why](doc/benchmarks.md#why-io-lags-in-ractor-mode-on-linux)).
|
|
138
|
+
|
|
139
|
+
³ With `MALLOC_ARENA_MAX=2`, the standard Ruby deployment setting
|
|
140
|
+
(144 MB without it;
|
|
141
|
+
[arenas](doc/benchmarks.md#memory-under-load-and-the-glibc-arena-footgun)).
|
|
141
142
|
|
|
142
143
|
A common first idea is to keep your current server and wrap the app in
|
|
143
|
-
a ractor pool. We measured that too
|
|
144
|
-
doc):
|
|
144
|
+
a ractor pool. We measured that too, with Kino at 8×3 for the wrappers'
|
|
145
|
+
24 slots ([analysis](doc/benchmarks.md#the-ractor-pool-wrapper-comparison)):
|
|
145
146
|
|
|
146
147
|
| endpoint | Kino :ractor (8×3) | Puma + ractor wrapper | Falcon + ractor wrapper |
|
|
147
148
|
|------------|-------------------:|----------------------:|------------------------:|
|
|
148
|
-
| /plaintext | **
|
|
149
|
-
| /cpu (fib) |
|
|
150
|
-
| /io (5 ms) | **4,
|
|
149
|
+
| /plaintext | **412,687** | 40,675 | 241,390 |
|
|
150
|
+
| /cpu (fib) | **108,063** | 33,322 | 94,417 |
|
|
151
|
+
| /io (5 ms) | **4,629** | 1,509 | 1,564 |
|
|
152
|
+
|
|
153
|
+
### Ruby 4.1 (development build)
|
|
151
154
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
+
Ruby 4.0.9 is the released Ruby, and the primary number above. The same
|
|
156
|
+
box and method on a **development build** of Ruby 4.1 (`4.1.0dev`,
|
|
157
|
+
master f2d62c677e of 2026-11-21, YJIT on), which can move before 4.1 is
|
|
158
|
+
released:
|
|
159
|
+
|
|
160
|
+
| endpoint, Ruby 4.1 dev | Kino :ractor | + lanes | :ractor, `workers 32` | Kino :threaded | Puma (cluster) |
|
|
161
|
+
|-------------|-------------:|--------:|-----------------------:|---------------:|---------------:|
|
|
162
|
+
| /plaintext | 512,945 | **608,283** | 463,223 | 378,380 | 269,662 |
|
|
163
|
+
| /10k | 447,144 | **515,785** | 388,306 | 275,927 | 218,884 |
|
|
164
|
+
| /cpu (fib) | 128,097 | 112,276 | **128,109** | 19,739 | 99,379 |
|
|
165
|
+
| /io (5 ms) | 1,558 | 1,542 | **6,102** | 4,702 | 4,728 |
|
|
166
|
+
| /io_native | 1,576 | 1,581 | **6,314** | 4,738 | 4,730 |
|
|
167
|
+
|
|
168
|
+
| tiny app, Ruby 4.1 dev | Kino (one process) | Puma cluster (8 workers) | ratio |
|
|
169
|
+
|-----------------|-------------------:|-------------------------:|------:|
|
|
170
|
+
| :ractor (8×1) | **52 MB** | 746 MB | ~14× |
|
|
171
|
+
| :threaded (8×3), `MALLOC_ARENA_MAX=2` | **150 MB** | 746 MB | ~5× |
|
|
172
|
+
|
|
173
|
+
Ruby 4.1 collects garbage per Ractor: **every ractor column rises** on
|
|
174
|
+
/plaintext and /10k, **`workers 32` gains about 90% there** and leads
|
|
175
|
+
the cluster on every row, and **`:ractor` memory falls to 52 MB**, 14×
|
|
176
|
+
lighter than the cluster; `:threaded` and Puma lose 2-13% on
|
|
177
|
+
/plaintext, /10k and /cpu. The rest, with the Ruby 4.1 wrapper, HTTP/2
|
|
178
|
+
and Rails tables:
|
|
179
|
+
[doc/benchmarks.md](doc/benchmarks.md#ruby-41-development-build).
|
|
155
180
|
|
|
156
181
|
### HTTP/2 head-to-head
|
|
157
182
|
|
|
158
183
|
Kino speaks HTTP/2 natively, so it skips the usual nginx-termination
|
|
159
|
-
hop. Same box
|
|
160
|
-
same app:
|
|
184
|
+
hop. Same box, Ruby 4.0.9; all over TLS, h2load, 64 in-flight, same app:
|
|
161
185
|
|
|
162
186
|
| /plaintext | req/s | /upload (64 KB) |
|
|
163
187
|
|-------------------------------|------------:|----------------:|
|
|
164
|
-
| Kino native h2 | **
|
|
165
|
-
| Kino HTTP/1.1 (same boot) |
|
|
166
|
-
| nginx h2 → Kino HTTP/1.1 |
|
|
167
|
-
| Falcon (native h2) |
|
|
188
|
+
| Kino native h2 | **355,281** | **52,635** |
|
|
189
|
+
| Kino HTTP/1.1 (same boot) | 225,264 | 47,940 |
|
|
190
|
+
| nginx h2 → Kino HTTP/1.1 | 245,741 | 1,224¹ |
|
|
191
|
+
| Falcon (native h2) | 100,399 | 49,162 |
|
|
168
192
|
|
|
169
|
-
Native h2
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
methodology: [doc/benchmarks.md](doc/benchmarks.md#http2); reproduce
|
|
175
|
-
with `bench/h2.sh`.
|
|
193
|
+
Native h2 is **+58%** over HTTP/1.1 on the same TLS server (+41% on
|
|
194
|
+
cleartext h2c) and **+45% over fronting the same Kino with nginx**—the
|
|
195
|
+
proxy hop is pure cost. Uploads, h2's classic weak spot, run ahead of
|
|
196
|
+
HTTP/1.1 over TLS (+10%) and within 3% of it in cleartext
|
|
197
|
+
([full matrix](doc/benchmarks.md#http2); `bench/h2.sh`).
|
|
176
198
|
|
|
177
|
-
¹
|
|
199
|
+
¹ Not an HTTP/2 cost: nginx spools a body larger than
|
|
200
|
+
`client_body_buffer_size` to a temporary file; with a larger buffer, or
|
|
201
|
+
`proxy_request_buffering off`, the lane runs at 51-52k.
|
|
178
202
|
|
|
179
203
|
### Rails
|
|
180
204
|
|
|
181
|
-
Rails
|
|
182
|
-
|
|
183
|
-
|
|
205
|
+
These Rails numbers were measured in `:threaded` mode, one GVL-bound
|
|
206
|
+
process, before Rails could run on Ractors. On the same box, Ruby 4.0.9
|
|
207
|
+
(the controller-only hello app, kept as the bench subject in
|
|
208
|
+
`bench/rails_hello.ru`; edge Rails, production, 8×5):
|
|
184
209
|
|
|
185
210
|
| Rails hello-world | req/s | memory (PSS) |
|
|
186
211
|
|------------------------------|-------:|-------------:|
|
|
187
|
-
| Kino :threaded (one process) |
|
|
188
|
-
| Puma cluster (8 workers) |
|
|
212
|
+
| Kino :threaded (one process) | 7,660 | **96 MB** |
|
|
213
|
+
| Puma cluster (8 workers) | 34,688 | 640 MB |
|
|
189
214
|
|
|
190
215
|
The honest trade-off: Puma's fork cluster uses all 8 cores, so it serves
|
|
191
|
-
~5
|
|
192
|
-
|
|
193
|
-
tracked in [doc/rails-on-ractors.md](doc/rails-on-ractors.md).
|
|
194
|
-
|
|
195
|
-
In short: on the tiny synthetic app, ractor mode beats fork-level CPU parallelism (**5.7×** Kino's
|
|
196
|
-
own GVL-bound threaded mode, +25% over the cluster) in one process, at
|
|
197
|
-
about 1/8th of the cluster's memory by PSS (~4× on a real Rails app).
|
|
198
|
-
Every Kino mode is 1.5-1.7× ahead of the cluster on I/O-light endpoints,
|
|
199
|
-
and native HTTP/2 adds +79% over HTTP/1.1 on top. The macOS numbers
|
|
200
|
-
(secondary; everything there hits the loopback ceiling) and the
|
|
201
|
-
YJIT × Ractors gotcha are in [doc/benchmarks.md](doc/benchmarks.md).
|
|
216
|
+
~4.5× the throughput—at ~6.7× the memory. Ractor-mode Rails is not
|
|
217
|
+
benchmarked yet ([what runs](doc/rails-on-ractors.md)).
|
|
202
218
|
|
|
203
|
-
Reproduce: `bench/run.sh [seconds] [concurrency]` for the main table
|
|
204
|
-
`bench/studies.sh`
|
|
205
|
-
|
|
219
|
+
Reproduce: `bench/run.sh [seconds] [concurrency]` for the main table
|
|
220
|
+
(the [exact commands](doc/benchmarks.md#methodology)), `bench/studies.sh`
|
|
221
|
+
for the follow-ups (CPU recipe, topology, scaling, sharded I/O, logging,
|
|
222
|
+
memory), `bench/h2.sh` for HTTP/2. The macOS numbers and the
|
|
223
|
+
YJIT × Ractors gotcha are in [doc/benchmarks.md](doc/benchmarks.md).
|
|
206
224
|
|
|
207
225
|
## Install
|
|
208
226
|
|
|
@@ -252,93 +270,42 @@ end
|
|
|
252
270
|
Kino::Server.run(app, port: 9292) # traps INT/TERM; Ctrl-C drains gracefully
|
|
253
271
|
```
|
|
254
272
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
server = Kino::Server.new(app,
|
|
259
|
-
bind: "127.0.0.1", # or "unix:///run/kino.sock" behind a proxy
|
|
260
|
-
port: 9292, # 0 = ephemeral; read back via server.port
|
|
261
|
-
workers: Kino.available_parallelism, # ractors (parallelism); the default
|
|
262
|
-
max_workers: nil, # experimental: grow past workers under load (see Elastic pool)
|
|
263
|
-
scale_down_after: 30, # seconds idle before an extra worker retires
|
|
264
|
-
threads: 1, # per worker; ractor default 1, threaded default 3
|
|
265
|
-
mode: :auto, # :auto | :ractor | :threaded
|
|
266
|
-
queue_depth: 1024, # bounded queue; overflow → 503
|
|
267
|
-
queue_timeout: 5.0, # seconds before 503 on a full queue
|
|
268
|
-
request_timeout: nil, # seconds before a slow response becomes a 504 (nil = off)
|
|
269
|
-
max_connections: 8192, # cap concurrent connections; default: most of ulimit -n
|
|
270
|
-
max_body_size: 50 * 1024 * 1024, # bytes before a 413; nil = let a proxy handle it
|
|
271
|
-
on_error: ->(e, env) { ErrorTracker.capture(e) }, # after the client got its 500
|
|
272
|
-
shutdown_timeout: 30, # drain deadline
|
|
273
|
-
control_bind: "127.0.0.1:9293", # monitoring: /stats /metrics /ready /live; port 0 reads back via server.control_port
|
|
274
|
-
control_token: ENV["KINO_CONTROL_TOKEN"], # optional Bearer auth for /stats + /metrics
|
|
275
|
-
tls: { cert: "cert.pem", key: "key.pem" }, # file paths or inline PEM
|
|
276
|
-
http2: true, # ALPN h2 on TLS + plaintext h2c; false = HTTP/1 only
|
|
277
|
-
)
|
|
278
|
-
server.start
|
|
279
|
-
server.shutdown # graceful: drain → deadline → abort stragglers
|
|
280
|
-
```
|
|
273
|
+
To embed it, `Kino::Server.new(app, **options)` returns a server you
|
|
274
|
+
`start` and `shutdown` (graceful: drain → deadline → abort stragglers);
|
|
275
|
+
[every option](doc/configuration.md#server-options), spelled out.
|
|
281
276
|
|
|
282
277
|
### Modes
|
|
283
278
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
I/O, serialized by the GVL for CPU.
|
|
292
|
-
- **`:auto`** (default): `:ractor` when the app is shareable, otherwise
|
|
293
|
-
a warning and `:threaded`. One caveat: a *class* used as a Rack app
|
|
294
|
-
always counts as "shareable" (classes are), even if calling it touches
|
|
295
|
-
unshareable state. Force `:threaded` for those.
|
|
279
|
+
**`:ractor`** runs `workers` Ractors × `threads` Threads each, and the
|
|
280
|
+
app must be `Ractor.shareable?` (frozen middleware, `shareable_proc`
|
|
281
|
+
endpoints). **`:threaded`** is the same machinery on plain Threads and
|
|
282
|
+
runs **any** Rack app, including all of Rails. **`:auto`** (default)
|
|
283
|
+
picks `:ractor` when the app is shareable, otherwise a warning and
|
|
284
|
+
`:threaded`. Either way a crashed worker thread is replaced while the
|
|
285
|
+
others serve on ([Modes](doc/configuration.md#modes)).
|
|
296
286
|
|
|
297
287
|
### Sharded I/O
|
|
298
288
|
|
|
299
|
-
`io_shards true` (off by default) moves HTTP I/O
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
On the 8-core reference box fast handlers gain +1-3% (best with
|
|
304
|
-
`io_threads 8`); the win grows with scheduler contention, so measure on
|
|
305
|
-
your own core count. Ruby-bound endpoints are unchanged. Orthogonal to
|
|
306
|
-
`mode`: it reshapes the Rust side only.
|
|
307
|
-
|
|
308
|
-
```ruby
|
|
309
|
-
# kino.rb
|
|
310
|
-
io_shards true
|
|
311
|
-
io_threads 8 # optional; default: half the available CPUs
|
|
312
|
-
```
|
|
289
|
+
`io_shards true` (off by default) moves HTTP I/O onto current-thread
|
|
290
|
+
shards, each owning its connections for their lifetime: no
|
|
291
|
+
work-stealing, no cross-thread wakeups on the hot path.
|
|
292
|
+
[Sharded I/O](doc/configuration.md#sharded-io) says when it pays.
|
|
313
293
|
|
|
314
294
|
### HTTP/2
|
|
315
295
|
|
|
316
|
-
On by default,
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
- **Plaintext binds** serve prior-knowledge h2c: a client that opens
|
|
321
|
-
with the HTTP/2 preface (an h2-preferring load balancer, a gRPC-style
|
|
322
|
-
backend hop, `curl --http2-prior-knowledge`) gets HTTP/2; ordinary
|
|
323
|
-
clients are HTTP/1.1 exactly as before. Browsers never do h2 on
|
|
324
|
-
plaintext, so a certificate-less kino behaves identically for them.
|
|
325
|
-
|
|
326
|
-
The Rack side is spec-complete on h2: `SERVER_PROTOCOL` is `"HTTP/2"`,
|
|
327
|
-
`HTTP_HOST`/`SERVER_NAME`/`SERVER_PORT` come from the `:authority`
|
|
328
|
-
pseudo-header (h2 requests carry no Host header), split cookie headers
|
|
329
|
-
are rejoined with `"; "`, and streamed uploads flow through the same
|
|
330
|
-
backpressured body channel as HTTP/1. Streams multiplex into the same
|
|
331
|
-
worker slots as keep-alive requests—`workers × threads` bounds
|
|
332
|
-
concurrency either way. (`rack.hijack` stays out on h2 as it is on h1;
|
|
333
|
-
the protocol has no 101 upgrade to hijack anyway.) `http2 false` pins
|
|
334
|
-
the server to HTTP/1 and drops `h2` from ALPN.
|
|
296
|
+
On by default, with nothing to configure: TLS binds advertise `h2` via
|
|
297
|
+
ALPN, plaintext binds serve prior-knowledge h2c, every other client
|
|
298
|
+
stays on HTTP/1.1, and `http2 false` pins the server to HTTP/1. What
|
|
299
|
+
the Rack side sees: [HTTP/2](doc/configuration.md#http2).
|
|
335
300
|
|
|
336
301
|
## Config file and CLI
|
|
337
302
|
|
|
338
303
|
Settings can live in a Puma-style Ruby DSL file: `kino.rb` in the
|
|
339
304
|
working directory, or `config/kino.rb` (the Rails layout), is picked up
|
|
340
305
|
automatically; `-C PATH` names any other. Precedence: explicit kwargs
|
|
341
|
-
and CLI flags > config file > defaults.
|
|
306
|
+
and CLI flags > config file > defaults. `kino --init` writes a sample
|
|
307
|
+
that documents every directive; the flags and a sample file are in
|
|
308
|
+
[doc/configuration.md](doc/configuration.md#config-file-and-cli).
|
|
342
309
|
|
|
343
310
|
```ruby
|
|
344
311
|
# kino.rb
|
|
@@ -348,77 +315,30 @@ threads 1
|
|
|
348
315
|
mode :ractor
|
|
349
316
|
```
|
|
350
317
|
|
|
351
|
-
```sh
|
|
352
|
-
kino --init # write a fully commented sample kino.rb
|
|
353
|
-
kino # config.ru + kino.rb, port 9292
|
|
354
|
-
kino --check # explain whether the app can run in :ractor mode
|
|
355
|
-
kino -C config/kino.rb -p 3000 -w 4 -m ractor my_app.ru
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
The generated sample documents every directive, including the Rails
|
|
359
|
-
settings and the performance notes.
|
|
360
|
-
|
|
361
318
|
## `kino --check`
|
|
362
319
|
|
|
363
|
-
When an app cannot run in `:ractor` mode,
|
|
364
|
-
of leaving you with a bare `Ractor::IsolationError`.
|
|
365
|
-
nothing
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
```
|
|
371
|
-
$ kino --check
|
|
372
|
-
check: app is NOT Ractor-shareable
|
|
373
|
-
- app (Proc at app.rb:12)—captures `cache` = {} (Hash) (unshareable)
|
|
374
|
-
- app (HelloApp).@instance—class-level ivar holds #<HelloApp…>—classes
|
|
375
|
-
pass Ractor.shareable?, but reading this from a worker ractor raises
|
|
376
|
-
Ractor::IsolationError on the first request
|
|
377
|
-
hints: freeze config at boot; build endpoints with Ractor.shareable_proc;
|
|
378
|
-
keep per-worker resources in Ractor.store_if_absent; or run mode :threaded.
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
Exit status is 0/1, so it works in CI. The programmatic form is
|
|
382
|
-
`Kino::Check.report(app)`.
|
|
320
|
+
When an app cannot run in `:ractor` mode, `kino --check` tells you why,
|
|
321
|
+
instead of leaving you with a bare `Ractor::IsolationError`. It changes
|
|
322
|
+
nothing and names each blocker: captured variables with the place they
|
|
323
|
+
were defined, instance variables by path, and the class-level instance
|
|
324
|
+
variable trap that catches class-style apps. Exit status is 0/1, so it
|
|
325
|
+
works in CI ([sample output](doc/configuration.md#kino---check)).
|
|
383
326
|
|
|
384
327
|
## Request timeouts
|
|
385
328
|
|
|
386
|
-
`request_timeout
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
returns, so set the deadline above your slowest legitimate endpoint and
|
|
393
|
-
watch `stats[:timeouts]`.
|
|
394
|
-
|
|
395
|
-
Timeouts guard your app; the network intake guards itself. New
|
|
396
|
-
connections past `max_connections` (default: most of `ulimit -n`) wait
|
|
397
|
-
in the kernel backlog; request bodies past `max_body_size` (default
|
|
398
|
-
50 MB, `nil` delegates to a fronting proxy) get a **413**; and fixed
|
|
399
|
-
deadlines drop slow-header clients (15 s), stalled TLS handshakes
|
|
400
|
-
(10 s), and uploads stalled mid-body (30 s). When a worker catches an
|
|
401
|
-
app or delivery error,
|
|
402
|
-
`on_error ->(error, env) { ErrorTracker.capture(error) }` is called
|
|
403
|
-
after the client got its 500—the only place a tracker sees errors
|
|
404
|
-
raised while the response was being written (in `:ractor` mode, build
|
|
405
|
-
the handler with `Ractor.shareable_proc`).
|
|
329
|
+
`request_timeout 30` (off by default) answers an app slower than that
|
|
330
|
+
with an immediate **504**; the handler is deliberately *not* killed,
|
|
331
|
+
because interrupting arbitrary Ruby mid-flight is unsafe. The network
|
|
332
|
+
intake guards itself with `max_connections`, `max_body_size` (a **413**)
|
|
333
|
+
and fixed deadlines for slow clients, and `on_error` hands what a worker
|
|
334
|
+
caught to your error tracker ([more](doc/configuration.md#request-timeouts)).
|
|
406
335
|
|
|
407
336
|
## Lifecycle hooks
|
|
408
337
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
-
|
|
413
|
-
- `after_request_complete { |env, status| }`: fires inside the worker after each successful response. This is the hot path—leave it unset for zero cost. In `:ractor` mode it must be `Ractor.shareable_proc`.
|
|
414
|
-
|
|
415
|
-
**Main-context hooks** run on the main thread, outside workers, and are plain procs:
|
|
416
|
-
- `after_boot { }`: fires once after the worker pool is up. Wire readiness here—sd_notify, a "server ready" metric, and so on.
|
|
417
|
-
- `on_worker_exit { |worker_index, error| }`: fires when a worker exits, with its index and the crash cause (or nil on a clean exit).
|
|
418
|
-
|
|
419
|
-
`after_worker_boot`'s argument is the worker's slot id, while in `:ractor` mode `on_worker_exit`'s argument identifies the exited ractor (`0`..`workers - 1`)—a different number space—so don't correlate boot and exit by that number in `:ractor` mode.
|
|
420
|
-
|
|
421
|
-
A raising hook is logged and never kills a worker.
|
|
338
|
+
`after_worker_boot` and `after_request_complete` run inside the worker;
|
|
339
|
+
`after_boot` and `on_worker_exit` run on the main thread. A raising
|
|
340
|
+
hook is logged and never kills a worker
|
|
341
|
+
([arguments](doc/configuration.md#lifecycle-hooks)).
|
|
422
342
|
|
|
423
343
|
## Elastic pool (experimental)
|
|
424
344
|
|
|
@@ -431,237 +351,148 @@ max_workers 16 # reached only under load
|
|
|
431
351
|
scale_down_after 30 # seconds idle before an extra worker retires
|
|
432
352
|
```
|
|
433
353
|
|
|
434
|
-
|
|
435
|
-
|
|
354
|
+
While requests wait in the queue, Kino adds a worker every 100 ms.
|
|
355
|
+
**Use it when your app waits**: on databases, upstream services, slow
|
|
356
|
+
clients. [Elastic pool](doc/operations.md#elastic-pool-experimental).
|
|
436
357
|
|
|
437
|
-
|
|
438
|
-
the queue clears or the pool hits `max_workers`. When the load passes,
|
|
439
|
-
workers above `workers` retire one at a time after `scale_down_after`
|
|
440
|
-
seconds idle, each finishing its current request first. Same behavior
|
|
441
|
-
in `:ractor` and `:threaded` mode.
|
|
358
|
+
## Stuck-worker quarantine
|
|
442
359
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
Pure CPU work gains nothing past the core count. In `:ractor` mode Ruby
|
|
448
|
-
itself runs at most `RUBY_MAX_CPU` ractors' Ruby code at once (default
|
|
449
|
-
8), and Kino warns at boot when the pool can exceed it. On a bigger box:
|
|
360
|
+
`quarantine_timeout 60` quarantines a dispatch slot whose request has
|
|
361
|
+
run past the deadline and spawns a replacement worker to restore
|
|
362
|
+
capacity; the wedged worker is never interrupted or force-killed.
|
|
363
|
+
[Quarantine](doc/operations.md#stuck-worker-quarantine).
|
|
450
364
|
|
|
451
|
-
|
|
452
|
-
RUBY_MAX_CPU=16 kino
|
|
453
|
-
```
|
|
365
|
+
## Stats
|
|
454
366
|
|
|
455
|
-
|
|
367
|
+
`server.stats` returns a live snapshot, and `kill -USR1 <pid>` logs it
|
|
368
|
+
as one line. `control_bind "127.0.0.1:9293"` serves a read-only
|
|
369
|
+
**control plane** from the native layer on its own thread, so it keeps
|
|
370
|
+
answering even while every Ruby worker is busy or stuck: `/stats` as
|
|
371
|
+
JSON, `/metrics` for Prometheus, and `/ready` and `/live` probes.
|
|
372
|
+
[Every counter](doc/operations.md#stats).
|
|
456
373
|
|
|
457
374
|
```sh
|
|
458
|
-
$ curl -s localhost:9293/stats | jq '{workers,
|
|
375
|
+
$ curl -s localhost:9293/stats | jq '{state, mode, workers, queued, in_flight, served, rejected}'
|
|
459
376
|
{
|
|
460
|
-
"
|
|
461
|
-
"
|
|
462
|
-
"
|
|
463
|
-
"
|
|
464
|
-
"
|
|
377
|
+
"state": "ready",
|
|
378
|
+
"mode": "ractor",
|
|
379
|
+
"workers": 8,
|
|
380
|
+
"queued": 0,
|
|
381
|
+
"in_flight": 0,
|
|
382
|
+
"served": 200,
|
|
383
|
+
"rejected": 0
|
|
465
384
|
}
|
|
466
385
|
```
|
|
467
386
|
|
|
468
|
-
|
|
469
|
-
`kino_scale_ups_total` and `kino_scale_downs_total`. `after_worker_boot`
|
|
470
|
-
fires for every worker the pool adds, `on_worker_exit` (with a nil
|
|
471
|
-
cause) for every one it retires.
|
|
387
|
+
## Logging
|
|
472
388
|
|
|
473
|
-
|
|
389
|
+
With one log line per request, `Kino::Logger` sustained **1.7× the
|
|
390
|
+
throughput of a shared `::Logger`** (155k vs 90k req/s on the 2026-09
|
|
391
|
+
c7a.2xlarge benchmark box): a lock-free channel feeds a Rust flusher
|
|
392
|
+
thread, so request threads never take a log mutex and never make a
|
|
393
|
+
write syscall. `log_requests true` adds an access log with a timing
|
|
394
|
+
breakdown:
|
|
474
395
|
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
occupied. `quarantine_max` (default: the worker count in `:ractor` mode,
|
|
480
|
-
workers × threads in `:threaded`) caps the total number of replacement
|
|
481
|
-
events over the process lifetime—past it the monitor stops replacing and
|
|
482
|
-
the server runs at reduced capacity.
|
|
483
|
-
|
|
484
|
-
The wedged worker is never interrupted or force-killed, and its slot stays
|
|
485
|
-
quarantined for good. In `:threaded` mode, if the blocked thread
|
|
486
|
-
eventually returns, it keeps serving requests on that same slot—but the
|
|
487
|
-
slot itself stays flagged quarantined (busy_ms reported as 0) for the rest
|
|
488
|
-
of the process; in `:ractor` mode the wedged ractor (and its supervisor
|
|
489
|
-
thread) leaks until the process exits, since a wedged ractor cannot be
|
|
490
|
-
safely interrupted. Monitor quarantine activity via `server.stats`
|
|
491
|
-
(top-level `quarantined` count and per-slot `worker_status[].quarantined`
|
|
492
|
-
flag), `GET /stats` (same), and `GET /metrics` (`kino_quarantined_workers`
|
|
493
|
-
gauge and `kino_quarantine_replacements_total` counter).
|
|
396
|
+
```
|
|
397
|
+
2026-08-22 14:03:11 +0300 → GET /users?q=1 from 127.0.0.1
|
|
398
|
+
2026-08-22 14:03:11 +0300 ← 200 GET /users?q=1 12.4ms (ruby 9.1ms [gc 0.8ms; 1.5k obj]; kino 3.2ms; wait 0.1ms)
|
|
399
|
+
```
|
|
494
400
|
|
|
495
|
-
|
|
401
|
+
Rails and Rack setup, the trade-offs and Kino's own server lines:
|
|
402
|
+
[Logging](doc/operations.md#logging).
|
|
496
403
|
|
|
497
|
-
|
|
498
|
-
from the native layer (one relaxed atomic per request, no measurable
|
|
499
|
-
cost):
|
|
404
|
+
## Timer waits
|
|
500
405
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
#
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
```
|
|
406
|
+
`Kino.sleep(seconds)` is a high-resolution sleep on the OS clock with
|
|
407
|
+
the GVL released, for explicit timer waits in handlers, as MRI's own
|
|
408
|
+
`sleep` wakes up late inside non-main ractors
|
|
409
|
+
([numbers](doc/benchmarks.md#why-io-lags-in-ractor-mode-on-linux)).
|
|
410
|
+
Ordinary blocking I/O does not need it, and under a
|
|
411
|
+
[`worker_loop`](doc/worker-loop.md#waiting-the-right-way) use `sleep`.
|
|
508
412
|
|
|
509
|
-
|
|
510
|
-
(pair it with `pidfile` to find the pid):
|
|
413
|
+
## Rack 3 compliance
|
|
511
414
|
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
415
|
+
The spec suite runs every test app under `Rack::Lint` over real sockets:
|
|
416
|
+
streaming request bodies, enumerable and callable response bodies,
|
|
417
|
+
lowercase and multi-value headers, HEAD/204 semantics, and
|
|
418
|
+
`rack.hijack`, full and partial, on HTTP/1.1 requests that ask to
|
|
419
|
+
upgrade ([Rack 3](doc/streaming.md#rack-3-compliance)).
|
|
515
420
|
|
|
516
|
-
|
|
517
|
-
`unix://` path) serves a read-only **control plane** from the native
|
|
518
|
-
layer on its own thread—it keeps answering even while every Ruby worker
|
|
519
|
-
is busy or stuck, and reports `draining` through a graceful shutdown:
|
|
520
|
-
|
|
521
|
-
- `GET /stats`—the same snapshot as `server.stats`, as JSON (plus
|
|
522
|
-
`state` and `version`).
|
|
523
|
-
- `GET /metrics`—Prometheus text format (`kino_requests_served_total`,
|
|
524
|
-
`kino_queue_depth`, `kino_ready`, …).
|
|
525
|
-
|
|
526
|
-
Both `/stats` and `/metrics` also break the counters down per dispatch
|
|
527
|
-
slot: `/stats` carries a `worker_status` array (`index`, `served`,
|
|
528
|
-
`in_flight`, `busy_ms`) and `/metrics` emits `kino_worker_*{worker="N"}`
|
|
529
|
-
series, one entry per execution slot (`workers × threads`)—a crashed
|
|
530
|
-
worker's slot is never reused, so it stays in the list with its counters
|
|
531
|
-
frozen where they stopped, meaning the array (and its `worker="N"` metric
|
|
532
|
-
series) grows by one across every respawn. `busy_ms` is how long the
|
|
533
|
-
slot's current request has been running (0 when idle), so a single slot
|
|
534
|
-
climbing while the rest sit at 0 is your stuck worker.
|
|
535
|
-
|
|
536
|
-
The `/stats` response and `server.stats` carry `queue_time` (count and
|
|
537
|
-
summed seconds), and `/metrics` exposes `kino_request_queue_seconds`—a
|
|
538
|
-
Prometheus histogram of queue-wait time, the worker-saturation signal.
|
|
539
|
-
Counts admitted requests only; a 503 after queue wait goes to `rejected`,
|
|
540
|
-
not `queue_time`.
|
|
541
|
-
|
|
542
|
-
- `GET /ready`—`200` when serving, `503` while booting or draining:
|
|
543
|
-
wire it to your load balancer or Kubernetes readiness probe.
|
|
544
|
-
- `GET /live`—`200` whenever the process is alive: the liveness probe.
|
|
545
|
-
|
|
546
|
-
`control_token "..."` puts `/stats` and `/metrics` behind
|
|
547
|
-
`Authorization: Bearer`; the probes stay open.
|
|
421
|
+
## Streaming responses
|
|
548
422
|
|
|
549
|
-
|
|
423
|
+
A body that does not respond to `to_ary` streams, and a client slower
|
|
424
|
+
than the app makes the next write wait for it, with the GVL released.
|
|
425
|
+
Once the client has gone, the next write raises `Kino::ClientGone`, so
|
|
426
|
+
an endless server-sent event feed or LLM token stream ends there and
|
|
427
|
+
frees its worker ([Streaming](doc/streaming.md#streaming-responses)).
|
|
550
428
|
|
|
551
|
-
|
|
552
|
-
throughput of a shared `::Logger`** (155k vs 90k req/s on the benchmark
|
|
553
|
-
box). There are two native pieces. Both write through a lock-free
|
|
554
|
-
channel to a Rust flusher thread, so request threads never take a log
|
|
555
|
-
mutex and never make a write syscall:
|
|
556
|
-
|
|
557
|
-
- **Access log** (`log_requests true`): two records per request to
|
|
558
|
-
stdout, including the 503s that never reach your app. The arrival line
|
|
559
|
-
is queued before the app runs, so a request that hangs shows as an
|
|
560
|
-
arrow with no answer; the completion line carries the status, the
|
|
561
|
-
total, and a timing breakdown: `ruby` is the time the request spent in
|
|
562
|
-
Ruby (with the GC pause and the objects allocated during it), `kino`
|
|
563
|
-
the server's own overhead, `wait` the queue time before a worker took
|
|
564
|
-
it. Recommended in development; cheap enough for production. On color
|
|
565
|
-
terminals the completion line is tinted by status class: 2xx green,
|
|
566
|
-
3xx yellow, 4xx maroon, 5xx bright red:
|
|
567
|
-
|
|
568
|
-
```
|
|
569
|
-
2026-08-22 14:03:11 +0300 → GET /users?q=1 from 127.0.0.1
|
|
570
|
-
2026-08-22 14:03:11 +0300 ← 200 GET /users?q=1 12.4ms (ruby 9.1ms [gc 0.8ms; 1.5k obj]; kino 3.2ms; wait 0.1ms)
|
|
571
|
-
```
|
|
572
|
-
|
|
573
|
-
The GC and allocation figures come from the VM's process-wide
|
|
574
|
-
counters, so they appear only where one request at a time can own
|
|
575
|
-
them: in `:threaded` mode, or in `:ractor` mode with `workers 1`.
|
|
576
|
-
Parallel ractors would bill each other's work, so there the breakdown
|
|
577
|
-
is `(ruby; kino; wait)` alone.
|
|
578
|
-
|
|
579
|
-
- **`Kino::Logger`**: a `::Logger` over the same async sink, for your
|
|
580
|
-
app's own logging (`Kino::Logger.new("log/production.log")`, or no
|
|
581
|
-
argument for stdout). The raw IO-like device is `Kino::Logger::Device`,
|
|
582
|
-
for integrations that want bytes without `::Logger` formatting. The
|
|
583
|
-
device is frozen and Ractor-shareable, so one device serves every
|
|
584
|
-
worker.
|
|
585
|
-
|
|
586
|
-
`Kino::Logger` in a **Rails** app: it is a real `::Logger` subclass, so
|
|
587
|
-
it fits anywhere Rails expects a logger:
|
|
429
|
+
## WebSockets (experimental)
|
|
588
430
|
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
config.logger = ActiveSupport::BroadcastLogger.new(
|
|
595
|
-
Kino::Logger.new("log/production.log"), Kino::Logger.new
|
|
596
|
-
)
|
|
597
|
-
# tagged logging wraps it like any ::Logger:
|
|
598
|
-
config.logger = ActiveSupport::TaggedLogging.new(Kino::Logger.new)
|
|
599
|
-
```
|
|
600
|
-
|
|
601
|
-
From a plain **Rack** app, give middleware the logger, or hand
|
|
602
|
-
`Rack::CommonLogger` the raw device (it just calls `write`):
|
|
431
|
+
Kino runs WebSockets natively: Rust does the handshake, framing, pings,
|
|
432
|
+
the close handshake and topic fan-out, and your callbacks run on the
|
|
433
|
+
worker that accepted the socket, so an idle socket costs Ruby nothing.
|
|
434
|
+
Native WebSockets need `lanes true`; Action Cable and other libraries
|
|
435
|
+
with their own stack get `rack.hijack` in either mode.
|
|
603
436
|
|
|
604
437
|
```ruby
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
438
|
+
module Chat
|
|
439
|
+
def self.on_open(ws) = ws.subscribe("chat")
|
|
440
|
+
def self.on_message(ws, data) = Kino::WebSocket.publish("chat", data)
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
app = Ractor.shareable_proc do |env|
|
|
444
|
+
if env["rack.upgrade?"] == :websocket
|
|
445
|
+
env["rack.upgrade"] = Chat
|
|
446
|
+
[101, {}, []] # the headers go into the 101; the status is ignored
|
|
447
|
+
else
|
|
448
|
+
[200, {"content-type" => "text/plain"}, ["hello"]]
|
|
449
|
+
end
|
|
450
|
+
end
|
|
608
451
|
```
|
|
609
452
|
|
|
610
|
-
(
|
|
611
|
-
|
|
612
|
-
reach Rack.)
|
|
613
|
-
|
|
614
|
-
Graceful shutdown drains both logs fully. A hard crash can lose the tail
|
|
615
|
-
of the buffer, and when you log faster than the disk can take (over 100k
|
|
616
|
-
lines/s), the sink drops lines instead of blocking request threads.
|
|
617
|
-
These trade-offs are measured in
|
|
618
|
-
[doc/benchmarks.md](doc/benchmarks.md#logging-costs).
|
|
619
|
-
|
|
620
|
-
**Server lines.** Everything Kino says about itself (draining, a crash
|
|
621
|
-
and its respawn, a hook that raised, quarantine, the stats line,
|
|
622
|
-
`rack.errors`) reads `kino[<pid>] <source>: message`, the source being
|
|
623
|
-
the worker that spoke, `worker-3` (or `worker-3/thread-2` in a
|
|
624
|
-
multi-threaded ractor), or `main`. On color terminals the label is dim
|
|
625
|
-
for notes, yellow for warnings, red for errors; the message stays plain.
|
|
626
|
-
A failed request gets a report instead of a bare backtrace: the request
|
|
627
|
-
line, the error, and where it raised in your code, then the trace with
|
|
628
|
-
your frames first, relative to the working directory, and the rest
|
|
629
|
-
folded:
|
|
453
|
+
[doc/websockets.md](doc/websockets.md) is the guide: callbacks, flow
|
|
454
|
+
control, topics, limits, close codes, `rack.hijack` and Action Cable.
|
|
630
455
|
|
|
631
|
-
|
|
632
|
-
kino[4213] worker-2: 500 GET /boom · RuntimeError: kaboom (app.rb:12:in 'explode')
|
|
633
|
-
app.rb:12:in 'explode'
|
|
634
|
-
/usr/lib/ruby/gems/4.0.0/gems/rack-3.2.7/lib/rack/builder.rb:...
|
|
635
|
-
… 38 more
|
|
636
|
-
```
|
|
456
|
+
## Fibers inside Kino workers (experimental)
|
|
637
457
|
|
|
638
|
-
|
|
639
|
-
|
|
458
|
+
With `worker_loop`, your own fiber scheduler runs inside each worker. A
|
|
459
|
+
request that waits (a database permit, an upstream call, a slow client)
|
|
460
|
+
suspends only its fiber, and the worker takes the next request
|
|
461
|
+
meanwhile, with no extra thread and no extra wake-up per request. Kino
|
|
462
|
+
still owns the Ractors, threads, dispatch and HTTP, and ships no
|
|
463
|
+
scheduler and depends on none; Async is the example:
|
|
640
464
|
|
|
641
|
-
|
|
465
|
+
```ruby
|
|
466
|
+
# kino.rb
|
|
467
|
+
require "async"
|
|
642
468
|
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
469
|
+
threads 1
|
|
470
|
+
worker_loop Ractor.shareable_proc { |worker|
|
|
471
|
+
Sync do |task|
|
|
472
|
+
worker.serve(concurrency: 32) { |job| task.async { job.call } }
|
|
473
|
+
end
|
|
474
|
+
}
|
|
475
|
+
```
|
|
648
476
|
|
|
649
|
-
|
|
477
|
+
[doc/worker-loop.md](doc/worker-loop.md) is the guide.
|
|
650
478
|
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
479
|
+
### Inline connections
|
|
480
|
+
|
|
481
|
+
A `worker_loop` server serves plain HTTP/1.1 connections on its worker
|
|
482
|
+
threads, with nothing to configure and no hand-off, wake-up or task per
|
|
483
|
+
request. TLS, HTTP/2 and anything outside a strict subset of HTTP/1.1
|
|
484
|
+
stay with hyper ([Inline connections](doc/worker-loop.md#inline-connections)).
|
|
656
485
|
|
|
657
486
|
## Rails
|
|
658
487
|
|
|
659
|
-
Rails
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
488
|
+
Rails main partially runs on Ractors: `Rails.application.ractorize!`
|
|
489
|
+
makes a production app shareable, and `examples/rails-hello` runs in
|
|
490
|
+
`:ractor` mode with Active Record on SQLite and a live feed over Kino's
|
|
491
|
+
native WebSockets, following Shopify's
|
|
492
|
+
[ractor-safe-rails](https://github.com/Shopify/ractor-safe-rails)
|
|
493
|
+
template. Apps that need what is not Ractor-safe yet, such as Action
|
|
494
|
+
Cable, stay on `:threaded`, where Action Cable runs unmodified
|
|
495
|
+
([what works](doc/rails-on-ractors.md)).
|
|
665
496
|
|
|
666
497
|
## Development
|
|
667
498
|
|
|
@@ -681,7 +512,7 @@ For the Rust network stack, thanks to [Sean McArthur](https://github.com/seanmon
|
|
|
681
512
|
|
|
682
513
|
## Assisted by
|
|
683
514
|
|
|
684
|
-
Claude Code (Fable 5, Opus
|
|
515
|
+
Claude Code (Fable 5.2, Opus 5.5).
|
|
685
516
|
|
|
686
517
|
## Contributing
|
|
687
518
|
|