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.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +5 -0
  3. data/CHANGELOG.md +281 -0
  4. data/Cargo.lock +164 -95
  5. data/README.md +269 -438
  6. data/doc/README.md +27 -2
  7. data/doc/architecture.md +862 -43
  8. data/doc/benchmarks.md +444 -214
  9. data/doc/configuration.md +215 -0
  10. data/doc/operations.md +258 -0
  11. data/doc/rails-on-ractors.md +161 -49
  12. data/doc/streaming.md +52 -0
  13. data/doc/websockets.md +469 -0
  14. data/doc/why-kino.md +6 -6
  15. data/doc/worker-loop.md +462 -0
  16. data/ext/kino/Cargo.toml +17 -1
  17. data/ext/kino/build.rs +32 -1
  18. data/ext/kino/clippy.toml +10 -0
  19. data/ext/kino/src/access_log.rs +70 -92
  20. data/ext/kino/src/control.rs +26 -685
  21. data/ext/kino/src/counters.rs +132 -0
  22. data/ext/kino/src/cpus.rs +1 -18
  23. data/ext/kino/src/dispatch/tests.rs +731 -0
  24. data/ext/kino/src/dispatch.rs +276 -0
  25. data/ext/kino/src/doorbell.rs +108 -0
  26. data/ext/kino/src/env_strings.rs +26 -30
  27. data/ext/kino/src/framing.rs +110 -0
  28. data/ext/kino/src/gvl.rs +292 -66
  29. data/ext/kino/src/hijack/tests.rs +416 -0
  30. data/ext/kino/src/hijack.rs +471 -0
  31. data/ext/kino/src/inline/answer.rs +488 -0
  32. data/ext/kino/src/inline/conn.rs +1124 -0
  33. data/ext/kino/src/inline/deadline.rs +421 -0
  34. data/ext/kino/src/inline/encode.rs +119 -0
  35. data/ext/kino/src/inline/io.rs +487 -0
  36. data/ext/kino/src/inline/mod.rs +766 -0
  37. data/ext/kino/src/inline/parity.rs +862 -0
  38. data/ext/kino/src/inline/parse.rs +131 -0
  39. data/ext/kino/src/inline/spin.rs +428 -0
  40. data/ext/kino/src/inline/testing.rs +628 -0
  41. data/ext/kino/src/io_shards.rs +240 -192
  42. data/ext/kino/src/lane_room.rs +99 -0
  43. data/ext/kino/src/lib.rs +139 -77
  44. data/ext/kino/src/listen.rs +180 -12
  45. data/ext/kino/src/log.rs +12 -47
  46. data/ext/kino/src/logsink.rs +3 -9
  47. data/ext/kino/src/mono.rs +12 -17
  48. data/ext/kino/src/pin.rs +3 -3
  49. data/ext/kino/src/queue/mod.rs +54 -0
  50. data/ext/kino/src/queue/take.rs +503 -0
  51. data/ext/kino/src/queue/testing.rs +107 -0
  52. data/ext/kino/src/queue/tests.rs +581 -0
  53. data/ext/kino/src/queue/worker.rs +501 -0
  54. data/ext/kino/src/registry.rs +168 -379
  55. data/ext/kino/src/request.rs +834 -235
  56. data/ext/kino/src/response.rs +532 -94
  57. data/ext/kino/src/server/accept.rs +322 -0
  58. data/ext/kino/src/server/config.rs +138 -0
  59. data/ext/kino/src/server/handle.rs +247 -0
  60. data/ext/kino/src/server/intake.rs +217 -0
  61. data/ext/kino/src/server/mod.rs +157 -0
  62. data/ext/kino/src/server/natives.rs +223 -0
  63. data/ext/kino/src/server/serve/intake_tests.rs +458 -0
  64. data/ext/kino/src/server/serve.rs +436 -0
  65. data/ext/kino/src/server/tests.rs +532 -0
  66. data/ext/kino/src/shared_gvl.rs +50 -0
  67. data/ext/kino/src/slot/lane.rs +138 -0
  68. data/ext/kino/src/slot/life.rs +111 -0
  69. data/ext/kino/src/slot/load.rs +193 -0
  70. data/ext/kino/src/slot/mod.rs +494 -0
  71. data/ext/kino/src/slot/park.rs +82 -0
  72. data/ext/kino/src/stats.rs +936 -0
  73. data/ext/kino/src/symbols.rs +66 -0
  74. data/ext/kino/src/test_support.rs +24 -14
  75. data/ext/kino/src/testing.rs +409 -0
  76. data/ext/kino/src/ticker.rs +87 -0
  77. data/ext/kino/src/timer.rs +4 -7
  78. data/ext/kino/src/tls.rs +101 -131
  79. data/ext/kino/src/typed_data.rs +131 -0
  80. data/ext/kino/src/upgrade.rs +77 -0
  81. data/ext/kino/src/ws/codec.rs +470 -0
  82. data/ext/kino/src/ws/drive.rs +600 -0
  83. data/ext/kino/src/ws/handshake.rs +267 -0
  84. data/ext/kino/src/ws/mod.rs +317 -0
  85. data/ext/kino/src/ws/server_tests.rs +759 -0
  86. data/ext/kino/src/ws/socket.rs +279 -0
  87. data/ext/kino/src/ws/stats.rs +88 -0
  88. data/ext/kino/src/ws/testing.rs +156 -0
  89. data/ext/kino/src/ws/topics.rs +550 -0
  90. data/lib/kino/cli.rb +100 -9
  91. data/lib/kino/configuration.rb +32 -10
  92. data/lib/kino/crash_backoff.rb +56 -0
  93. data/lib/kino/input.rb +36 -17
  94. data/lib/kino/log.rb +8 -1
  95. data/lib/kino/monitor.rb +1 -1
  96. data/lib/kino/pool.rb +169 -0
  97. data/lib/kino/pool_scaler.rb +12 -13
  98. data/lib/kino/quarantine_monitor.rb +3 -3
  99. data/lib/kino/ractor_supervisor.rb +201 -161
  100. data/lib/kino/server.rb +291 -185
  101. data/lib/kino/slot_bank.rb +21 -9
  102. data/lib/kino/stream.rb +2 -2
  103. data/lib/kino/teardown.rb +33 -0
  104. data/lib/kino/templates/kino.rb.tt +41 -6
  105. data/lib/kino/threaded_pool.rb +87 -102
  106. data/lib/kino/version.rb +1 -1
  107. data/lib/kino/web_socket.rb +125 -0
  108. data/lib/kino/worker/loop.rb +319 -0
  109. data/lib/kino/worker.rb +345 -79
  110. data/lib/kino/worker_hooks.rb +7 -1
  111. data/lib/kino/worker_row.rb +15 -0
  112. data/lib/kino.rb +27 -3
  113. data/lib/rackup/handler/kino.rb +1 -1
  114. data/sig/kino.rbs +93 -10
  115. metadata +66 -3
  116. data/ext/kino/src/queue.rs +0 -333
  117. data/ext/kino/src/server.rs +0 -1785
@@ -1,51 +1,163 @@
1
1
  # Rails on Kino, and the state of Rails-on-Ractors
2
2
 
3
- `examples/rails-hello` runs edge Rails on Kino.
4
-
5
- ## What works as of Kino 0.1.0: `:threaded` mode
6
-
7
- Rails 8.2.0.alpha boots and serves with `mode :threaded` (see the
8
- example's `kino.rb`; just `bundle exec kino` in that directory). Measured
9
- on the hello-world (c7a.2xlarge, 8 cores, production mode, 8×5):
10
- ~2.6k req/s in 92 MB PSS, single process. The 8-worker Puma cluster
11
- reaches ~12.1k by parallelizing across forks, at 389 MB PSS (794 MB RSS,
12
- but its forks share the framework copy-on-write, so PSS is the fair
13
- figure)—Rails-on-Ractors is interesting precisely because it could offer
14
- that ~4.6× parallelism at ~1/4th of the memory.
15
-
16
- Pair it with production-style Rails settings: eager load, no code
17
- reloading, database pool ≥ workers × threads, logger to stdout or another
18
- thread-safe device.
19
-
20
- ## What doesn't (yet): `:ractor` mode—with the exact blockers
21
-
22
- `examples/rails-hello/ractor_probe.rb` re-tests this against whatever
23
- Rails is bundled; when it prints SUCCEEDED *and* a 200 ractor-mode
24
- response (sharing the app is only the first blocker), flip
25
- `mode :ractor`. As of June 2026 (rails main):
26
-
27
- 1. **Sharing the booted app fails.**
28
- `Ractor.make_shareable(Rails.application)` raises
29
- `Ractor::IsolationError: Proc's self is not shareable` at
30
- `railties/lib/rails/application/finisher.rb:151`—a routes-reloader
31
- hook lambda Rails registers at boot (in the development path) whose
32
- `self` is the unshareable application instance.
33
- 2. **Calling the app from a worker ractor fails** even where
34
- `make_shareable` succeeds on a class:
35
- `Ractor::IsolationError: can not get unshareable values from instance
36
- variables of classes/modules from non-main Ractors (@instance from
37
- HelloApp)`—`Rails.application` is a class-level ivar read on the
38
- dispatch path.
39
- 3. **Every workaround is closed by one VM rule** (verified on Ruby 4.0.5):
40
- class instance variables are main-ractor-only—non-main ractors can
41
- neither read them (when the value is unshareable) nor set them, **even
42
- inside a `Ruby::Box`** (`RUBY_BOX=1`). Box isolates constant tables,
43
- but Ractor isolation still applies to box-local classes:
44
-
45
- ```
46
- Ractor::IsolationError: can not set instance variables of
47
- classes/modules by non-main Ractors
48
- ```
49
-
50
- So booting a private Rails per worker ractor (boxed or not) dies on
51
- the first class-ivar write, and sharing one boot dies on reads.
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.