kino 0.7.2 → 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 +281 -0
- data/Cargo.lock +164 -95
- 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 +17 -1
- 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/doc/rails-on-ractors.md
CHANGED
|
@@ -1,51 +1,163 @@
|
|
|
1
1
|
# Rails on Kino, and the state of Rails-on-Ractors
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
`
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
`
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
3
|
+
Rails main partially runs on Ractors: `Rails.application.ractorize!`
|
|
4
|
+
makes a booted production app shareable, and Kino serves it from worker
|
|
5
|
+
Ractors. `examples/rails-hello` is a single-file Rails app that does
|
|
6
|
+
exactly that. Its Ractor setup follows Shopify's
|
|
7
|
+
[ractor-safe-rails](https://github.com/Shopify/ractor-safe-rails)
|
|
8
|
+
template. The blockers listed here in June 2026 (unshareable boot-time
|
|
9
|
+
procs, class-level state read from worker Ractors) were resolved
|
|
10
|
+
upstream by `ractorize!`.
|
|
11
|
+
|
|
12
|
+
## How to run it
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
cd examples/rails-hello
|
|
16
|
+
bundle exec kino # mode :ractor, from kino.rb
|
|
17
|
+
bundle exec kino --mode threaded # the same app on plain threads
|
|
18
|
+
bundle exec kino --check # reports the app Ractor-shareable
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Keep `bundle exec`: the example needs the Gemfile's Rails main, not an
|
|
22
|
+
installed Rails release. `config.ru` calls `HelloApp.ractorize!` and
|
|
23
|
+
runs `HelloApp.instance` (running the class works too, but `--check`
|
|
24
|
+
flags its class-level ivars). Puma in single mode serves it
|
|
25
|
+
(`bundle exec puma config.ru`); in cluster mode it hangs, because the
|
|
26
|
+
Ractor-safe logger's writer thread does not survive fork.
|
|
27
|
+
|
|
28
|
+
## What works
|
|
29
|
+
|
|
30
|
+
Verified on the example in `:ractor` mode:
|
|
31
|
+
|
|
32
|
+
- Controllers and routes, including the root route and a posts
|
|
33
|
+
resource.
|
|
34
|
+
- Active Record on SQLite: reads, inserts, and a table that persists
|
|
35
|
+
across restarts.
|
|
36
|
+
- Rendering plain text and JSON.
|
|
37
|
+
- Validations: an over-long post body returns 422 with the validation
|
|
38
|
+
message.
|
|
39
|
+
- Error pages: unknown paths return 404, a request missing a required
|
|
40
|
+
parameter returns 400, and the server keeps serving.
|
|
41
|
+
- Concurrent reads from several worker Ractors, with no Ractor errors.
|
|
42
|
+
- A live feed of new posts over Kino's native WebSockets (below).
|
|
43
|
+
|
|
44
|
+
### Live posts
|
|
45
|
+
|
|
46
|
+
`/live` is a WebSocket that gets every post created after it connects,
|
|
47
|
+
one text message per post, in the JSON that `POST /posts` returns. With
|
|
48
|
+
the example running, open <http://localhost:9292> and run this in the
|
|
49
|
+
browser console:
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
new WebSocket("ws://localhost:9292/live").onmessage = e => console.log(e.data)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Then create a post, and the console logs it:
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
curl -d body="Hello, live" localhost:9292/posts
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Action Cable does not run on Ractors yet: `ActionCable.server` reads
|
|
62
|
+
its config from a class variable, which worker Ractors cannot read, and
|
|
63
|
+
websocket-driver unmasks frames in a C extension that is not marked
|
|
64
|
+
Ractor-safe. The feed uses Kino's own WebSockets instead. Each socket
|
|
65
|
+
subscribes to a `posts` topic when it opens, and `PostsController#create`
|
|
66
|
+
publishes to it after a save; Kino sends each message to every
|
|
67
|
+
subscriber, whichever worker holds its socket. Native WebSockets need
|
|
68
|
+
`lanes true`, which the example's `kino.rb` sets. Under Puma, `/live`
|
|
69
|
+
answers 426 and posts are not published.
|
|
70
|
+
|
|
71
|
+
## What the example sets, and why
|
|
72
|
+
|
|
73
|
+
Most of these fixed a concrete failure. The first block is gems, the
|
|
74
|
+
rest is app code.
|
|
75
|
+
|
|
76
|
+
**Gems** (`Gemfile`)
|
|
77
|
+
|
|
78
|
+
- **Rails main.** `ractorize!` exists only there.
|
|
79
|
+
- **rack main.** Boot fails in `ractorize!` with `rack/ractorize not
|
|
80
|
+
available` because no released rack ships that file.
|
|
81
|
+
- **Shopify's i18n `ractor_support` branch.** Released i18n keeps its
|
|
82
|
+
config in class variables, which worker Ractors cannot read: every
|
|
83
|
+
request answers 500 with `can not access class variables from
|
|
84
|
+
non-main Ractors`.
|
|
85
|
+
|
|
86
|
+
**Settings** (`app.rb` and `config.ru`)
|
|
87
|
+
|
|
88
|
+
- **`ENV["RAILS_ENV"] ||= "production"`.** The ractorized app only boots
|
|
89
|
+
in production.
|
|
90
|
+
- **`ActiveSupport::Ractors.unshareable_proc_action = :raise`.** Without
|
|
91
|
+
it, boot fails making a boot-time proc shareable (`Proc's self is not
|
|
92
|
+
shareable`).
|
|
93
|
+
- **`ActiveSupport::TaggedLogging.ractor_logger($stdout)`.** A plain
|
|
94
|
+
`Logger` holds a `Monitor`, which `ractorize!` cannot make shareable.
|
|
95
|
+
- **`config.i18n.fallbacks = true`.** Without it, every render reads
|
|
96
|
+
`I18n.fallbacks` from a class variable and answers 500.
|
|
97
|
+
- **`require "i18n/ractorize"` after `ractorize!`.** It makes I18n's
|
|
98
|
+
backend shareable. Without it, any `I18n.t` from a worker, such as a
|
|
99
|
+
validation message, answers 500 with an isolation error on
|
|
100
|
+
`I18n::Config`.
|
|
101
|
+
- **`config.cache_store = :null_store`.** The default cache store is
|
|
102
|
+
not Ractor-safe, so this protects anyone who adds `Rails.cache`.
|
|
103
|
+
- **`config.public_file_server.enabled = false`.** Carried over from
|
|
104
|
+
the template; not needed by the example.
|
|
105
|
+
- **Routes through `routes.append` inside the app class, and controllers
|
|
106
|
+
defined before `initialize!`.** Boot freezes the route set, so a
|
|
107
|
+
later `routes.draw` fails, and the eager-loading boot looks up each
|
|
108
|
+
route's controller.
|
|
109
|
+
- **`render body: payload.to_json, content_type: "application/json"`
|
|
110
|
+
instead of `render json:`.** The JSON renderer is defined with an
|
|
111
|
+
unshareable block, so worker Ractors cannot call it.
|
|
112
|
+
- **`Post.define_attribute_methods` in the main Ractor.** Without it the
|
|
113
|
+
first boot passes, but after a restart over existing rows every
|
|
114
|
+
`/posts` request fails with an isolation error on a generated
|
|
115
|
+
attribute-methods constant.
|
|
116
|
+
- **`max_connections=64` in the database URL.** Worker Ractors send
|
|
117
|
+
every database call through one main-Ractor thread; a checkout that
|
|
118
|
+
waits on the pool blocks the check-ins that would free a connection,
|
|
119
|
+
and requests time out. Keep it at least workers × threads.
|
|
120
|
+
- **No SQLite busy timeout.** See the limitations below.
|
|
121
|
+
- **`Rails.backtrace_cleaner.remove_filters!`.** Ruby 4.0.9 crashes when
|
|
122
|
+
a Ractor-shareable proc calls `String#sub` with a regex from many
|
|
123
|
+
Ractors at once. Rails' backtrace cleaner does exactly that on
|
|
124
|
+
concurrent errors, so the example clears its filters. Logged
|
|
125
|
+
backtraces keep full paths.
|
|
126
|
+
|
|
127
|
+
## What does not work yet
|
|
128
|
+
|
|
129
|
+
As of Rails main and Ruby 4.0.9:
|
|
130
|
+
|
|
131
|
+
- **Database work does not scale with workers.** Rails runs every worker
|
|
132
|
+
Ractor's database call on one main-Ractor thread, through
|
|
133
|
+
ractor-dispatch's single executor thread. Concurrent SQLite write
|
|
134
|
+
transactions from several Ractors also contend. The example sets no
|
|
135
|
+
busy timeout, so the loser fails fast with a 500 (`database is
|
|
136
|
+
locked`) instead of stalling every worker while it waits.
|
|
137
|
+
- **Action Cable, the default cache store, the Solid gems and the async
|
|
138
|
+
Active Job adapter are not Ractor-safe yet.** Apps that need them stay
|
|
139
|
+
on `:threaded`.
|
|
140
|
+
- **Built-in renderers such as `render json:` fail from worker
|
|
141
|
+
Ractors.** `Renderers.add` defines them with a plain block. The
|
|
142
|
+
example renders JSON with `render body:`.
|
|
143
|
+
- **Attribute methods must be defined in the main Ractor at boot.** Call
|
|
144
|
+
`define_attribute_methods` on each model; a worker cannot define them.
|
|
145
|
+
- **Puma cluster mode hangs.** The Ractor-safe logger starts a writer
|
|
146
|
+
thread at boot, and it does not survive fork. Puma single mode works.
|
|
147
|
+
- **Production only.** The ractorized app only boots in production (the
|
|
148
|
+
example defaults `RAILS_ENV` to production).
|
|
149
|
+
- **The regex crash above** is a Ruby VM bug; the backtrace filters stay
|
|
150
|
+
off until it is fixed.
|
|
151
|
+
- **Branch gems.** The example needs rack main and the i18n branch until
|
|
152
|
+
their Ractor support is released.
|
|
153
|
+
|
|
154
|
+
## Threaded mode
|
|
155
|
+
|
|
156
|
+
`:threaded` runs any Rails app, including the parts above. Pair it with
|
|
157
|
+
production-style settings: eager load, no code reloading, a database
|
|
158
|
+
pool of at least workers × threads, and a logger on stdout or another
|
|
159
|
+
thread-safe device. The Rails numbers in the
|
|
160
|
+
[README](../README.md#rails) and [benchmarks](benchmarks.md#rails) were
|
|
161
|
+
measured in `:threaded` mode on the controller-only hello app
|
|
162
|
+
(`bench/rails_hello.ru`), before Rails could run on Ractors; ractor-mode
|
|
163
|
+
Rails is not benchmarked yet.
|
data/doc/streaming.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Rack 3 and streaming responses
|
|
2
|
+
|
|
3
|
+
What Kino's Rack 3 support covers, and how a streamed response behaves
|
|
4
|
+
when its client is slow or gone. How the body channels work inside is
|
|
5
|
+
in [architecture.md](architecture.md#backpressure-in-both-directions).
|
|
6
|
+
|
|
7
|
+
## Rack 3 compliance
|
|
8
|
+
|
|
9
|
+
The spec suite runs every test app under `Rack::Lint` over real sockets:
|
|
10
|
+
streaming request bodies (forward-only `rack.input`), enumerable and
|
|
11
|
+
callable (full-duplex stream) response bodies, lowercase and multi-value
|
|
12
|
+
headers, HEAD/204 semantics. `rack.hijack`, full and partial, is offered
|
|
13
|
+
on HTTP/1.1 requests that ask to upgrade (see
|
|
14
|
+
[WebSockets](websockets.md#rackhijack-and-action-cable)).
|
|
15
|
+
|
|
16
|
+
## Streaming responses
|
|
17
|
+
|
|
18
|
+
A body that does not respond to `to_ary` streams: each chunk goes out
|
|
19
|
+
as the body yields it (chunked unless the app set `content-length`),
|
|
20
|
+
and a Rack 3 streaming body, one that responds to `call` but not
|
|
21
|
+
`each`, writes to the stream it is called with. A client slower than
|
|
22
|
+
the app makes the next write wait for it, with the GVL released.
|
|
23
|
+
|
|
24
|
+
Once the client has gone (it hung up, or its connection broke), the
|
|
25
|
+
body's next write raises `Kino::ClientGone`, an `IOError`, so an endless
|
|
26
|
+
body such as a server-sent event feed or an LLM token stream ends there
|
|
27
|
+
and frees its worker. A response that can carry no body (a HEAD
|
|
28
|
+
request, a 204 or 304, one already answered with a `request_timeout`
|
|
29
|
+
504) ends its body the same way. Kino closes the body and runs
|
|
30
|
+
`after_request_complete` as for any finished response, and logs
|
|
31
|
+
nothing; an `ensure` in the body is the place to clean up:
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
class Ticks
|
|
35
|
+
def each
|
|
36
|
+
loop do
|
|
37
|
+
yield "data: #{Time.now.to_i}\n\n"
|
|
38
|
+
Kino.sleep(1)
|
|
39
|
+
end
|
|
40
|
+
ensure
|
|
41
|
+
Kino::Log.info("ticker stopped")
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
app = ->(env) { [200, {"content-type" => "text/event-stream"}, Ticks.new] }
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
A body that raises anything else mid-stream is logged and reported to
|
|
49
|
+
`on_error`, and its client sees the connection cut rather than a
|
|
50
|
+
response that looks complete. Cutting a stream short never waits on a
|
|
51
|
+
client that stopped reading, so such a client cannot hold up the worker
|
|
52
|
+
or a shutdown.
|