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
@@ -0,0 +1,488 @@
1
+ //! The answers to an inline connection's requests, written on its socket under its lock: the
2
+ //! app's, whole or streamed, and Kino's own (a give-up's 500, a request_timeout's 504).
3
+
4
+ use std::io::Write;
5
+ use std::net::Shutdown;
6
+ use std::os::fd::{AsRawFd, RawFd};
7
+ use std::sync::atomic::Ordering;
8
+ use std::sync::Arc;
9
+ use std::time::Duration;
10
+
11
+ use http::{HeaderMap, StatusCode};
12
+
13
+ use super::encode::{self, Body};
14
+ use super::io::{InlineIo, IoState};
15
+ use crate::access_log::{Logged, Timing};
16
+ use crate::response::{Cut, Responder};
17
+ use crate::server::{InFlight, HEADER_READ_TIMEOUT};
18
+ use crate::slot::WorkerSlot;
19
+
20
+ /// Kino's own answer in place of the app's, after which the connection closes.
21
+ struct Canned {
22
+ status: u16,
23
+ head: &'static [u8],
24
+ body: &'static [u8],
25
+ }
26
+
27
+ /// A give-up's 500, as response::plain_response(500, ..).
28
+ const GIVE_UP: Canned = Canned {
29
+ status: 500,
30
+ head:
31
+ b"HTTP/1.1 500 Internal Server Error\r\n\
32
+ content-type: text/plain\r\nconnection: close\r\ncontent-length: 22\r\nserver: Kino\r\n\r\n",
33
+ body: b"Internal Server Error\n",
34
+ };
35
+ const _: () = assert!(GIVE_UP.body.len() == 22);
36
+
37
+ /// hyper's 504 (server::handle), but closing the connection: its job still runs the app, so no
38
+ /// next request could be read.
39
+ const TIMED_OUT: Canned = Canned {
40
+ status: 504,
41
+ head:
42
+ b"HTTP/1.1 504 Gateway Timeout\r\n\
43
+ content-type: text/plain\r\nconnection: close\r\ncontent-length: 16\r\nserver: Kino\r\n\r\n",
44
+ body: b"Gateway Timeout\n",
45
+ };
46
+ const _: () = assert!(TIMED_OUT.body.len() == 16);
47
+
48
+ /// hyper's answer to a 1xx other than 101: a bare 500, after which the connection ends.
49
+ const INTERIM: &[u8] = b"HTTP/1.1 500 Internal Server Error\r\ncontent-length: 0\r\n\r\n";
50
+ const INTERIM_HEAD: &[u8] = b"HTTP/1.1 500 Internal Server Error\r\n\r\n";
51
+
52
+ /// The request a connection serves, counted in ServerInner::in_flight until its answer is out.
53
+ pub(super) struct Current {
54
+ pub(super) responder: Arc<Responder>,
55
+ pub(super) head_only: bool,
56
+ /// The connection ends after this answer: asked by the request, or as a streamed head said.
57
+ pub(super) close: bool,
58
+ /// Counts it in flight until it drops: with its answer, or its connection.
59
+ pub(super) _in_flight: InFlight,
60
+ /// How the app's writes go out, once its streamed head did.
61
+ pub(super) stream: Option<Streamed>,
62
+ /// Its arrival, admitted as it is read: where its total and its time in Ruby start. Its
63
+ /// completion line follows its answer's head, as hyper's does.
64
+ pub(super) logged: Option<Box<Logged>>,
65
+ }
66
+
67
+ #[derive(Clone, Copy)]
68
+ pub(super) enum Streamed {
69
+ Chunked,
70
+ /// The app's length, this many bytes still to go.
71
+ Length(usize),
72
+ }
73
+
74
+ /// What a write of a streamed body did.
75
+ #[derive(Debug, PartialEq, Eq)]
76
+ pub enum Wrote {
77
+ /// On its way: what the socket did not take goes out at the next write's look or the job's.
78
+ Done,
79
+ /// The socket still holds the last write: wait for it to take more, say so
80
+ /// (InlineIo::waited), then write this again; the descriptor stays open until then.
81
+ Wait(RawFd),
82
+ /// Nothing reaches the client any more: it left, the stream was cut, or the answer is whole.
83
+ Gone,
84
+ /// No stream of this request is going out: never started, or ended.
85
+ Closed,
86
+ }
87
+
88
+ impl InlineIo {
89
+ /// The answer is whole: unless the connection ends, its next head is due HEADER_READ_TIMEOUT
90
+ /// after the answer went out (now, or once the job's flush sends the rest). Until then the
91
+ /// deadline is the answer's, which bounds it only on a worker that leaves (expire).
92
+ fn answered(&self, state: &IoState) {
93
+ if !(state.closing && state.out.sent()) {
94
+ self.deadline
95
+ .due_in(&self.origin.server, HEADER_READ_TIMEOUT);
96
+ }
97
+ }
98
+
99
+ /// Whether the connection ends after `current`'s answer (the app's own Connection aside):
100
+ /// asked by the request, the drain, or a quarantine. A retiring worker's goes on, with hyper.
101
+ fn closes(&self, current: &Current) -> bool {
102
+ current.close || self.origin.server.is_draining() || self.slot.life.quarantined()
103
+ }
104
+
105
+ /// The socket took the last of the stream's length: the connection's end is decided now, as
106
+ /// hyper decides once that is flushed.
107
+ fn length_out(&self, state: &mut IoState) {
108
+ if let Some(current) = state.current.as_mut() {
109
+ current.close = self.closes(current);
110
+ }
111
+ }
112
+
113
+ /// With request_timeout set, the request served times out until its head goes out.
114
+ pub(super) fn times_out(&self, state: &IoState) -> bool {
115
+ self.origin.server.config.request_timeout_ms > 0
116
+ && state
117
+ .current
118
+ .as_ref()
119
+ .is_some_and(|current| !current.responder.claimed())
120
+ }
121
+
122
+ /// `logged`'s completion line: `total` after its arrival, with the breakdown for the app's
123
+ /// own answer (`apps`), which has no queue wait (none inline) and no GC figures (a
124
+ /// worker_loop worker's requests interleave); Kino's 504 and 500 carry none.
125
+ #[inline]
126
+ fn log(&self, logged: Option<Box<Logged>>, total: Option<Duration>, status: u16, apps: bool) {
127
+ let (Some(logged), Some(log)) = (logged, &self.origin.server.access_log) else {
128
+ return;
129
+ };
130
+ let total = total.unwrap_or_else(|| logged.elapsed());
131
+ let timing = apps.then_some(Timing {
132
+ wait: Duration::ZERO,
133
+ ruby: total,
134
+ gc: None,
135
+ });
136
+ logged.completion(log, status, total, timing);
137
+ }
138
+
139
+ /// The app's answer to an inline request, written now; what the socket does not take waits
140
+ /// for the job's next look. False when something answered the request first.
141
+ pub fn respond(
142
+ &self,
143
+ responder: &Responder,
144
+ status: StatusCode,
145
+ headers: &HeaderMap,
146
+ body: &[u8],
147
+ ) -> bool {
148
+ let mut state = self.state.lock();
149
+ if !state.serves(responder) || !responder.claim_inline() {
150
+ return false;
151
+ }
152
+ let Some(mut current) = state.current.take() else {
153
+ return false;
154
+ };
155
+ // Its total ends where its answer begins, before the write, as a stream's and hyper's do.
156
+ let total = current.logged.as_ref().map(|logged| logged.elapsed());
157
+ state.answered_on(&self.slot);
158
+ let begun = self.begin(
159
+ &mut state,
160
+ &current,
161
+ status,
162
+ headers,
163
+ Body::Length(body.len()),
164
+ );
165
+ if let Some((head, framing, close)) = begun {
166
+ let body = if framing == Body::Length(0) {
167
+ &[][..]
168
+ } else {
169
+ body
170
+ };
171
+ if state.write(&[&head, body]).is_err() || close {
172
+ state.closing = true;
173
+ }
174
+ self.answered(&state);
175
+ }
176
+ drop(state);
177
+ self.log(current.logged.take(), total, status.as_u16(), true);
178
+ true
179
+ }
180
+
181
+ /// The head of the app's streamed answer, written now; its body follows through write_chunk
182
+ /// to finish_stream, chunked unless the app gave its length. Where the head ends the answer,
183
+ /// body writes go nowhere; where a body could have followed, the first write counts as its
184
+ /// client gone, as hyper's dropped body counts. False when something answered the request
185
+ /// first.
186
+ pub fn start_stream(
187
+ &self,
188
+ responder: &Responder,
189
+ status: StatusCode,
190
+ headers: &HeaderMap,
191
+ ) -> bool {
192
+ let mut state = self.state.lock();
193
+ if !state.serves(responder) || !responder.claim_inline() {
194
+ // A request starts its stream once: one going out is its own.
195
+ if state.streamed(responder).is_none() {
196
+ responder.answered_first();
197
+ }
198
+ return false;
199
+ }
200
+ let Some(mut current) = state.current.take() else {
201
+ return false;
202
+ };
203
+ self.log(current.logged.take(), None, status.as_u16(), true);
204
+ let Some((head, framing, close)) =
205
+ self.begin(&mut state, &current, status, headers, Body::Chunked)
206
+ else {
207
+ if crate::framing::bodiless(status.as_u16(), current.head_only) {
208
+ responder.answered_first();
209
+ } else {
210
+ state.dropped = address(responder);
211
+ }
212
+ state.answered_on(&self.slot);
213
+ return true;
214
+ };
215
+ if state.write(&[&head]).is_err() {
216
+ state.current = Some(current);
217
+ state.cut(responder, Cut::ClientGone);
218
+ return true;
219
+ }
220
+ // As hyper drops the body of a response that may carry none.
221
+ if crate::framing::bodiless(status.as_u16(), current.head_only) {
222
+ responder.answered_first();
223
+ state.closing |= close;
224
+ state.answered_on(&self.slot);
225
+ self.answered(&state);
226
+ return true;
227
+ }
228
+ current.close = close;
229
+ current.stream = Some(match framing {
230
+ Body::Chunked => Streamed::Chunked,
231
+ Body::Length(length) => Streamed::Length(length),
232
+ });
233
+ responder.set_streaming(true);
234
+ state.current = Some(current);
235
+ true
236
+ }
237
+
238
+ /// One chunk, framed as its head said, once the last one's rest is out: as much as the socket
239
+ /// takes now, the rest kept for the next look. While the socket still holds some: Wait, the
240
+ /// chunk not taken, so the buffer holds at most one chunk's rest; the descriptor stays open
241
+ /// until the caller's wait is back (InlineIo::waited). A write error cuts the stream
242
+ /// (Cut::ClientGone): Gone, and the connection ends after the request. Bytes past the app's
243
+ /// length never go out, as hyper's encoder takes no more: the answer is whole, and a later
244
+ /// write is Gone, counted as its client gone.
245
+ pub fn write_chunk(&self, responder: &Responder, data: &[u8]) -> Wrote {
246
+ let mut state = self.state.lock();
247
+ let Some(&mut framing) = state.streamed(responder) else {
248
+ return if responder.stream_cut() || state.dropped == address(responder) {
249
+ Wrote::Gone
250
+ } else {
251
+ Wrote::Closed
252
+ };
253
+ };
254
+ let length_unsent = matches!(framing, Streamed::Length(0)) && !state.out.sent();
255
+ match state.flush() {
256
+ Ok(true) => {}
257
+ Ok(false) => {
258
+ let Some(fd) = state.stream.as_ref().map(AsRawFd::as_raw_fd) else {
259
+ return Wrote::Gone;
260
+ };
261
+ state.lent += 1;
262
+ return Wrote::Wait(fd);
263
+ }
264
+ Err(_) => return state.cut(responder, Cut::ClientGone),
265
+ }
266
+ // The last of its length went out just now.
267
+ if length_unsent {
268
+ self.length_out(&mut state);
269
+ }
270
+ // An empty chunk would end the body.
271
+ if data.is_empty() {
272
+ return Wrote::Done;
273
+ }
274
+ let written = match framing {
275
+ Streamed::Chunked => {
276
+ let mut size = [0; 18];
277
+ let size = chunk_size(data.len(), &mut size);
278
+ state.write(&[size, data, b"\r\n"])
279
+ }
280
+ Streamed::Length(left) => {
281
+ let fits = &data[..data.len().min(left)];
282
+ if let Some(Streamed::Length(left)) = state.streamed(responder) {
283
+ *left -= fits.len();
284
+ }
285
+ let written = if fits.is_empty() {
286
+ Ok(())
287
+ } else {
288
+ state.write(&[fits])
289
+ };
290
+ if !fits.is_empty() && fits.len() == left && state.out.sent() {
291
+ self.length_out(&mut state);
292
+ }
293
+ if data.len() > left {
294
+ if let Some(current) = state.current.take() {
295
+ self.ended(&mut state, &current, true);
296
+ }
297
+ state.dropped = address(responder);
298
+ if fits.is_empty() {
299
+ return Wrote::Gone;
300
+ }
301
+ }
302
+ written
303
+ }
304
+ };
305
+ match written {
306
+ Ok(()) => Wrote::Done,
307
+ Err(_) => state.cut(responder, Cut::ClientGone),
308
+ }
309
+ }
310
+
311
+ /// The end of the app's stream: a chunked body's last chunk, after the rest; a body shorter
312
+ /// than its length ends the connection where it stops, as hyper does. Nothing once ended or
313
+ /// cut.
314
+ pub fn finish_stream(&self, responder: &Responder) {
315
+ let mut state = self.state.lock();
316
+ let Some(&mut framing) = state.streamed(responder) else {
317
+ return;
318
+ };
319
+ let Some(current) = state.current.take() else {
320
+ return;
321
+ };
322
+ let whole = match framing {
323
+ Streamed::Chunked => state.write(&[b"0\r\n\r\n"]).is_ok(),
324
+ Streamed::Length(left) => left == 0,
325
+ };
326
+ self.ended(&mut state, &current, whole);
327
+ }
328
+
329
+ /// `current`'s stream ended, `whole` or not: its rest goes out at the job's next look, then
330
+ /// the connection ends unless the answer is whole and may keep it. One still owed when the
331
+ /// drain began ends too, as hyper's does once the drain turns keep-alive off mid-answer.
332
+ fn ended(&self, state: &mut IoState, current: &Current, whole: bool) {
333
+ state.answered_on(&self.slot);
334
+ current.responder.set_streaming(false);
335
+ let closes = match current.stream {
336
+ Some(Streamed::Length(_)) => current.close,
337
+ _ => self.closes(current),
338
+ };
339
+ if !whole || closes {
340
+ state.closing = true;
341
+ }
342
+ self.answered(state);
343
+ }
344
+
345
+ /// Responder::give_up from any thread: the 500 if nothing answered yet, written now or never
346
+ /// (the socket is shut down rather than wait), or a stream going out cut short.
347
+ pub(crate) fn give_up(&self, responder: &Responder, why: Cut) {
348
+ let mut state = self.state.lock();
349
+ let ours = state.serves(responder);
350
+ let claimed = responder.claim_inline();
351
+ if !ours {
352
+ return;
353
+ }
354
+ if !claimed {
355
+ // A stream going out is cut, never ending as if whole; one whose length is all
356
+ // written is whole already. What the socket takes of the rest goes now, as a job that
357
+ // unwound looks no more, and it ends as finish_stream ends it; a worker given up on
358
+ // shuts it down after (InlineIo::abandon).
359
+ responder.cut_stream(why);
360
+ match state.current.take() {
361
+ Some(mut current) if matches!(current.stream, Some(Streamed::Length(0))) => {
362
+ let unsent = !state.out.sent();
363
+ if state.flush().unwrap_or(false) && unsent {
364
+ current.close = self.closes(&current);
365
+ }
366
+ self.ended(&mut state, &current, true);
367
+ }
368
+ _ => state.shutdown(),
369
+ }
370
+ return;
371
+ }
372
+ self.answer_and_end(&mut state, &GIVE_UP);
373
+ }
374
+
375
+ /// Past request_timeout with no answer begun: the 504 instead, counted with hyper's, written
376
+ /// now or never, and the connection ends after it. The app runs on, as through hyper; its
377
+ /// answer goes nowhere, and a stream it starts ends at its first write.
378
+ pub(super) fn time_out(&self, state: &mut IoState) {
379
+ let claimed = |current: &Current| current.responder.claim_inline();
380
+ if state.current.as_ref().is_some_and(claimed) {
381
+ let timeouts = &self.origin.server.counters.timeouts;
382
+ timeouts.fetch_add(1, Ordering::Relaxed);
383
+ self.answer_and_end(state, &TIMED_OUT);
384
+ }
385
+ }
386
+
387
+ /// Kino's own answer in place of the app's to the request served, just claimed: written
388
+ /// whole if the socket takes it now, else cut where it stopped, or not at all behind an
389
+ /// earlier answer still buffered (shut down rather than wait: no job may be left to write the
390
+ /// rest). The connection ends either way, and the request's line is logged with no breakdown.
391
+ fn answer_and_end(&self, state: &mut IoState, canned: &Canned) {
392
+ let Some(current) = state.current.take() else {
393
+ return;
394
+ };
395
+ let body = if current.head_only { &[] } else { canned.body };
396
+ let written = state.out.sent()
397
+ && state.write(&[canned.head, body]).is_ok()
398
+ && matches!(state.flush(), Ok(true));
399
+ match &state.stream {
400
+ Some(stream) if written => {
401
+ let _ = stream.socket.shutdown(Shutdown::Write);
402
+ }
403
+ _ => state.shutdown(),
404
+ }
405
+ state.closing = true;
406
+ self.log(current.logged, None, canned.status, false);
407
+ }
408
+
409
+ /// How `current`'s answer begins: its head, its body's framing and whether the connection
410
+ /// ends after. None where it ends at once, as hyper ends it: after a 1xx other than 101, or
411
+ /// for a head hyper refuses.
412
+ fn begin(
413
+ &self,
414
+ state: &mut IoState,
415
+ current: &Current,
416
+ status: StatusCode,
417
+ headers: &HeaderMap,
418
+ body: Body,
419
+ ) -> Option<(Vec<u8>, Body, bool)> {
420
+ // As hyper: no 1xx but a 101 is final, and a 101 ends the connection, as no switch is
421
+ // served here.
422
+ let switching = status == StatusCode::SWITCHING_PROTOCOLS;
423
+ if status.is_informational() && !switching {
424
+ let answer = if current.head_only {
425
+ INTERIM_HEAD
426
+ } else {
427
+ INTERIM
428
+ };
429
+ let _ = state.write(&[answer]);
430
+ state.closing = true;
431
+ return None;
432
+ }
433
+ let closes = self.closes(current);
434
+ let begun = encode::head(status, headers, body, current.head_only, closes);
435
+ let Some((head, framing, close)) = begun else {
436
+ state.closing = true;
437
+ return None;
438
+ };
439
+ Some((head, framing, close || switching))
440
+ }
441
+ }
442
+
443
+ impl IoState {
444
+ fn serves(&self, responder: &Responder) -> bool {
445
+ self.current
446
+ .as_ref()
447
+ .is_some_and(|current| std::ptr::eq(Arc::as_ptr(&current.responder), responder))
448
+ }
449
+
450
+ fn streamed(&mut self, responder: &Responder) -> Option<&mut Streamed> {
451
+ self.current
452
+ .as_mut()
453
+ .filter(|current| std::ptr::eq(Arc::as_ptr(&current.responder), responder))
454
+ .and_then(|current| current.stream.as_mut())
455
+ }
456
+
457
+ /// The stream served is cut short (`why`, unless it was already): nothing more goes out, and
458
+ /// the connection ends at its job's next look.
459
+ fn cut(&mut self, responder: &Responder, why: Cut) -> Wrote {
460
+ responder.cut_stream(why);
461
+ self.current = None;
462
+ self.out.clear();
463
+ self.closing = true;
464
+ Wrote::Gone
465
+ }
466
+
467
+ /// The answer is whole: until its job looks again, the connection counts as answered on
468
+ /// `slot`.
469
+ fn answered_on(&mut self, slot: &WorkerSlot) {
470
+ if !self.answered {
471
+ self.answered = true;
472
+ slot.inline.answered.fetch_add(1, Ordering::Relaxed);
473
+ }
474
+ }
475
+ }
476
+
477
+ /// `responder`'s address, which tells its request apart.
478
+ fn address(responder: &Responder) -> usize {
479
+ std::ptr::from_ref(responder) as usize
480
+ }
481
+
482
+ /// A chunk's size line, as hyper writes it, in `out`.
483
+ fn chunk_size(len: usize, out: &mut [u8; 18]) -> &[u8] {
484
+ let mut line = std::io::Cursor::new(&mut out[..]);
485
+ let _ = write!(line, "{len:X}\r\n");
486
+ let end = line.position() as usize;
487
+ &out[..end]
488
+ }