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.
- checksums.yaml +4 -4
- data/.yardopts +5 -0
- data/CHANGELOG.md +286 -0
- data/Cargo.lock +209 -132
- 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 +19 -3
- 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,487 @@
|
|
|
1
|
+
//! What an inline connection shares with the threads that answer it (InlineIo, its state under
|
|
2
|
+
//! one lock), and how it ends: shut down, abandoned with its worker, or handed back to hyper.
|
|
3
|
+
|
|
4
|
+
use std::io::{ErrorKind, IoSlice, Read, Write};
|
|
5
|
+
use std::net::Shutdown;
|
|
6
|
+
use std::os::fd::{AsRawFd, RawFd};
|
|
7
|
+
use std::sync::atomic::{AtomicU64, Ordering};
|
|
8
|
+
use std::sync::{Arc, Weak};
|
|
9
|
+
use std::time::Duration;
|
|
10
|
+
|
|
11
|
+
use bytes::Bytes;
|
|
12
|
+
use parking_lot::Mutex;
|
|
13
|
+
|
|
14
|
+
use super::answer::Current;
|
|
15
|
+
use crate::counters::Counted;
|
|
16
|
+
use crate::registry::ServerInner;
|
|
17
|
+
use crate::response::Cut;
|
|
18
|
+
use crate::server::{Held, Peer, StdConn, HEADER_READ_TIMEOUT};
|
|
19
|
+
use crate::slot::WorkerSlot;
|
|
20
|
+
|
|
21
|
+
/// The socket and what its requests share with the threads that may answer them.
|
|
22
|
+
/// Its last Arc may go in a thread-safe free, so what it holds keeps typed_data.rs's promise.
|
|
23
|
+
pub struct InlineIo {
|
|
24
|
+
pub(super) state: Mutex<IoState>,
|
|
25
|
+
pub(super) deadline: Deadline,
|
|
26
|
+
pub(super) origin: Origin,
|
|
27
|
+
/// The worker it was placed on, which counts it while it holds its seat.
|
|
28
|
+
pub(super) slot: Arc<WorkerSlot>,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// When the connection is cut off if it still waits on its client (InlineIo::expire), on the
|
|
32
|
+
/// inline clock: HEADER_READ_TIMEOUT after its accept or its last answer, BODY_READ_TIMEOUT after
|
|
33
|
+
/// a body's last bytes; with request_timeout set, an answer must begin by it or the request gets
|
|
34
|
+
/// a 504. Set under the state's lock, but for a body's bytes, which only its job reads; the
|
|
35
|
+
/// ticker reads it off the worker's list (Listed) with no lock.
|
|
36
|
+
#[derive(Clone)]
|
|
37
|
+
pub(super) struct Deadline(Arc<AtomicU64>);
|
|
38
|
+
|
|
39
|
+
impl Deadline {
|
|
40
|
+
pub(super) fn new(at: u64) -> Deadline {
|
|
41
|
+
Deadline(Arc::new(AtomicU64::new(at)))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
pub(super) fn due(&self, now: u64) -> bool {
|
|
45
|
+
self.0.load(Ordering::Relaxed) <= now
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
pub(super) fn set(&self, at: u64) {
|
|
49
|
+
self.0.store(at, Ordering::Relaxed);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
pub(super) fn due_in(&self, server: &ServerInner, timeout: Duration) {
|
|
53
|
+
self.set(super::now(server) + micros(timeout));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// Of a connection waiting for its next head: what is left of HEADER_READ_TIMEOUT.
|
|
57
|
+
fn left(&self, server: &ServerInner) -> Duration {
|
|
58
|
+
let at = self.0.load(Ordering::Relaxed);
|
|
59
|
+
Duration::from_micros(at.saturating_sub(super::now(server)))
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/// What the socket did not take yet: `buf` from `pos` on.
|
|
64
|
+
#[derive(Default)]
|
|
65
|
+
pub(super) struct WriteBuf {
|
|
66
|
+
pub(super) buf: Vec<u8>,
|
|
67
|
+
pub(super) pos: usize,
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// A larger write buffer is let go once empty, so one big response does not keep its memory.
|
|
71
|
+
const WBUF_KEEP: usize = 64 * 1024;
|
|
72
|
+
|
|
73
|
+
impl WriteBuf {
|
|
74
|
+
pub(super) fn sent(&self) -> bool {
|
|
75
|
+
self.pos == self.buf.len()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
pub(super) fn clear(&mut self) {
|
|
79
|
+
self.buf.clear();
|
|
80
|
+
self.pos = 0;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// `parts` after what is buffered: as much as `stream` takes now, the rest kept for `flush`.
|
|
84
|
+
fn write(&mut self, stream: &StdConn, parts: &[&[u8]]) -> std::io::Result<()> {
|
|
85
|
+
let mut written = 0;
|
|
86
|
+
if self.sent() {
|
|
87
|
+
self.clear();
|
|
88
|
+
let slices: smallvec::SmallVec<[IoSlice; 3]> =
|
|
89
|
+
parts.iter().map(|part| IoSlice::new(part)).collect();
|
|
90
|
+
written = loop {
|
|
91
|
+
match (&stream.socket).write_vectored(&slices) {
|
|
92
|
+
Ok(n) => break n,
|
|
93
|
+
Err(e) if e.kind() == ErrorKind::Interrupted => {}
|
|
94
|
+
Err(e) if e.kind() == ErrorKind::WouldBlock => break 0,
|
|
95
|
+
Err(e) => return Err(e),
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
for part in parts {
|
|
100
|
+
let skip = written.min(part.len());
|
|
101
|
+
written -= skip;
|
|
102
|
+
self.buf.extend_from_slice(&part[skip..]);
|
|
103
|
+
}
|
|
104
|
+
Ok(())
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/// True once nothing is buffered.
|
|
108
|
+
fn flush(&mut self, stream: &StdConn) -> std::io::Result<bool> {
|
|
109
|
+
while self.pos < self.buf.len() {
|
|
110
|
+
match stream.send(&self.buf[self.pos..]) {
|
|
111
|
+
Ok(0) => return Err(ErrorKind::WriteZero.into()),
|
|
112
|
+
Ok(n) => self.pos += n,
|
|
113
|
+
Err(e) if e.kind() == ErrorKind::Interrupted => {}
|
|
114
|
+
Err(e) if e.kind() == ErrorKind::WouldBlock => return Ok(false),
|
|
115
|
+
Err(e) => return Err(e),
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if self.buf.capacity() > WBUF_KEEP {
|
|
119
|
+
self.buf = Vec::new();
|
|
120
|
+
}
|
|
121
|
+
self.clear();
|
|
122
|
+
Ok(true)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/// On its worker's list, with its deadline, which the ticker reads without the connection's lock.
|
|
127
|
+
pub struct Listed {
|
|
128
|
+
pub(super) deadline: Deadline,
|
|
129
|
+
pub(super) io: Weak<InlineIo>,
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
impl Listed {
|
|
133
|
+
pub(crate) fn io(&self) -> Option<Arc<InlineIo>> {
|
|
134
|
+
self.io.upgrade()
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
pub(crate) fn is(&self, io: *const InlineIo) -> bool {
|
|
138
|
+
std::ptr::eq(self.io.as_ptr(), io)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/// What hyper needs to serve the connection from where it is: handed back by its job, or taken
|
|
143
|
+
/// over idle after its job ended or its worker crashed.
|
|
144
|
+
pub(crate) struct Origin {
|
|
145
|
+
pub(crate) server: Arc<ServerInner>,
|
|
146
|
+
/// Hand-backs go to the I/O runtime that accepted the connection.
|
|
147
|
+
pub(crate) home: tokio::runtime::Handle,
|
|
148
|
+
pub(crate) peer: Peer,
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
#[derive(Default)]
|
|
152
|
+
pub(super) struct IoState {
|
|
153
|
+
/// None once closed or handed back; kept past its job's let-go while a write waits (`lent`).
|
|
154
|
+
pub(super) stream: Option<StdConn>,
|
|
155
|
+
/// Its count on its worker and its max_connections permit, given back once by whichever comes
|
|
156
|
+
/// first: its job letting it go (the permit to hyper with the socket) or abandon.
|
|
157
|
+
pub(super) seat: Option<Seat>,
|
|
158
|
+
pub(super) held: Option<Held>,
|
|
159
|
+
pub(super) out: WriteBuf,
|
|
160
|
+
/// The connection ends once what it wrote is out.
|
|
161
|
+
pub(super) closing: bool,
|
|
162
|
+
/// What its job waits on the socket for, as a drain or a quarantine sees it.
|
|
163
|
+
pub(super) waits: Waits,
|
|
164
|
+
/// The last answer is whole and its job has not looked since (nor stepped aside): counted in
|
|
165
|
+
/// its worker's InlineSeats::answered meanwhile.
|
|
166
|
+
pub(super) answered: bool,
|
|
167
|
+
/// Bytes read past the returned request wait unparsed: a next request may sit whole in the
|
|
168
|
+
/// buffer, which no deadline cuts off (hyper parses a head it holds).
|
|
169
|
+
pub(super) unparsed: bool,
|
|
170
|
+
/// Writes waiting on the socket (Wrote::Wait) not back yet: the descriptor stays open
|
|
171
|
+
/// meanwhile, so no wait meets a closed or reused one.
|
|
172
|
+
pub(super) lent: usize,
|
|
173
|
+
/// Its job let it go while a write still waited: the last write back closes it.
|
|
174
|
+
pub(super) released: bool,
|
|
175
|
+
/// Its job looked at it: until then it may go to hyper untouched (inline::unplace), and no
|
|
176
|
+
/// crash counts it as cut off.
|
|
177
|
+
pub(super) taken: bool,
|
|
178
|
+
/// The request whose streamed body hyper would have dropped (a refused head, a length past
|
|
179
|
+
/// its end), by its Responder's address (0: none): its next write finds the client gone,
|
|
180
|
+
/// counted as hyper counts it.
|
|
181
|
+
pub(super) dropped: usize,
|
|
182
|
+
/// The request being served, until its answer is written.
|
|
183
|
+
pub(super) current: Option<Current>,
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
#[derive(Clone, Copy, Default, PartialEq, Eq)]
|
|
187
|
+
pub(super) enum Waits {
|
|
188
|
+
/// Nothing below: its job looks, or waits on a body or a write.
|
|
189
|
+
#[default]
|
|
190
|
+
Other,
|
|
191
|
+
/// The next request, nothing of it read (or the step aside just before: yield_turn).
|
|
192
|
+
Idle,
|
|
193
|
+
/// The rest of a head, on a connection that served a request.
|
|
194
|
+
MidHead,
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/// A connection's count on its worker (InlineSeats::conns), given back on drop.
|
|
198
|
+
pub(crate) type Seat = Counted<WorkerSlot>;
|
|
199
|
+
|
|
200
|
+
pub(crate) fn take_seat(slot: &Arc<WorkerSlot>) -> Seat {
|
|
201
|
+
Counted::on(slot, |slot| &slot.inline.conns)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
#[derive(Clone, Copy)]
|
|
205
|
+
pub(crate) enum Shut {
|
|
206
|
+
/// Counts no close.
|
|
207
|
+
Drain,
|
|
208
|
+
/// Counted as cut off.
|
|
209
|
+
Quarantine,
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
impl InlineIo {
|
|
213
|
+
/// The ticker's look once the deadline passed (inline::tick): a connection still waiting on
|
|
214
|
+
/// its client is shut down, which wakes its job to close it; its client reads the end. A
|
|
215
|
+
/// request whose answer has not begun past request_timeout gets a 504 and the connection ends
|
|
216
|
+
/// after it (time_out). One serving a request, or writing out on a worker that stays, waits
|
|
217
|
+
/// on no client: it is looked at again a header timeout on. On a worker that leaves, a client
|
|
218
|
+
/// that took nothing more of an answer for a header timeout is cut off, so the worker can go.
|
|
219
|
+
/// While its worker lives, one whose job holds unparsed bytes or whose client's bytes wait
|
|
220
|
+
/// unread is spared: its thread was only held up.
|
|
221
|
+
pub(super) fn expire(&self, now: u64) {
|
|
222
|
+
let slot = &*self.slot;
|
|
223
|
+
let mut state = self.state.lock();
|
|
224
|
+
if !self.deadline.due(now) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if self.times_out(&state) {
|
|
228
|
+
self.time_out(&mut state);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
let alive = !slot.life.quarantined();
|
|
232
|
+
if state.current.is_some() || !state.out.sent() {
|
|
233
|
+
let leaving = !alive || slot.life.seems_retired();
|
|
234
|
+
if leaving && state.current.is_none() && !(alive && state.writable()) {
|
|
235
|
+
state.ends(slot);
|
|
236
|
+
state.shutdown();
|
|
237
|
+
} else {
|
|
238
|
+
self.deadline.set(now + micros(HEADER_READ_TIMEOUT));
|
|
239
|
+
}
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if alive && (state.unparsed || state.unread()) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
state.ends(slot);
|
|
246
|
+
state.shutdown();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/// A drain's or a quarantine's look: the socket is shut down, which wakes the job to close
|
|
250
|
+
/// it, if its job waits for a request of which nothing was read (or steps aside before it),
|
|
251
|
+
/// or, for a drain, for the rest of a kept-alive head, as hyper closes such a one. A drain
|
|
252
|
+
/// spares one whose client's bytes wait unread, as hyper reads what came before it heeds the
|
|
253
|
+
/// drain. A job about to wait sees either itself (InlineConn::wait).
|
|
254
|
+
pub(super) fn shut_if_idle(&self, shut: Shut) {
|
|
255
|
+
let mut state = self.state.lock();
|
|
256
|
+
match (shut, state.waits) {
|
|
257
|
+
(_, Waits::Other) | (Shut::Quarantine, Waits::MidHead) => return,
|
|
258
|
+
(Shut::Drain, _) if state.unread() => return,
|
|
259
|
+
(Shut::Drain, _) => {}
|
|
260
|
+
(Shut::Quarantine, _) => state.ends(&self.slot),
|
|
261
|
+
}
|
|
262
|
+
state.shutdown();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/// Past the shutdown deadline: the socket is shut down unless a request is being served,
|
|
266
|
+
/// cutting one still reading a request or writing to a slow reader.
|
|
267
|
+
pub(super) fn shut_unless_serving(&self) {
|
|
268
|
+
let mut state = self.state.lock();
|
|
269
|
+
if state.current.is_none() {
|
|
270
|
+
state.ends(&self.slot);
|
|
271
|
+
state.shutdown();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/// Its worker is given up on for `why` (a crash, or the shutdown past its deadline): counted
|
|
276
|
+
/// as cut off, unless its job never looked at it or, on a crash, it waits idle; abandon
|
|
277
|
+
/// counts those once they do not go to hyper.
|
|
278
|
+
pub(super) fn giving_up(&self, why: Cut) {
|
|
279
|
+
let mut state = self.state.lock();
|
|
280
|
+
if state.taken && !(why == Cut::Crash && state.idle()) {
|
|
281
|
+
state.ends(&self.slot);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/// A write's wait (Wrote::Wait) is back; with none out, a connection let go meanwhile closes.
|
|
286
|
+
pub(crate) fn waited(&self) {
|
|
287
|
+
let mut state = self.state.lock();
|
|
288
|
+
state.lent = state.lent.saturating_sub(1);
|
|
289
|
+
if state.lent == 0 && state.released {
|
|
290
|
+
if let Some(stream) = state.stream.take() {
|
|
291
|
+
close(stream);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/// Its worker is given up on (it crashed, or the shutdown passed its deadline): no job looks
|
|
297
|
+
/// at it again. On a crash an idle one goes to hyper. Else an unanswered request gets its 500
|
|
298
|
+
/// (or its stream is cut, counted for `why`), what the socket takes now of an answer goes
|
|
299
|
+
/// out, the socket is shut down so its client reads the end, and its worker counts it no
|
|
300
|
+
/// more, once. Only its job, or the last write waiting at its end, closes the descriptor. Out
|
|
301
|
+
/// come its seat and permit.
|
|
302
|
+
pub(super) fn abandon(&self, why: Cut) -> (Option<Seat>, Option<Held>) {
|
|
303
|
+
if why == Cut::Crash {
|
|
304
|
+
if let Some(seat) = self.to_hyper_if_idle() {
|
|
305
|
+
return (Some(seat), None);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
let owed = {
|
|
309
|
+
let mut state = self.state.lock();
|
|
310
|
+
state.ends(&self.slot);
|
|
311
|
+
state
|
|
312
|
+
.current
|
|
313
|
+
.as_ref()
|
|
314
|
+
.map(|current| current.responder.clone())
|
|
315
|
+
};
|
|
316
|
+
if let Some(responder) = owed {
|
|
317
|
+
self.give_up(&responder, why);
|
|
318
|
+
}
|
|
319
|
+
let mut state = self.state.lock();
|
|
320
|
+
let _ = state.flush();
|
|
321
|
+
state.shutdown();
|
|
322
|
+
state.looked(&self.slot);
|
|
323
|
+
(state.seat.take(), state.held.take())
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/// Its worker crashed or its job ended with it idle: hyper takes it over as it is, on its
|
|
327
|
+
/// accepting runtime, handed back, so a request already sent is served and a
|
|
328
|
+
/// keep-alive client keeps it, with what is left of its header time. A job that looks again
|
|
329
|
+
/// finds it over. Out comes its seat; None, changing nothing, unless idle.
|
|
330
|
+
pub(super) fn to_hyper_if_idle(&self) -> Option<Seat> {
|
|
331
|
+
let (stream, held, seat) = {
|
|
332
|
+
let mut state = self.state.lock();
|
|
333
|
+
if !state.idle() {
|
|
334
|
+
return None;
|
|
335
|
+
}
|
|
336
|
+
state.looked(&self.slot);
|
|
337
|
+
state.waits = Waits::Other;
|
|
338
|
+
state.closing = true;
|
|
339
|
+
(state.stream.take()?, state.held.take()?, state.seat.take())
|
|
340
|
+
};
|
|
341
|
+
self.to_hyper(stream, held, Bytes::new(), true);
|
|
342
|
+
seat
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/// Hyper serves the connection from here on, on its accepting runtime, reading `prefix` (what
|
|
346
|
+
/// was read here) first; counted as handed back, or else as declined at accept. With too little read to
|
|
347
|
+
/// sniff (nothing, or part of the h2 preface), its client has what is left of its header
|
|
348
|
+
/// time.
|
|
349
|
+
pub(super) fn to_hyper(&self, stream: StdConn, held: Held, prefix: Bytes, handed_back: bool) {
|
|
350
|
+
if let Some(inline) = &self.origin.server.inline {
|
|
351
|
+
let count = match handed_back {
|
|
352
|
+
true => &inline.handbacks,
|
|
353
|
+
false => &inline.declined,
|
|
354
|
+
};
|
|
355
|
+
count.fetch_add(1, Ordering::Relaxed);
|
|
356
|
+
}
|
|
357
|
+
crate::server::serve_handed_back(
|
|
358
|
+
&self.origin,
|
|
359
|
+
stream,
|
|
360
|
+
prefix,
|
|
361
|
+
held,
|
|
362
|
+
self.deadline.left(&self.origin.server),
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
impl IoState {
|
|
368
|
+
/// Kino ends the connection (a deadline, a crash, the shutdown, a quarantine, a write nobody
|
|
369
|
+
/// waits on), counted once: not if it was ending already (an answer that closes it, a 504, an
|
|
370
|
+
/// earlier cut) or is its worker's no more.
|
|
371
|
+
pub(super) fn ends(&mut self, slot: &WorkerSlot) {
|
|
372
|
+
if !self.closing && self.seat.is_some() {
|
|
373
|
+
slot.inline.closed.fetch_add(1, Ordering::Relaxed);
|
|
374
|
+
self.closing = true;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/// Hyper could take it over as it is: open and its worker's, its job waiting for a next
|
|
379
|
+
/// request with nothing read, nothing owed, nothing left to write and no write waiting.
|
|
380
|
+
fn idle(&self) -> bool {
|
|
381
|
+
self.waits == Waits::Idle
|
|
382
|
+
&& !self.closing
|
|
383
|
+
&& self.seat.is_some()
|
|
384
|
+
&& self.stream.is_some()
|
|
385
|
+
&& self.held.is_some()
|
|
386
|
+
&& self.current.is_none()
|
|
387
|
+
&& !self.unparsed
|
|
388
|
+
&& self.out.sent()
|
|
389
|
+
&& self.lent == 0
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
pub(super) fn write(&mut self, parts: &[&[u8]]) -> std::io::Result<()> {
|
|
393
|
+
let stream = self.stream.as_ref().ok_or(ErrorKind::NotConnected)?;
|
|
394
|
+
self.out.write(stream, parts)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
pub(super) fn flush(&mut self) -> std::io::Result<bool> {
|
|
398
|
+
let stream = self.stream.as_ref().ok_or(ErrorKind::NotConnected)?;
|
|
399
|
+
self.out.flush(stream)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/// Its job looks for the next request, steps aside before it, or lets go: no longer answered.
|
|
403
|
+
pub(super) fn looked(&mut self, slot: &WorkerSlot) {
|
|
404
|
+
if self.answered {
|
|
405
|
+
self.answered = false;
|
|
406
|
+
slot.inline.answered.fetch_sub(1, Ordering::Relaxed);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/// Cut the connection short, from any thread: its job wakes and ends.
|
|
411
|
+
pub(super) fn shutdown(&mut self) {
|
|
412
|
+
if let Some(stream) = &self.stream {
|
|
413
|
+
let _ = stream.socket.shutdown(Shutdown::Both);
|
|
414
|
+
}
|
|
415
|
+
self.out.clear();
|
|
416
|
+
self.closing = true;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/// The socket takes more now: its client read some since a write found it full.
|
|
420
|
+
fn writable(&self) -> bool {
|
|
421
|
+
self.stream.as_ref().is_some_and(|stream| {
|
|
422
|
+
poll_for(stream.as_raw_fd(), libc::POLLOUT, 0)
|
|
423
|
+
.is_some_and(|revents| revents & (libc::POLLERR | libc::POLLHUP) == 0)
|
|
424
|
+
})
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/// Its client's bytes wait unread in the socket.
|
|
428
|
+
pub(super) fn unread(&self) -> bool {
|
|
429
|
+
let mut byte = [0u8];
|
|
430
|
+
self.stream
|
|
431
|
+
.as_ref()
|
|
432
|
+
.is_some_and(|stream| matches!(stream.peek(&mut byte), Ok(n) if n > 0))
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/// What a closing connection reads off its socket at most (close): a request or two its client
|
|
437
|
+
/// sent behind the last answer. Past it, the close resets the connection.
|
|
438
|
+
const DISCARD_MAX: usize = 64 * 1024;
|
|
439
|
+
|
|
440
|
+
/// The close of a connection's socket by its job or by the last write waiting on it: the FIN goes
|
|
441
|
+
/// out behind what the socket still holds of an answer, and what the client sent that nothing will
|
|
442
|
+
/// read is read off first, up to DISCARD_MAX. A close with such bytes unread resets the connection
|
|
443
|
+
/// instead (RFC 1122, 4.2.2.13), dropping what the socket held of the answer; a client may not
|
|
444
|
+
/// even take that reset, and waits on an end that never comes.
|
|
445
|
+
pub(super) fn close(stream: StdConn) {
|
|
446
|
+
let _ = stream.socket.shutdown(Shutdown::Write);
|
|
447
|
+
let mut sink = [0u8; 8 * 1024];
|
|
448
|
+
let mut left = DISCARD_MAX;
|
|
449
|
+
while left > 0 {
|
|
450
|
+
match (&stream.socket).read(&mut sink) {
|
|
451
|
+
Ok(0) => break,
|
|
452
|
+
Ok(n) => left = left.saturating_sub(n),
|
|
453
|
+
Err(e) if e.kind() == ErrorKind::Interrupted => {}
|
|
454
|
+
Err(_) => break,
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
pub(super) fn forget(slot: &WorkerSlot, io: &Arc<InlineIo>) {
|
|
460
|
+
let io = Arc::as_ptr(io);
|
|
461
|
+
slot.inline.ios.lock().retain(|listed| !listed.is(io));
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
pub(super) fn micros(span: Duration) -> u64 {
|
|
465
|
+
u64::try_from(span.as_micros()).unwrap_or(u64::MAX)
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/// A blocking write's wait (write_chunk's Wait): up to `timeout` for `fd` to take more or fail (a
|
|
469
|
+
/// closed descriptor reads ready).
|
|
470
|
+
pub fn await_writable(fd: RawFd, timeout: Duration) {
|
|
471
|
+
let ms = libc::c_int::try_from(timeout.as_millis()).unwrap_or(libc::c_int::MAX);
|
|
472
|
+
poll_for(fd, libc::POLLOUT, ms);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/// poll(2) for `events` on `fd` up to `ms` (0: no wait): the events seen, None on timeout or
|
|
476
|
+
/// failure.
|
|
477
|
+
pub(super) fn poll_for(fd: RawFd, events: libc::c_short, ms: libc::c_int) -> Option<libc::c_short> {
|
|
478
|
+
let mut ready = libc::pollfd {
|
|
479
|
+
fd,
|
|
480
|
+
events,
|
|
481
|
+
revents: 0,
|
|
482
|
+
};
|
|
483
|
+
// SAFETY: `ready` is one pollfd borrowed for the call alone, and the count says one; poll(2)
|
|
484
|
+
// reports a bad `fd` in revents rather than touch it.
|
|
485
|
+
let polled = unsafe { libc::poll(&mut ready, 1, ms) };
|
|
486
|
+
(polled == 1).then_some(ready.revents)
|
|
487
|
+
}
|