kino 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +5 -0
  3. data/CHANGELOG.md +286 -0
  4. data/Cargo.lock +209 -132
  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 +19 -3
  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
@@ -20,6 +20,7 @@ use std::fmt::Write as _;
20
20
  use std::net::IpAddr;
21
21
  use std::time::Duration;
22
22
 
23
+ use crate::logsink::Sink;
23
24
  use crate::style::{self, Stream};
24
25
 
25
26
  /// Per-request timing, measured on the way through and riding the
@@ -36,6 +37,45 @@ pub struct Timing {
36
37
  pub gc: Option<(Duration, u64)>,
37
38
  }
38
39
 
40
+ /// A request's two records in the access log, whichever way it is served: made as its arrival
41
+ /// line is queued, spent on its completion line.
42
+ pub struct Logged {
43
+ method: String,
44
+ target: String,
45
+ start: std::time::Instant,
46
+ }
47
+
48
+ impl Logged {
49
+ /// Queues the arrival line on `log` now, before the app sees the request, so a hang shows
50
+ /// as an arrow with no answer.
51
+ pub fn arrival(log: &Sink, method: &http::Method, uri: &http::Uri, from: IpAddr) -> Logged {
52
+ let target = match uri.query() {
53
+ Some(query) => format!("{}?{query}", uri.path()),
54
+ None => uri.path().to_string(),
55
+ };
56
+ let method = method.to_string();
57
+ log.write_line(arrival(&method, &target, from));
58
+ Logged {
59
+ method,
60
+ target,
61
+ start: std::time::Instant::now(),
62
+ }
63
+ }
64
+
65
+ /// Since its arrival: its total, once its answer begins.
66
+ pub fn elapsed(&self) -> Duration {
67
+ self.start.elapsed()
68
+ }
69
+
70
+ /// Queues the completion line, with the breakdown where a worker measured `timing`.
71
+ /// A thread-safe free may run it (an inline give-up's 500), so it keeps typed_data.rs's
72
+ /// promise: it formats and queues, no more.
73
+ pub fn completion(self, log: &Sink, status: u16, total: Duration, timing: Option<Timing>) {
74
+ let line = completion(status, &self.method, &self.target, total, timing);
75
+ log.write_line(line);
76
+ }
77
+ }
78
+
39
79
  /// The arrival record, stamped and styled for stdout.
40
80
  pub fn arrival(method: &str, target: &str, ip: IpAddr) -> String {
41
81
  let color = style::enabled(Stream::Stdout);
@@ -141,105 +181,43 @@ fn write_count(out: &mut String, n: u64) {
141
181
 
142
182
  #[cfg(test)]
143
183
  mod tests {
144
- use super::{arrival_line, completion_line, now_stamp, write_breakdown, write_count, Timing};
145
- use std::net::{IpAddr, Ipv4Addr};
146
184
  use std::time::Duration;
147
185
 
148
- fn breakdown(total: Duration, timing: &Timing) -> String {
149
- let mut out = String::new();
150
- write_breakdown(&mut out, total, timing);
151
- out
152
- }
153
-
154
- fn count(n: u64) -> String {
155
- let mut out = String::new();
156
- write_count(&mut out, n);
157
- out
158
- }
186
+ use super::{write_breakdown, write_count, Timing};
159
187
 
188
+ // The records' shape, their stamp and the blank line after a completion:
189
+ // spec/logging_spec.rb. What the breakdown's numbers are is checked here.
160
190
  #[test]
161
- fn arrival_line_carries_method_target_and_ip() {
162
- let ip = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 5));
163
- assert_eq!(
164
- arrival_line("GET", "/users?q=1", ip),
165
- "\u{2192} GET /users?q=1 from 203.0.113.5"
166
- );
167
- }
168
-
169
- #[test]
170
- fn completion_line_carries_status_target_and_total() {
171
- assert_eq!(
172
- completion_line(200, "GET", "/users", Duration::from_millis(12)),
173
- "\u{2190} 200 GET /users 12.0ms"
174
- );
175
- }
176
-
177
- #[test]
178
- fn breakdown_splits_the_total_into_ruby_kino_and_wait() {
179
- let timing = Timing {
180
- wait: Duration::from_micros(100),
181
- ruby: Duration::from_millis(41),
182
- gc: Some((Duration::from_micros(9_700), 52_000)),
183
- };
184
- // kino = total - ruby - wait = 45.2 - 41.0 - 0.1 = 4.1ms.
185
- assert_eq!(
186
- breakdown(Duration::from_micros(45_200), &timing),
187
- "(ruby 41.0ms [gc 9.7ms; 52k obj]; kino 4.1ms; wait 0.1ms)"
188
- );
189
- }
190
-
191
- #[test]
192
- fn breakdown_leaves_out_the_gc_bracket_when_not_measured() {
193
- let timing = Timing {
194
- wait: Duration::from_millis(1),
195
- ruby: Duration::from_millis(2),
196
- gc: None,
197
- };
198
- assert_eq!(
199
- breakdown(Duration::from_millis(4), &timing),
200
- "(ruby 2.0ms; kino 1.0ms; wait 1.0ms)"
201
- );
202
- }
203
-
204
- #[test]
205
- fn breakdown_floors_kino_at_zero() {
206
- let timing = Timing {
207
- wait: Duration::from_millis(1),
208
- ruby: Duration::from_millis(40),
209
- gc: Some((Duration::ZERO, 10)),
210
- };
211
- assert_eq!(
212
- breakdown(Duration::from_millis(40), &timing),
213
- "(ruby 40.0ms [gc 0.0ms; 10 obj]; kino 0.0ms; wait 1.0ms)"
214
- );
191
+ fn a_breakdown_splits_the_total_into_ruby_kino_and_wait_with_the_gc_bracket_when_measured() {
192
+ let us = Duration::from_micros;
193
+ // The total, wait, ruby and GC pause in microseconds with the objects allocated, and the
194
+ // breakdown: kino is what is left, floored at zero. One row per case; rustfmt would split
195
+ // each row field by field.
196
+ #[rustfmt::skip]
197
+ let rows = [
198
+ (45_200, 100, 41_000, Some((9_700, 52_000)), "(ruby 41.0ms [gc 9.7ms; 52k obj]; kino 4.1ms; wait 0.1ms)"),
199
+ (4_000, 1_000, 2_000, None, "(ruby 2.0ms; kino 1.0ms; wait 1.0ms)"),
200
+ (40_000, 1_000, 40_000, Some((0, 10)), "(ruby 40.0ms [gc 0.0ms; 10 obj]; kino 0.0ms; wait 1.0ms)"),
201
+ ];
202
+ for (total, wait, ruby, gc, expected) in rows {
203
+ let gc = gc.map(|(pause, objects)| (us(pause), objects));
204
+ let timing = Timing {
205
+ wait: us(wait),
206
+ ruby: us(ruby),
207
+ gc,
208
+ };
209
+ let mut breakdown = String::new();
210
+ write_breakdown(&mut breakdown, us(total), &timing);
211
+ assert_eq!(breakdown, expected);
212
+ }
215
213
  }
216
214
 
217
215
  #[test]
218
216
  fn count_humanizes_allocation_counts() {
219
- assert_eq!(count(0), "0");
220
- assert_eq!(count(523), "523");
221
- assert_eq!(count(1500), "1.5k");
222
- assert_eq!(count(52_000), "52k");
223
- }
224
-
225
- #[test]
226
- fn timestamp_is_date_time_and_numeric_offset() {
227
- // `YYYY-MM-DD HH:MM:SS +HHMM`, e.g. `2026-08-22 14:03:11 +0300`.
228
- let ts = now_stamp();
229
- let (datetime, offset) = ts.rsplit_once(' ').expect("offset field");
230
- let (date, time) = datetime.split_once(' ').expect("date and time");
231
- assert_eq!(date.len(), 10, "date {date}");
232
- assert_eq!(&date[4..5], "-");
233
- assert_eq!(time.len(), 8, "time {time}");
234
- assert_eq!(&time[2..3], ":");
235
- assert_eq!(offset.len(), 5, "offset {offset}");
236
- assert!(matches!(&offset[0..1], "+" | "-"));
237
- assert!(offset[1..].bytes().all(|b| b.is_ascii_digit()));
238
- }
239
-
240
- #[test]
241
- fn completion_ends_with_the_blank_line_newline() {
242
- let line = super::completion(200, "GET", "/", Duration::from_millis(1), None);
243
- assert!(line.ends_with("\u{2190} 200 GET / 1.0ms\n") || line.ends_with("1.0ms\x1b[0m\n"));
217
+ for (n, humanized) in [(0, "0"), (523, "523"), (1500, "1.5k"), (52_000, "52k")] {
218
+ let mut count = String::new();
219
+ write_count(&mut count, n);
220
+ assert_eq!(count, humanized);
221
+ }
244
222
  }
245
223
  }