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
|
@@ -0,0 +1,766 @@
|
|
|
1
|
+
//! Inline connections: on a worker_loop server, a plain HTTP/1.1 connection is served on its
|
|
2
|
+
//! worker's thread, Rust reading a strict subset of HTTP/1.1 (parse.rs) and writing the answers;
|
|
3
|
+
//! any other request goes back to hyper, its bytes replayed, before the app sees it. Only the
|
|
4
|
+
//! connection's job closes its socket or hands it back (or the last write still waiting as the
|
|
5
|
+
//! job lets go); other threads write under its lock or shut it down. doc/architecture.md has the
|
|
6
|
+
//! mechanism.
|
|
7
|
+
|
|
8
|
+
use std::cell::RefCell;
|
|
9
|
+
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
|
10
|
+
use std::sync::Arc;
|
|
11
|
+
|
|
12
|
+
use magnus::prelude::*;
|
|
13
|
+
use magnus::{Error, Ruby, Value};
|
|
14
|
+
use parking_lot::Mutex;
|
|
15
|
+
|
|
16
|
+
use crate::registry::ServerInner;
|
|
17
|
+
use crate::response::Cut;
|
|
18
|
+
use crate::server::{Conn, Held, Peer, StdConn};
|
|
19
|
+
use crate::slot::WorkerSlot;
|
|
20
|
+
|
|
21
|
+
mod answer;
|
|
22
|
+
mod conn;
|
|
23
|
+
mod deadline;
|
|
24
|
+
mod encode;
|
|
25
|
+
mod io;
|
|
26
|
+
#[cfg(test)]
|
|
27
|
+
mod parity;
|
|
28
|
+
mod parse;
|
|
29
|
+
mod spin;
|
|
30
|
+
#[cfg(test)]
|
|
31
|
+
pub mod testing;
|
|
32
|
+
|
|
33
|
+
pub use answer::Wrote;
|
|
34
|
+
pub use conn::{InlineConn, Next};
|
|
35
|
+
pub use deadline::tick;
|
|
36
|
+
pub(crate) use io::Origin;
|
|
37
|
+
pub use io::{await_writable, InlineIo, Listed};
|
|
38
|
+
use io::{forget, take_seat, Shut};
|
|
39
|
+
|
|
40
|
+
const INLINE_BODY_MAX: u64 = 64 * 1024;
|
|
41
|
+
|
|
42
|
+
pub struct InlineServer {
|
|
43
|
+
/// Held while placing, so two I/O threads never pick the same least-loaded worker.
|
|
44
|
+
placing: Mutex<()>,
|
|
45
|
+
declined: AtomicU64,
|
|
46
|
+
/// Connections that went back to hyper once a worker had them, whatever for.
|
|
47
|
+
handbacks: AtomicU64,
|
|
48
|
+
/// mono_us as of the ticker's last look, up to a TICK behind: a deadline costs a load, not a
|
|
49
|
+
/// clock read.
|
|
50
|
+
clock: Clock,
|
|
51
|
+
pub(crate) spinners: spin::Spinners,
|
|
52
|
+
/// The inline spin's switch (ServerConfig::inline_spin), off from the drain on; and the most
|
|
53
|
+
/// requests in flight server-wide it runs under (spin::in_flight_limit).
|
|
54
|
+
pub(crate) spin_on: AtomicBool,
|
|
55
|
+
pub(crate) spin_in_flight: usize,
|
|
56
|
+
/// A spin's budget and a run of spins' bound (spin::BUDGET, spin::RUN), which tests lengthen.
|
|
57
|
+
pub(crate) spin_budget: std::time::Duration,
|
|
58
|
+
pub(crate) spin_run: std::time::Duration,
|
|
59
|
+
/// How far tests moved the clock on past mono_us (testing::advance),
|
|
60
|
+
/// which the ticker's looks keep.
|
|
61
|
+
#[cfg(test)]
|
|
62
|
+
ahead: AtomicU64,
|
|
63
|
+
/// What happens to the worker placement picks before the connection
|
|
64
|
+
/// is sent to it (tests).
|
|
65
|
+
#[cfg(test)]
|
|
66
|
+
pub(crate) on_pick: Mutex<Option<OnPick>>,
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#[cfg(test)]
|
|
70
|
+
pub(crate) type OnPick = Box<dyn FnOnce(&WorkerSlot) + Send>;
|
|
71
|
+
|
|
72
|
+
/// On its own cache line: every answer reads it, and the counters beside it churn.
|
|
73
|
+
#[repr(align(128))]
|
|
74
|
+
struct Clock(AtomicU64);
|
|
75
|
+
|
|
76
|
+
impl Default for InlineServer {
|
|
77
|
+
fn default() -> Self {
|
|
78
|
+
InlineServer {
|
|
79
|
+
placing: Mutex::default(),
|
|
80
|
+
declined: AtomicU64::default(),
|
|
81
|
+
handbacks: AtomicU64::default(),
|
|
82
|
+
clock: Clock(AtomicU64::new(crate::mono::mono_us())),
|
|
83
|
+
spinners: spin::Spinners::default(),
|
|
84
|
+
spin_on: AtomicBool::new(false),
|
|
85
|
+
spin_in_flight: spin::in_flight_limit(crate::cpus::count()),
|
|
86
|
+
spin_budget: spin::BUDGET,
|
|
87
|
+
spin_run: spin::RUN,
|
|
88
|
+
#[cfg(test)]
|
|
89
|
+
ahead: AtomicU64::new(0),
|
|
90
|
+
#[cfg(test)]
|
|
91
|
+
on_pick: Mutex::default(),
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
impl InlineServer {
|
|
97
|
+
pub fn declined(&self) -> u64 {
|
|
98
|
+
self.declined.load(Ordering::Relaxed)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
pub fn handbacks(&self) -> u64 {
|
|
102
|
+
self.handbacks.load(Ordering::Relaxed)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
fn now(server: &ServerInner) -> u64 {
|
|
107
|
+
server
|
|
108
|
+
.inline
|
|
109
|
+
.as_ref()
|
|
110
|
+
.map_or_else(crate::mono::mono_us, |inline| {
|
|
111
|
+
inline.clock.0.load(Ordering::Relaxed)
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
fn real_now(server: &ServerInner) -> u64 {
|
|
116
|
+
let now = crate::mono::mono_us();
|
|
117
|
+
#[cfg(test)]
|
|
118
|
+
let now = now
|
|
119
|
+
+ server
|
|
120
|
+
.inline
|
|
121
|
+
.as_ref()
|
|
122
|
+
.map_or(0, |inline| inline.ahead.load(Ordering::Relaxed));
|
|
123
|
+
#[cfg(not(test))]
|
|
124
|
+
let _ = server;
|
|
125
|
+
now
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/// At accept: a plain connection goes to a worker_loop worker (pick); with none free, or one that
|
|
129
|
+
/// no longer takes it, it goes to hyper, counted as declined.
|
|
130
|
+
pub(crate) fn place(
|
|
131
|
+
server: &Arc<ServerInner>,
|
|
132
|
+
conn: Conn,
|
|
133
|
+
held: Held,
|
|
134
|
+
peer: Peer,
|
|
135
|
+
) -> Result<(), (Conn, Held)> {
|
|
136
|
+
let Some(inline) = &server.inline else {
|
|
137
|
+
return Err((conn, held));
|
|
138
|
+
};
|
|
139
|
+
let seat = {
|
|
140
|
+
let _placing = inline.placing.lock();
|
|
141
|
+
pick(server).map(|slot| take_seat(&slot))
|
|
142
|
+
};
|
|
143
|
+
let Some(seat) = seat else {
|
|
144
|
+
inline.declined.fetch_add(1, Ordering::Relaxed);
|
|
145
|
+
return Err((conn, held));
|
|
146
|
+
};
|
|
147
|
+
// The worker's scheduler waits on it from now on; failing that, it closes.
|
|
148
|
+
let Ok(stream) = StdConn::detach(conn) else {
|
|
149
|
+
return Ok(());
|
|
150
|
+
};
|
|
151
|
+
let home = tokio::runtime::Handle::current();
|
|
152
|
+
let conn = InlineConn::new(server, seat, stream, held, peer, home);
|
|
153
|
+
let slot = conn.slot().clone();
|
|
154
|
+
#[cfg(test)]
|
|
155
|
+
{
|
|
156
|
+
let picked = inline.on_pick.lock().take();
|
|
157
|
+
if let Some(meanwhile) = picked {
|
|
158
|
+
meanwhile(&slot);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
let Err(mut conn) = slot.send_placed(conn) else {
|
|
162
|
+
return Ok(());
|
|
163
|
+
};
|
|
164
|
+
// Never taken: to hyper, registered anew, which tokio does not know readable yet.
|
|
165
|
+
conn.send_to_hyper(false);
|
|
166
|
+
Ok(())
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/// A serving worker_loop worker in the pool with fewer inline connections than its capacity and
|
|
170
|
+
/// room for a request (one that filled up stays full until its loop next takes with a slot free).
|
|
171
|
+
/// Ties go to the fewest open connections, then requests, then the last parked.
|
|
172
|
+
fn pick(server: &ServerInner) -> Option<Arc<WorkerSlot>> {
|
|
173
|
+
server
|
|
174
|
+
.slots
|
|
175
|
+
.read()
|
|
176
|
+
.iter()
|
|
177
|
+
.filter(|slot| slot.lane.doorbell.is_some())
|
|
178
|
+
.filter(|slot| !slot.life.seems_retired() && !slot.life.quarantined())
|
|
179
|
+
.filter(|slot| {
|
|
180
|
+
slot.lane
|
|
181
|
+
.rx
|
|
182
|
+
.as_ref()
|
|
183
|
+
.is_some_and(|lane| !lane.is_disconnected())
|
|
184
|
+
})
|
|
185
|
+
.filter(|slot| {
|
|
186
|
+
let capacity = slot.load.capacity.load(Ordering::Relaxed);
|
|
187
|
+
capacity > 0 && slot.inline.conns.load(Ordering::Relaxed) < capacity
|
|
188
|
+
})
|
|
189
|
+
.filter(|slot| !slot.load.at_capacity())
|
|
190
|
+
.min_by_key(|slot| {
|
|
191
|
+
(
|
|
192
|
+
open_conns(slot),
|
|
193
|
+
slot.load.in_flight.load(Ordering::Relaxed),
|
|
194
|
+
std::cmp::Reverse(slot.park.parked_at.load(Ordering::Relaxed)),
|
|
195
|
+
)
|
|
196
|
+
})
|
|
197
|
+
.cloned()
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/// Inline connections less those answered whose job has not looked for the next request yet.
|
|
201
|
+
fn open_conns(slot: &WorkerSlot) -> usize {
|
|
202
|
+
slot.inline
|
|
203
|
+
.conns
|
|
204
|
+
.load(Ordering::Relaxed)
|
|
205
|
+
.saturating_sub(slot.inline.answered.load(Ordering::Relaxed))
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/// The drain began: idle connections are shut down, which wakes each job to close its own.
|
|
209
|
+
pub fn drain(server: &ServerInner) {
|
|
210
|
+
for slot in server.slots.read().iter() {
|
|
211
|
+
shut_idle(slot, Shut::Drain);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
fn shut_idle(slot: &WorkerSlot, shut: Shut) {
|
|
216
|
+
for io in ios(slot) {
|
|
217
|
+
io.shut_if_idle(shut);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/// `slot`'s worker was quarantined and may come back: its idle connections are shut so their
|
|
222
|
+
/// clients go elsewhere, and those it never took go to hyper. A request it serves stays its own.
|
|
223
|
+
pub fn quarantine(slot: &WorkerSlot) {
|
|
224
|
+
shut_idle(slot, Shut::Quarantine);
|
|
225
|
+
unplace(slot);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/// What was placed on `slot`'s worker and never taken goes to hyper, handed back.
|
|
229
|
+
pub fn unplace(slot: &WorkerSlot) {
|
|
230
|
+
if let Some(placed) = slot.lane.placed_rx.as_ref() {
|
|
231
|
+
while let Ok(mut conn) = placed.try_recv() {
|
|
232
|
+
conn.send_to_hyper(true);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/// `slot`'s worker crashed (Cut::Crash) or the shutdown passed its deadline (Cut::Shutdown): each
|
|
238
|
+
/// connection not ending already counts as cut off, once, in the one total of those Kino cut
|
|
239
|
+
/// (InlineSeats::closed); on a crash an idle one goes to hyper instead (abandon_slot). Runs
|
|
240
|
+
/// before the worker's requests are given up on, which ends their connections.
|
|
241
|
+
pub fn giving_up(slot: &WorkerSlot, why: Cut) {
|
|
242
|
+
for io in ios(slot) {
|
|
243
|
+
io.giving_up(why);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/// `slot`'s worker is given up on: each connection is answered or shut down and counted there no
|
|
248
|
+
/// more, whether or not its job looks again.
|
|
249
|
+
pub(crate) fn abandon_slot(slot: &WorkerSlot, why: Cut) {
|
|
250
|
+
for io in ios(slot) {
|
|
251
|
+
let (seat, held) = io.abandon(why);
|
|
252
|
+
if seat.is_some() {
|
|
253
|
+
forget(slot, &io);
|
|
254
|
+
}
|
|
255
|
+
drop((seat, held));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
pub fn shut_unserved(slot: &WorkerSlot) {
|
|
260
|
+
for io in ios(slot) {
|
|
261
|
+
io.shut_unless_serving();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
pub(super) fn ios(slot: &WorkerSlot) -> Vec<Arc<InlineIo>> {
|
|
266
|
+
slot.inline
|
|
267
|
+
.ios
|
|
268
|
+
.lock()
|
|
269
|
+
.iter()
|
|
270
|
+
.filter_map(Listed::io)
|
|
271
|
+
.collect()
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/// Kino::Native::Conn, made in the worker's Ractor by the take that found it. Its drop takes the
|
|
275
|
+
/// state lock and the worker's list, then hands the socket to hyper on the accepting runtime or
|
|
276
|
+
/// answers and shuts it, with no Ruby.
|
|
277
|
+
/// Wrapped in typed_data.rs, whose promise that drop keeps.
|
|
278
|
+
#[derive(magnus::DataTypeFunctions)]
|
|
279
|
+
pub struct ConnHandle(RefCell<Option<Box<InlineConn>>>);
|
|
280
|
+
|
|
281
|
+
pub fn wrap(ruby: &Ruby, conn: Box<InlineConn>) -> Value {
|
|
282
|
+
ruby.obj_wrap(ConnHandle(RefCell::new(Some(conn))))
|
|
283
|
+
.as_value()
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
impl ConnHandle {
|
|
287
|
+
pub fn fd(rb_self: &ConnHandle) -> Option<i32> {
|
|
288
|
+
rb_self.0.borrow().as_ref().and_then(|conn| conn.fd())
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/// Conn#next_request(room): the admitted env; :readable or :writable to wait first; :yield to
|
|
292
|
+
/// step aside first; nil once over. Without `room`, a request goes back to hyper with the
|
|
293
|
+
/// connection.
|
|
294
|
+
pub fn next_request(ruby: &Ruby, rb_self: &ConnHandle, room: bool) -> Result<Value, Error> {
|
|
295
|
+
let mut conn = rb_self.0.borrow_mut();
|
|
296
|
+
let Some(conn) = conn.as_mut() else {
|
|
297
|
+
return Ok(ruby.qnil().as_value());
|
|
298
|
+
};
|
|
299
|
+
let symbols = crate::symbols::get();
|
|
300
|
+
if conn.yield_turn() {
|
|
301
|
+
return Ok(symbols.step_aside.value());
|
|
302
|
+
}
|
|
303
|
+
crate::shared_gvl::pass_in_loop(conn.server(), conn.slot())?;
|
|
304
|
+
Ok(match conn.next(room) {
|
|
305
|
+
Next::Request(ctx) => {
|
|
306
|
+
crate::queue::admit_inline(ruby, conn.server(), conn.slot(), ctx)?.as_value()
|
|
307
|
+
}
|
|
308
|
+
Next::Readable => symbols.readable.value(),
|
|
309
|
+
Next::Writable => symbols.writable.value(),
|
|
310
|
+
Next::Closed => ruby.qnil().as_value(),
|
|
311
|
+
})
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/// Conn#close: a request still owed its answer gets a 500, then the socket closes. The borrow
|
|
315
|
+
/// cannot fail: the Conn never leaves its job's thread, and next_request's GVL pass, the one
|
|
316
|
+
/// way other Ruby code runs inside it, switches no fiber.
|
|
317
|
+
pub fn close(rb_self: &ConnHandle) {
|
|
318
|
+
if let Ok(mut conn) = rb_self.0.try_borrow_mut() {
|
|
319
|
+
conn.take();
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
#[cfg(test)]
|
|
325
|
+
mod tests {
|
|
326
|
+
use std::io::Write;
|
|
327
|
+
use std::sync::atomic::Ordering;
|
|
328
|
+
|
|
329
|
+
use tokio_util::either::Either;
|
|
330
|
+
|
|
331
|
+
use super::*;
|
|
332
|
+
use crate::inline::testing::{
|
|
333
|
+
accepted, answer, answer_dispatched, await_bytes, closes, cut_offs, handbacks,
|
|
334
|
+
inline_loop_server, look, loopback, loopback_holding, narrowed, next_request, once_written,
|
|
335
|
+
placed, placed_on_first, read_to_end, reads_within, send_get, wedge, Event, Wire,
|
|
336
|
+
};
|
|
337
|
+
use crate::queue::{Item, Take, Taker};
|
|
338
|
+
use crate::request::test_ctx;
|
|
339
|
+
use crate::testing::{permit, slot};
|
|
340
|
+
|
|
341
|
+
fn conns(server: &ServerInner) -> Vec<usize> {
|
|
342
|
+
let slots = server.slots.read();
|
|
343
|
+
slots
|
|
344
|
+
.iter()
|
|
345
|
+
.map(|slot| slot.inline.conns.load(Ordering::Relaxed))
|
|
346
|
+
.collect()
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
fn declined(server: &ServerInner) -> u64 {
|
|
350
|
+
server.inline.as_ref().expect("inline on").declined()
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
354
|
+
async fn a_unix_socket_connection_is_served_inline_and_handed_back_as_one() {
|
|
355
|
+
let server = inline_loop_server(1, 8);
|
|
356
|
+
let (client, accepted) = std::os::unix::net::UnixStream::pair().expect("a socket pair");
|
|
357
|
+
accepted.set_nonblocking(true).expect("nonblocking");
|
|
358
|
+
let accepted = tokio::net::UnixStream::from_std(accepted).expect("registered");
|
|
359
|
+
let peer = crate::server::UNIX_PEER;
|
|
360
|
+
let held = Held::new(permit(), None);
|
|
361
|
+
assert!(place(&server, Either::Right(accepted), held, peer).is_ok());
|
|
362
|
+
let slot = slot(&server, 0);
|
|
363
|
+
let Take::Item(Item::Conn(mut conn)) = Taker::new(&server, &slot).ready() else {
|
|
364
|
+
panic!("its worker did not take the unix-socket connection");
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
// Served inline, with the addresses hyper gives a unix-socket
|
|
368
|
+
// request.
|
|
369
|
+
let timeout = Some(std::time::Duration::from_secs(5));
|
|
370
|
+
client.set_read_timeout(timeout).expect("read timeout");
|
|
371
|
+
(&client)
|
|
372
|
+
.write_all(b"GET /inline HTTP/1.1\r\nhost: x\r\n\r\n")
|
|
373
|
+
.unwrap();
|
|
374
|
+
let ctx = next_request(&mut conn);
|
|
375
|
+
assert_eq!((ctx.peer.remote, ctx.peer.local), (peer.remote, peer.local));
|
|
376
|
+
answer(&ctx, b"inline");
|
|
377
|
+
drop(ctx);
|
|
378
|
+
let mut wire = Wire::of(&client);
|
|
379
|
+
assert_eq!(wire.response().2, b"inline");
|
|
380
|
+
assert!(
|
|
381
|
+
!conn.readable(),
|
|
382
|
+
"an idle unix socket read as holding its client's bytes"
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
// An HTTP/1.0 request goes back to hyper, which answers it on the
|
|
386
|
+
// same socket, registered anew as a unix stream.
|
|
387
|
+
(&client).write_all(b"GET /back HTTP/1.0\r\n\r\n").unwrap();
|
|
388
|
+
await_bytes(&conn);
|
|
389
|
+
assert!(matches!(look(&mut conn, true), Next::Closed));
|
|
390
|
+
answer_dispatched(&server, &["/back"]).await;
|
|
391
|
+
let (status, _, body) = wire.response();
|
|
392
|
+
assert_eq!(
|
|
393
|
+
(status.as_str(), &body[..]),
|
|
394
|
+
("HTTP/1.0 200 OK", &b"hyper:/back"[..])
|
|
395
|
+
);
|
|
396
|
+
assert_eq!((handbacks(&server), conns(&server)), (1, vec![0]));
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/// How a worker leaves what was placed on it and not taken yet.
|
|
400
|
+
#[derive(Clone, Copy, Debug)]
|
|
401
|
+
enum Leaves {
|
|
402
|
+
/// Its loop takes it as the worker retires.
|
|
403
|
+
TakenRetiring,
|
|
404
|
+
/// Its thread ends with no crash to tell.
|
|
405
|
+
Ends,
|
|
406
|
+
/// Its thread ends once it retired.
|
|
407
|
+
EndsRetired,
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
411
|
+
async fn what_was_placed_on_a_leaving_worker_goes_to_hyper_with_its_client_and_no_more_comes() {
|
|
412
|
+
for leaves in [Leaves::TakenRetiring, Leaves::Ends, Leaves::EndsRetired] {
|
|
413
|
+
let server = inline_loop_server(2, 8);
|
|
414
|
+
let sibling = slot(&server, 1);
|
|
415
|
+
let (slot, worker) = crate::queue::testing::boot(&server, 0);
|
|
416
|
+
let client = placed_on_first(&server);
|
|
417
|
+
send_get(&client, "/placed", false);
|
|
418
|
+
if !matches!(leaves, Leaves::Ends) {
|
|
419
|
+
slot.retire();
|
|
420
|
+
}
|
|
421
|
+
if let Leaves::TakenRetiring = leaves {
|
|
422
|
+
let sibling = Taker::new(&server, &sibling).ready();
|
|
423
|
+
assert!(matches!(sibling, Take::Nothing), "a sibling took it");
|
|
424
|
+
let Take::Item(Item::Conn(mut conn)) = Taker::new(&server, &slot).ready() else {
|
|
425
|
+
panic!("the retiring worker did not take its connection");
|
|
426
|
+
};
|
|
427
|
+
await_bytes(&conn);
|
|
428
|
+
assert!(
|
|
429
|
+
matches!(conn.next(true), Next::Closed),
|
|
430
|
+
"a retired worker served it"
|
|
431
|
+
);
|
|
432
|
+
} else {
|
|
433
|
+
crate::queue::Worker::release(&worker);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
answer_dispatched(&server, &["/placed"]).await;
|
|
437
|
+
let (_, headers, body) = Wire::of(&client).response();
|
|
438
|
+
assert_eq!(body, b"hyper:/placed", "{leaves:?}");
|
|
439
|
+
assert!(
|
|
440
|
+
!closes(&headers),
|
|
441
|
+
"{leaves:?}: its client lost the connection"
|
|
442
|
+
);
|
|
443
|
+
assert_eq!(handbacks(&server), 1, "{leaves:?}");
|
|
444
|
+
for _ in 0..2 {
|
|
445
|
+
drop(placed(&server));
|
|
446
|
+
}
|
|
447
|
+
assert_eq!(
|
|
448
|
+
conns(&server),
|
|
449
|
+
[0, 2],
|
|
450
|
+
"{leaves:?}: placed on the worker that left"
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
456
|
+
async fn quarantine_shuts_idle_connections_hands_back_placed_ones_and_ends_the_rest_after_their_answers(
|
|
457
|
+
) {
|
|
458
|
+
let server = inline_loop_server(2, 8);
|
|
459
|
+
let slots = server.slots.read().clone();
|
|
460
|
+
// One idle on it, looked at by its job once, its client silent.
|
|
461
|
+
let (idle_client, mut idle) = loopback(&server);
|
|
462
|
+
assert!(matches!(idle.next(true), Next::Readable));
|
|
463
|
+
// As its worker is given up on as wedged, a request it is stuck
|
|
464
|
+
// in, not answered yet, and one answered, its job not back yet.
|
|
465
|
+
let (stuck_client, mut stuck) = loopback(&server);
|
|
466
|
+
send_get(&stuck_client, "/stuck", false);
|
|
467
|
+
let late = next_request(&mut stuck);
|
|
468
|
+
crate::queue::enlist_inline(&server, &slots[0], &late);
|
|
469
|
+
let (done_client, mut done) = loopback(&server);
|
|
470
|
+
send_get(&done_client, "/done", false);
|
|
471
|
+
answer(&next_request(&mut done), b"done");
|
|
472
|
+
// Placed on it, and not taken: its client's request comes.
|
|
473
|
+
let placed_client = placed_on_first(&server);
|
|
474
|
+
send_get(&placed_client, "/placed", false);
|
|
475
|
+
|
|
476
|
+
wedge(&slots[0]);
|
|
477
|
+
|
|
478
|
+
assert_eq!(
|
|
479
|
+
read_to_end(&idle_client),
|
|
480
|
+
b"",
|
|
481
|
+
"an idle connection stayed open"
|
|
482
|
+
);
|
|
483
|
+
answer_dispatched(&server, &["/placed"]).await;
|
|
484
|
+
assert_eq!(Wire::of(&placed_client).response().2, b"hyper:/placed");
|
|
485
|
+
drop(placed(&server));
|
|
486
|
+
assert_eq!(
|
|
487
|
+
conns(&server)[1],
|
|
488
|
+
1,
|
|
489
|
+
"placement did not pass over the quarantined worker"
|
|
490
|
+
);
|
|
491
|
+
|
|
492
|
+
// Its worker comes back, and its jobs look again: what each owes
|
|
493
|
+
// goes out, and its connection ends.
|
|
494
|
+
assert!(
|
|
495
|
+
matches!(done.next(true), Next::Closed),
|
|
496
|
+
"kept alive after its answer"
|
|
497
|
+
);
|
|
498
|
+
let mut wire = Wire::of(&done_client);
|
|
499
|
+
assert_eq!(
|
|
500
|
+
(wire.response().2, wire.answer(false)),
|
|
501
|
+
(b"done".to_vec(), Event::Closed)
|
|
502
|
+
);
|
|
503
|
+
answer(&late, b"late");
|
|
504
|
+
let mut wire = Wire::of(&stuck_client);
|
|
505
|
+
let (_, headers, body) = wire.response();
|
|
506
|
+
assert!(
|
|
507
|
+
body == b"late" && closes(&headers),
|
|
508
|
+
"the answer kept its connection alive: {headers:?}"
|
|
509
|
+
);
|
|
510
|
+
assert!(matches!(stuck.next(true), Next::Closed));
|
|
511
|
+
assert_eq!(wire.answer(false), Event::Closed);
|
|
512
|
+
assert!(matches!(idle.next(true), Next::Closed));
|
|
513
|
+
assert_eq!(
|
|
514
|
+
(cut_offs(&server), handbacks(&server), conns(&server)[0]),
|
|
515
|
+
(2, 1, 0),
|
|
516
|
+
"the idle one and the one answered before cut off, the one placed handed back, and \
|
|
517
|
+
none counted on the worker"
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
522
|
+
async fn an_answer_still_going_out_as_the_drain_begins_or_its_worker_is_given_up_on_goes_out_whole_and_ends_its_connection(
|
|
523
|
+
) {
|
|
524
|
+
// The request behind comes in the first one's write, read with it, or in a second write
|
|
525
|
+
// once the job read the first, left unread in the socket: a close with bytes unread
|
|
526
|
+
// resets the connection.
|
|
527
|
+
for (writes, quarantined) in [(1, false), (1, true), (2, false), (2, true)] {
|
|
528
|
+
let server = inline_loop_server(2, 8);
|
|
529
|
+
let slot = slot(&server, 0);
|
|
530
|
+
let (client, mut conn) = narrowed(&server);
|
|
531
|
+
let behind = b"GET /behind HTTP/1.1\r\nhost: x\r\n\r\n";
|
|
532
|
+
let mut sent = b"GET /big HTTP/1.1\r\nhost: x\r\n\r\n".to_vec();
|
|
533
|
+
if writes == 1 {
|
|
534
|
+
sent.extend_from_slice(behind);
|
|
535
|
+
}
|
|
536
|
+
(&client).write_all(&sent).unwrap();
|
|
537
|
+
let ctx = next_request(&mut conn);
|
|
538
|
+
if writes == 2 {
|
|
539
|
+
(&client).write_all(behind).unwrap();
|
|
540
|
+
let fd = conn.fd().expect("open");
|
|
541
|
+
assert!(reads_within(fd, 5_000), "the request behind never came");
|
|
542
|
+
}
|
|
543
|
+
let big = vec![b'x'; 1 << 20];
|
|
544
|
+
answer(&ctx, &big);
|
|
545
|
+
drop(ctx);
|
|
546
|
+
assert!(
|
|
547
|
+
matches!(conn.next(true), Next::Writable),
|
|
548
|
+
"the answer went out at once"
|
|
549
|
+
);
|
|
550
|
+
|
|
551
|
+
if quarantined {
|
|
552
|
+
wedge(&slot);
|
|
553
|
+
} else {
|
|
554
|
+
server.stop_accepting();
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// Its job's look puts the rest out (its worker back, if it was
|
|
558
|
+
// given up on), then ends the connection.
|
|
559
|
+
let reader = std::thread::spawn(move || {
|
|
560
|
+
let mut wire = Wire::of(&client);
|
|
561
|
+
(wire.response().2.len(), wire.end())
|
|
562
|
+
});
|
|
563
|
+
let served = matches!(once_written(&mut conn), Next::Request(_));
|
|
564
|
+
drop(conn);
|
|
565
|
+
let (read, rest) = reader.join().expect("the client");
|
|
566
|
+
let what = format!("{writes} writes, quarantined {quarantined}");
|
|
567
|
+
assert!(!served, "{what}: served a request sent behind the answer");
|
|
568
|
+
assert_eq!(
|
|
569
|
+
(read, rest),
|
|
570
|
+
(big.len(), Event::Closed),
|
|
571
|
+
"{what}: cut short, reset or left open"
|
|
572
|
+
);
|
|
573
|
+
assert_eq!(cut_offs(&server), u64::from(quarantined), "{what}");
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
578
|
+
async fn a_crashed_workers_connections_are_answered_or_handed_to_hyper_though_no_job_looks_again(
|
|
579
|
+
) {
|
|
580
|
+
let server = inline_loop_server(2, 8);
|
|
581
|
+
let slots = server.slots.read().clone();
|
|
582
|
+
let slot = &slots[0];
|
|
583
|
+
// One serving the request the worker crashed in.
|
|
584
|
+
let (serving_client, mut serving) = loopback(&server);
|
|
585
|
+
send_get(&serving_client, "/crash", false);
|
|
586
|
+
let crashed = next_request(&mut serving);
|
|
587
|
+
crate::queue::enlist_inline(&server, slot, &crashed);
|
|
588
|
+
// One answered, its job not back yet, holding one of the server's
|
|
589
|
+
// connection permits.
|
|
590
|
+
let permits = Arc::new(tokio::sync::Semaphore::new(1));
|
|
591
|
+
let permit = permits.clone().try_acquire_owned().expect("a permit");
|
|
592
|
+
let (answered_client, mut answered) = loopback_holding(&server, permit);
|
|
593
|
+
send_get(&answered_client, "/done", false);
|
|
594
|
+
answer(&next_request(&mut answered), b"done");
|
|
595
|
+
// One answered, its job stepping aside for the others, as its
|
|
596
|
+
// client's next request comes.
|
|
597
|
+
let (aside_client, mut aside) = loopback(&server);
|
|
598
|
+
send_get(&aside_client, "/first", false);
|
|
599
|
+
answer(&next_request(&mut aside), b"first");
|
|
600
|
+
assert_eq!(Wire::of(&aside_client).response().2, b"first");
|
|
601
|
+
assert!(
|
|
602
|
+
aside.yield_turn(),
|
|
603
|
+
"the answered connection did not step aside"
|
|
604
|
+
);
|
|
605
|
+
send_get(&aside_client, "/next", false);
|
|
606
|
+
await_bytes(&aside);
|
|
607
|
+
// One placed on it that it never took, its client's request sent.
|
|
608
|
+
let placed_client = placed_on_first(&server);
|
|
609
|
+
send_get(&placed_client, "/placed", false);
|
|
610
|
+
assert!(
|
|
611
|
+
slot.recycle().is_none(),
|
|
612
|
+
"a slot its connections count on was recycled"
|
|
613
|
+
);
|
|
614
|
+
|
|
615
|
+
slot.crash(&server);
|
|
616
|
+
|
|
617
|
+
let status = Wire::of(&serving_client).response().0;
|
|
618
|
+
assert_eq!(status, "HTTP/1.1 500 Internal Server Error");
|
|
619
|
+
assert_eq!(read_to_end(&serving_client), b"");
|
|
620
|
+
assert_eq!(Wire::of(&answered_client).response().2, b"done");
|
|
621
|
+
assert_eq!(read_to_end(&answered_client), b"");
|
|
622
|
+
answer_dispatched(&server, &["/placed", "/next"]).await;
|
|
623
|
+
assert_eq!(Wire::of(&placed_client).response().2, b"hyper:/placed");
|
|
624
|
+
assert_eq!(Wire::of(&aside_client).response().2, b"hyper:/next");
|
|
625
|
+
// Counted once, and nowhere once their worker is gone, nor again
|
|
626
|
+
// as their jobs look or go with the dead worker's heap.
|
|
627
|
+
let counts = || {
|
|
628
|
+
(
|
|
629
|
+
cut_offs(&server),
|
|
630
|
+
handbacks(&server),
|
|
631
|
+
slot.inline.conns.load(Ordering::Relaxed),
|
|
632
|
+
slot.inline.answered.load(Ordering::Relaxed),
|
|
633
|
+
server.counters.in_flight.load(Ordering::Relaxed),
|
|
634
|
+
)
|
|
635
|
+
};
|
|
636
|
+
let settled = (2, 2, 0, 0, 0);
|
|
637
|
+
assert_eq!(counts(), settled);
|
|
638
|
+
assert_eq!(
|
|
639
|
+
permits.available_permits(),
|
|
640
|
+
1,
|
|
641
|
+
"a dead worker's connection holds its permit"
|
|
642
|
+
);
|
|
643
|
+
assert!(matches!(answered.next(true), Next::Closed));
|
|
644
|
+
assert!(matches!(aside.next(true), Next::Closed));
|
|
645
|
+
drop((serving, answered, aside, crashed));
|
|
646
|
+
assert_eq!(counts(), settled);
|
|
647
|
+
assert!(slot.recycle().is_some(), "a quiet crashed slot is recycled");
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
651
|
+
async fn a_connection_being_placed_as_its_worker_crashes_goes_to_hyper_untouched() {
|
|
652
|
+
let server = inline_loop_server(2, 8);
|
|
653
|
+
let slot = slot(&server, 0);
|
|
654
|
+
let (_client, crate::server::Accepted { conn, peer, held }) = accepted();
|
|
655
|
+
// As place has it: its seat taken and the connection made, not
|
|
656
|
+
// sent to the worker yet, when the worker crashes.
|
|
657
|
+
let stream = StdConn::detach(conn).expect("a std stream");
|
|
658
|
+
let home = tokio::runtime::Handle::current();
|
|
659
|
+
let conn = InlineConn::new(&server, take_seat(&slot), stream, held, peer, home);
|
|
660
|
+
slot.crash(&server);
|
|
661
|
+
let Err(mut conn) = slot.send_placed(conn) else {
|
|
662
|
+
panic!("a crashed worker took a connection");
|
|
663
|
+
};
|
|
664
|
+
assert!(
|
|
665
|
+
conn.let_go_to_hyper().is_some(),
|
|
666
|
+
"the crash cut a connection its worker never had"
|
|
667
|
+
);
|
|
668
|
+
assert_eq!(slot.inline.conns.load(Ordering::Relaxed), 0);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
672
|
+
async fn a_full_worker_takes_a_connection_placed_on_it_and_hands_its_request_to_hyper() {
|
|
673
|
+
use crate::queue::Room;
|
|
674
|
+
// Placed on the first while it has room, and a request sent to its
|
|
675
|
+
// lane; then a request fills it before its loop takes either: its
|
|
676
|
+
// loop takes events and the connection (Room::Events), or, its
|
|
677
|
+
// backlog of events full too, the connection alone, an event left
|
|
678
|
+
// in its inbox (Room::Connections).
|
|
679
|
+
for room in [Room::Events, Room::Connections] {
|
|
680
|
+
let server = inline_loop_server(2, 1);
|
|
681
|
+
let slots = server.slots.read().clone();
|
|
682
|
+
let client = placed_on_first(&server);
|
|
683
|
+
let lane = crate::registry::LaneItem::Request(test_ctx());
|
|
684
|
+
assert!(slots[0].send_lane(lane).is_ok());
|
|
685
|
+
let running = test_ctx();
|
|
686
|
+
crate::queue::enlist_inline(&server, &slots[0], &running);
|
|
687
|
+
let inbox = slots[0].lane.ws_tx.as_ref().expect("an inbox");
|
|
688
|
+
if room == Room::Connections {
|
|
689
|
+
assert!(inbox.try_send(crate::ws::WsEvent::Close { id: 1 }).is_ok());
|
|
690
|
+
}
|
|
691
|
+
send_get(&client, "/placed", false);
|
|
692
|
+
|
|
693
|
+
let taker = Taker::new(&server, &slots[0]);
|
|
694
|
+
let Take::Item(Item::Conn(mut conn)) = taker.nonblock(room) else {
|
|
695
|
+
panic!("{room:?}: a full worker left the connection placed on it waiting");
|
|
696
|
+
};
|
|
697
|
+
assert!(
|
|
698
|
+
matches!(taker.nonblock(room), Take::Nothing),
|
|
699
|
+
"{room:?}: took more"
|
|
700
|
+
);
|
|
701
|
+
let lane = slots[0].lane.rx.as_ref().expect("a lane").len();
|
|
702
|
+
let inbox = slots[0].lane.ws_rx.as_ref().expect("an inbox").len();
|
|
703
|
+
let event = usize::from(room == Room::Connections);
|
|
704
|
+
assert_eq!(
|
|
705
|
+
(lane, inbox),
|
|
706
|
+
(1, event),
|
|
707
|
+
"{room:?}: the request or the event was taken"
|
|
708
|
+
);
|
|
709
|
+
await_bytes(&conn);
|
|
710
|
+
assert!(
|
|
711
|
+
matches!(conn.next(false), Next::Closed),
|
|
712
|
+
"{room:?}: kept a request"
|
|
713
|
+
);
|
|
714
|
+
assert_eq!(handbacks(&server), 1);
|
|
715
|
+
answer_dispatched(&server, &["/placed"]).await;
|
|
716
|
+
assert_eq!(Wire::of(&client).response().2, b"hyper:/placed");
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/// On a runtime of one thread, so that nothing reports the socket,
|
|
721
|
+
/// registered anew, readable before hyper first reads it.
|
|
722
|
+
#[test]
|
|
723
|
+
fn a_connection_its_worker_refuses_as_it_is_sent_is_declined_and_served_by_hyper() {
|
|
724
|
+
// Its worker retires as placement picks it; as the drain begins
|
|
725
|
+
// too, hyper still answers the request already sent, and closes.
|
|
726
|
+
for drain in [false, true] {
|
|
727
|
+
let runtime = tokio::runtime::Builder::new_current_thread()
|
|
728
|
+
.enable_all()
|
|
729
|
+
.build();
|
|
730
|
+
runtime.expect("a runtime").block_on(async {
|
|
731
|
+
let server = inline_loop_server(2, 8);
|
|
732
|
+
let _accepting = server.shutdown_tx.subscribe();
|
|
733
|
+
let inline = server.inline.clone().expect("inline on");
|
|
734
|
+
let draining = server.clone();
|
|
735
|
+
*inline.on_pick.lock() = Some(Box::new(move |slot: &WorkerSlot| {
|
|
736
|
+
slot.retire();
|
|
737
|
+
if drain {
|
|
738
|
+
draining.stop_accepting();
|
|
739
|
+
}
|
|
740
|
+
}));
|
|
741
|
+
let (client, accepted) = accepted();
|
|
742
|
+
send_get(&client, "/refused", false);
|
|
743
|
+
let Either::Left(stream) = &accepted.conn else {
|
|
744
|
+
panic!("a TCP connection");
|
|
745
|
+
};
|
|
746
|
+
stream.peek(&mut [0u8]).await.expect("its bytes in");
|
|
747
|
+
tokio::spawn(crate::server::serve_conn(accepted, None, server.clone()));
|
|
748
|
+
let answered = tokio::time::timeout(
|
|
749
|
+
std::time::Duration::from_secs(3),
|
|
750
|
+
answer_dispatched(&server, &["/refused"]),
|
|
751
|
+
);
|
|
752
|
+
assert!(answered.await.is_ok(), "drain {drain}: never read");
|
|
753
|
+
let reading = tokio::task::spawn_blocking(move || Wire::of(&client).response());
|
|
754
|
+
let (_, headers, body) = reading.await.expect("the client");
|
|
755
|
+
assert_eq!(body, b"hyper:/refused", "drain {drain}");
|
|
756
|
+
assert_eq!(closes(&headers), drain, "drain {drain}: {headers:?}");
|
|
757
|
+
assert!(
|
|
758
|
+
inline.on_pick.lock().is_none(),
|
|
759
|
+
"placement picked no worker"
|
|
760
|
+
);
|
|
761
|
+
let counts = (conns(&server), declined(&server), handbacks(&server));
|
|
762
|
+
assert_eq!(counts, (vec![0, 0], 1, 0), "drain {drain}");
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|