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,436 @@
1
+ //! Serving an accepted connection: the TLS handshake, the client's first bytes in time
2
+ //! (intake.rs), then hyper until it ends or drains; or inline on a worker (inline/), with hyper
3
+ //! again for what it hands back.
4
+
5
+ use std::sync::Arc;
6
+ use std::time::Duration;
7
+
8
+ use bytes::Bytes;
9
+ use hyper::service::service_fn;
10
+ use hyper_util::rt::{TokioExecutor, TokioIo, TokioTimer};
11
+ use hyper_util::server::conn::auto;
12
+ use tokio::io::{AsyncRead, AsyncWrite};
13
+ use tokio::net::TcpStream;
14
+ use tokio_util::either::Either;
15
+
16
+ use super::accept::{Accepted, Conn, ConnIo, Held, Peer, StdConn};
17
+ use super::handle::handle_request;
18
+ use super::intake::{
19
+ drain_signal, first_bytes, preface, took_first, Began, Intake, H2_PING_EVERY, H2_PING_TIMEOUT,
20
+ HEADER_READ_TIMEOUT,
21
+ };
22
+ use crate::registry::ServerInner;
23
+
24
+ /// TLS is boxed: an upgrade boxes the stream, and a plain one must not carry a TLS session's room.
25
+ type Io = Either<Box<tokio_rustls::server::TlsStream<TcpStream>>, Conn>;
26
+
27
+ pub(crate) async fn serve_conn(
28
+ accepted: Accepted,
29
+ acceptor: Option<tokio_rustls::TlsAcceptor>,
30
+ server: Arc<ServerInner>,
31
+ ) {
32
+ serve_conn_by(Intake::KINO, accepted, acceptor, server).await;
33
+ }
34
+
35
+ async fn serve_conn_by(
36
+ intake: Intake,
37
+ Accepted { conn, peer, held }: Accepted,
38
+ acceptor: Option<tokio_rustls::TlsAcceptor>,
39
+ server: Arc<ServerInner>,
40
+ ) {
41
+ let (stream, acceptor) = match (conn, acceptor) {
42
+ (Either::Left(stream), Some(acceptor)) => (stream, acceptor),
43
+ // A plain one is served inline from here on, unless no worker has room for it.
44
+ // TLS over a unix socket is refused at bind time.
45
+ (conn, _) => {
46
+ if let Err((conn, held)) = crate::inline::place(&server, conn, held, peer) {
47
+ let accepted = Accepted { conn, peer, held };
48
+ serve(accepted, None, intake.first_byte, server).await;
49
+ }
50
+ return;
51
+ }
52
+ };
53
+ // Failed or stalled handshakes (scans, plain HTTP, slowloris) just drop the connection.
54
+ // The timeout bounds a stall, and the drain closes one whose handshake has not begun.
55
+ let accepted = tokio::time::Instant::now();
56
+ let began = first_bytes(Either::Left(&stream), &[], intake.handshake, &server);
57
+ if began.await.is_none() {
58
+ return;
59
+ }
60
+ let handshake = tokio::time::timeout_at(accepted + intake.handshake, acceptor.accept(stream));
61
+ let Ok(Ok(mut tls)) = handshake.await else {
62
+ return;
63
+ };
64
+ let due = accepted + intake.first_byte;
65
+ let Some((protocol, prefix)) = tls_protocol(&mut tls, due, &server).await else {
66
+ return;
67
+ };
68
+ let io = ConnIo::<Io>::new(Either::Left(Box::new(tls)), held, prefix);
69
+ serve_connection(io, protocol, server, peer).await;
70
+ }
71
+
72
+ /// The protocol a TLS client chose by ALPN, with no sniff, and what was read of its start to
73
+ /// tell, for hyper to read again. Only HTTP/1 has a header timer, so h2's preface, or what tells
74
+ /// the two apart without ALPN, is read here; None, and it closes, when that is not in by `due`.
75
+ async fn tls_protocol(
76
+ tls: &mut tokio_rustls::server::TlsStream<TcpStream>,
77
+ due: tokio::time::Instant,
78
+ server: &ServerInner,
79
+ ) -> Option<(Protocol, Bytes)> {
80
+ let left = || due.saturating_duration_since(tokio::time::Instant::now());
81
+ let (tcp, session) = tls.get_ref();
82
+ let alpn = session.alpn_protocol();
83
+ if !server.config.http2 || alpn == Some(b"http/1.1") {
84
+ // What came with the handshake's last flight is read already. Else the peek sees
85
+ // records, whose first byte (a content type, 0x14 to 0x18) never starts the h2 preface.
86
+ if session.wants_read() {
87
+ first_bytes(Either::Left(tcp), &[], left(), server).await?;
88
+ }
89
+ return Some((Protocol::Http1, Bytes::new()));
90
+ }
91
+ let alpn_h2 = alpn == Some(b"h2");
92
+ let (began, prefix) = preface(tls, left(), server).await?;
93
+ let protocol = match alpn_h2 || began == Began::H2 {
94
+ true => Protocol::Http2,
95
+ false => Protocol::Http1,
96
+ };
97
+ Some((protocol, prefix))
98
+ }
99
+
100
+ /// A plain connection served by hyper once its client began, by `within` (first_bytes): fresh, or
101
+ /// handed back with `read` read, which hyper reads first.
102
+ async fn serve(
103
+ accepted: Accepted,
104
+ read: Option<Bytes>,
105
+ within: Duration,
106
+ server: Arc<ServerInner>,
107
+ ) {
108
+ let plain = match &accepted.conn {
109
+ Either::Left(stream) => Either::Left(stream),
110
+ Either::Right(stream) => Either::Right(stream),
111
+ };
112
+ let first = read.as_deref().unwrap_or_default();
113
+ if first_bytes(plain.clone(), first, within, &server)
114
+ .await
115
+ .is_none()
116
+ {
117
+ return;
118
+ }
119
+ let prefix = read.map_or_else(Bytes::new, |read| took_first(plain, read));
120
+ let Accepted { conn, peer, held } = accepted;
121
+ let io = ConnIo::<Io>::new(Either::Right(conn), held, prefix);
122
+ serve_connection(io, Protocol::of(&server), server, peer).await;
123
+ }
124
+
125
+ /// An inline connection handed back: hyper serves it on its accepting runtime (`origin`), reading
126
+ /// `read` first; its client has `within`, what is left of its header time.
127
+ /// A thread-safe free may run it (an idle connection's handle), so it keeps typed_data.rs's
128
+ /// promise: it only spawns, and the task registers the stream.
129
+ pub(crate) fn serve_handed_back(
130
+ origin: &crate::inline::Origin,
131
+ stream: StdConn,
132
+ read: Bytes,
133
+ held: Held,
134
+ within: Duration,
135
+ ) {
136
+ let (server, peer) = (origin.server.clone(), origin.peer);
137
+ origin.home.spawn(async move {
138
+ // Registered with the runtime that polls it.
139
+ let Ok(conn) = stream.into_tokio() else {
140
+ return;
141
+ };
142
+ let accepted = Accepted { conn, peer, held };
143
+ serve(accepted, Some(read), within, server).await;
144
+ });
145
+ }
146
+
147
+ /// SETTINGS_MAX_CONCURRENT_STREAMS from slot capacity, not hyper's flat 200 (zeros keep that).
148
+ fn advertised_streams(workers: usize, threads: usize) -> u32 {
149
+ let slots = workers.saturating_mul(threads);
150
+ if slots == 0 {
151
+ return 200;
152
+ }
153
+ slots.clamp(8, 1024) as u32
154
+ }
155
+
156
+ fn stream_capacity(topology: &super::Topology) -> u32 {
157
+ advertised_streams(topology.max_workers, topology.threads)
158
+ }
159
+
160
+ /// Kino's HTTP/2 settings, on either of hyper's h2 builders (they share no trait). No Date header:
161
+ /// a clock read per response for a SHOULD. A client silent for H2_PING_EVERY gets a PING, and one
162
+ /// that leaves it unanswered for H2_PING_TIMEOUT is closed, so a peer that is gone holds no
163
+ /// connection; one that answers keeps its connection, idle or not.
164
+ macro_rules! h2_settings {
165
+ ($builder:expr, $max_streams:expr) => {
166
+ $builder
167
+ .timer(TokioTimer::new())
168
+ .auto_date_header(false)
169
+ .max_concurrent_streams($max_streams)
170
+ .keep_alive_interval(H2_PING_EVERY)
171
+ .keep_alive_timeout(H2_PING_TIMEOUT)
172
+ };
173
+ }
174
+
175
+ /// Kino's HTTP/1 settings, on either of hyper's h1 builders: the timer is what makes
176
+ /// header_read_timeout fire, and no Date header, as on h2.
177
+ macro_rules! h1_settings {
178
+ ($builder:expr) => {
179
+ $builder
180
+ .timer(TokioTimer::new())
181
+ .header_read_timeout(HEADER_READ_TIMEOUT)
182
+ .auto_date_header(false)
183
+ };
184
+ }
185
+
186
+ /// Plaintext: h2 by preface sniff, else HTTP/1 (TLS: by ALPN, http1_builder and http2_builder).
187
+ fn conn_builder(max_streams: u32) -> auto::Builder<TokioExecutor> {
188
+ let mut builder = auto::Builder::new(TokioExecutor::new());
189
+ h1_settings!(builder.http1());
190
+ h2_settings!(builder.http2(), max_streams);
191
+ builder
192
+ }
193
+
194
+ /// How hyper serves a connection: HTTP/1, h2 (as a TLS client chose), or either by sniff.
195
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
196
+ enum Protocol {
197
+ Http1,
198
+ Http2,
199
+ Auto,
200
+ }
201
+
202
+ impl Protocol {
203
+ /// The sniff, or HTTP/1 alone where `server` has HTTP/2 off.
204
+ fn of(server: &ServerInner) -> Protocol {
205
+ if server.config.http2 {
206
+ Protocol::Auto
207
+ } else {
208
+ Protocol::Http1
209
+ }
210
+ }
211
+ }
212
+
213
+ /// Serves `io` by `protocol` until it ends or drains.
214
+ async fn serve_connection<I>(
215
+ io: ConnIo<I>,
216
+ protocol: Protocol,
217
+ server: Arc<ServerInner>,
218
+ peer: Peer,
219
+ ) where
220
+ I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
221
+ {
222
+ let max_streams = stream_capacity(&server.config.topology);
223
+ let mut drain = server.shutdown_tx.subscribe();
224
+ // A count of the connection's own, moved per request: the server's, which every worker take
225
+ // moves, stays off the I/O threads.
226
+ let server = Arc::new(server);
227
+ let service = service_fn(move |req| {
228
+ let server = server.clone();
229
+ async move { handle_request(&server, peer, req).await }
230
+ });
231
+ // On the drain signal: finish the request, then close (GOAWAY on h2).
232
+ // A macro: hyper's connection types share no trait.
233
+ macro_rules! serve_until_drained {
234
+ ($conn:expr) => {{
235
+ let mut conn = std::pin::pin!($conn);
236
+ // The connection first: a handed-back one with a request read is served before a
237
+ // drain that began meanwhile closes it, as inline it would have been.
238
+ tokio::select! {
239
+ biased;
240
+ _ = conn.as_mut() => {}
241
+ _ = drain_signal(&mut drain) => {
242
+ conn.as_mut().graceful_shutdown();
243
+ let _ = conn.as_mut().await;
244
+ }
245
+ }
246
+ }};
247
+ }
248
+ let io = TokioIo::new(io);
249
+ // Upgrades need upgradeable connections, where http1_only is ignored: pin HTTP/1 directly.
250
+ match protocol {
251
+ Protocol::Http1 => {
252
+ let conn = http1_builder().serve_connection(io, service);
253
+ serve_until_drained!(conn.with_upgrades())
254
+ }
255
+ Protocol::Http2 => {
256
+ serve_until_drained!(http2_builder(max_streams).serve_connection(io, service))
257
+ }
258
+ Protocol::Auto => {
259
+ let builder = conn_builder(max_streams);
260
+ serve_until_drained!(builder.serve_connection_with_upgrades(io, service))
261
+ }
262
+ }
263
+ }
264
+
265
+ /// For a TLS client that chose h2.
266
+ fn http2_builder(max_streams: u32) -> hyper::server::conn::http2::Builder<TokioExecutor> {
267
+ let mut builder = hyper::server::conn::http2::Builder::new(TokioExecutor::new());
268
+ h2_settings!(builder, max_streams);
269
+ builder
270
+ }
271
+
272
+ fn http1_builder() -> hyper::server::conn::http1::Builder {
273
+ let mut builder = hyper::server::conn::http1::Builder::new();
274
+ h1_settings!(builder);
275
+ builder
276
+ }
277
+
278
+ #[cfg(test)]
279
+ pub(crate) mod testing {
280
+ use std::sync::Arc;
281
+
282
+ use super::{serve_connection, ConnIo, Protocol};
283
+ use crate::registry::ServerInner;
284
+ use crate::server::accept::Held;
285
+
286
+ /// `io` as hyper serves a connection, holding a permit of its own.
287
+ pub(super) fn conn_io<I>(io: I) -> ConnIo<I> {
288
+ let held = Held::new(crate::testing::permit(), None);
289
+ ConnIo::new(io, held, bytes::Bytes::new())
290
+ }
291
+
292
+ /// Serve one connection the production way, over an in-memory pipe:
293
+ /// the client's end.
294
+ pub fn spawn_conn(server: Arc<ServerInner>) -> tokio::io::DuplexStream {
295
+ let protocol = Protocol::of(&server);
296
+ spawn_conn_by(server, protocol).0
297
+ }
298
+
299
+ /// The same by `protocol`, with the connection's task.
300
+ pub(super) fn spawn_conn_by(
301
+ server: Arc<ServerInner>,
302
+ protocol: Protocol,
303
+ ) -> (tokio::io::DuplexStream, tokio::task::JoinHandle<()>) {
304
+ let (client, conn) = tokio::io::duplex(64 * 1024);
305
+ let peer = crate::testing::PEER;
306
+ let served = serve_connection(conn_io(conn), protocol, server, peer);
307
+ (client, tokio::spawn(served))
308
+ }
309
+
310
+ /// The body of a request hyper read (its `body_rx`), as its forwarder
311
+ /// sends it: each chunk as it comes, up to the end.
312
+ pub async fn hyper_body_of(body_rx: Option<&flume::Receiver<bytes::Bytes>>) -> Vec<u8> {
313
+ let mut body = Vec::new();
314
+ if let Some(body_rx) = body_rx {
315
+ while let Ok(chunk) = body_rx.recv_async().await {
316
+ body.extend_from_slice(&chunk);
317
+ }
318
+ }
319
+ body
320
+ }
321
+ }
322
+
323
+ #[cfg(test)]
324
+ mod intake_tests;
325
+
326
+ #[cfg(test)]
327
+ mod tests {
328
+ use std::convert::Infallible;
329
+
330
+ use tokio::io::AsyncWriteExt;
331
+
332
+ use super::*;
333
+ use crate::response::{plain_response, HyperResponse};
334
+ use crate::testing::h2_get;
335
+
336
+ async fn ok(_: hyper::Request<hyper::body::Incoming>) -> Result<HyperResponse, Infallible> {
337
+ Ok(plain_response(200, "ok\n"))
338
+ }
339
+
340
+ #[test]
341
+ fn a_connection_advertises_the_slots_of_its_pools_ceiling_floored_and_capped() {
342
+ assert_eq!(advertised_streams(8, 3), 24);
343
+ assert_eq!(advertised_streams(2, 1), 8, "floor");
344
+ assert_eq!(advertised_streams(64, 32), 1024, "cap");
345
+ assert_eq!(
346
+ advertised_streams(8, 0),
347
+ 200,
348
+ "an embedder's unknown topology"
349
+ );
350
+ // An elastic pool grows to meet what an h2 balancer sends.
351
+ let elastic = super::super::Topology {
352
+ workers: 2,
353
+ max_workers: 8,
354
+ threads: 3,
355
+ ..Default::default()
356
+ };
357
+ assert_eq!(stream_capacity(&elastic), 24, "the ceiling, not the floor");
358
+ }
359
+
360
+ #[tokio::test]
361
+ async fn http2_off_refuses_the_preface_but_serves_h1() {
362
+ // What a server with http2 off serves every connection with.
363
+ let pinned = || {
364
+ let (client, conn) = tokio::io::duplex(64 * 1024);
365
+ let served = http1_builder().serve_connection(TokioIo::new(conn), service_fn(ok));
366
+ tokio::spawn(served.with_upgrades());
367
+ TokioIo::new(client)
368
+ };
369
+ // The pinned h1 codec reads the preface as a malformed request line.
370
+ let h2 = async {
371
+ let (mut sender, conn) =
372
+ hyper::client::conn::http2::handshake(TokioExecutor::new(), pinned()).await?;
373
+ tokio::spawn(conn);
374
+ sender.send_request(h2_get("/")).await
375
+ };
376
+ assert!(h2.await.is_err(), "h2 must not be served when pinned to h1");
377
+
378
+ let (mut sender, conn) = hyper::client::conn::http1::handshake(pinned())
379
+ .await
380
+ .expect("h1 handshake");
381
+ tokio::spawn(conn);
382
+ let response = sender.send_request(crate::testing::get("/")).await;
383
+ assert_eq!(response.expect("h1 response").status(), 200);
384
+ }
385
+
386
+ #[tokio::test]
387
+ async fn streams_beyond_the_advertised_cap_queue_instead_of_failing() {
388
+ // Six requests at a cap of 2: the h2 client holds the excess until
389
+ // the server frees a stream, and nothing is refused or reset.
390
+ let (client, conn) = tokio::io::duplex(64 * 1024);
391
+ tokio::spawn(async move {
392
+ let _ = conn_builder(2)
393
+ .serve_connection(TokioIo::new(conn), service_fn(ok))
394
+ .await;
395
+ });
396
+ let (mut sender, conn) =
397
+ hyper::client::conn::http2::handshake(TokioExecutor::new(), TokioIo::new(client))
398
+ .await
399
+ .expect("h2 handshake");
400
+ tokio::spawn(conn);
401
+ let requests: Vec<_> = (0..6)
402
+ .map(|i| sender.send_request(h2_get(&format!("/{i}"))))
403
+ .collect();
404
+ for request in requests {
405
+ assert_eq!(request.await.expect("queued stream served").status(), 200);
406
+ }
407
+ }
408
+
409
+ #[tokio::test(start_paused = true)]
410
+ async fn header_read_timeout_still_fires_through_the_auto_builder() {
411
+ let (mut client, conn) = tokio::io::duplex(64 * 1024);
412
+ let served = tokio::spawn(async move {
413
+ let _ = conn_builder(200)
414
+ .serve_connection(TokioIo::new(conn), service_fn(ok))
415
+ .await;
416
+ });
417
+ // A partial request line, then silence: the slow-header guard reaps
418
+ // the connection (the paused clock auto-advances past 15 s).
419
+ client.write_all(b"GET / HT").await.expect("partial write");
420
+ tokio::time::timeout(Duration::from_secs(60), served)
421
+ .await
422
+ .expect("connection reaped by header_read_timeout")
423
+ .expect("serve task join");
424
+ }
425
+
426
+ #[test]
427
+ fn an_upgraded_plain_connection_carries_no_room_for_a_tls_session() {
428
+ // hyper boxes the stream it hands over on an upgrade.
429
+ let served = size_of::<ConnIo<Io>>();
430
+ let plain = size_of::<ConnIo<tokio::net::TcpStream>>();
431
+ assert!(
432
+ served <= plain + 16,
433
+ "{served} bytes, {plain} for plain TCP"
434
+ );
435
+ }
436
+ }