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
data/ext/kino/src/pin.rs
CHANGED
|
@@ -154,8 +154,8 @@ impl PinSlab {
|
|
|
154
154
|
/// through it. `Kino::Server` holds one for its response-buffer slab for
|
|
155
155
|
/// the server's lifetime (surviving worker-ractor crashes); env_strings
|
|
156
156
|
/// registers one for its cache slab as an immortal root at init.
|
|
157
|
-
|
|
158
|
-
|
|
157
|
+
/// Wrapped in typed_data.rs, whose promise its drop keeps: it lets go of the slab, reading none
|
|
158
|
+
/// of the strings.
|
|
159
159
|
pub struct PinKeeper(pub Arc<PinSlab>);
|
|
160
160
|
|
|
161
161
|
impl magnus::DataTypeFunctions for PinKeeper {
|
|
@@ -215,7 +215,7 @@ pub fn pinned_bytes(slab: &Arc<PinSlab>, body: RString) -> Option<Bytes> {
|
|
|
215
215
|
// rules out an embedded buffer, so ptr is a stable heap allocation.
|
|
216
216
|
let (ptr, len) = unsafe {
|
|
217
217
|
(
|
|
218
|
-
rb_sys::macros::RSTRING_PTR(tmp)
|
|
218
|
+
rb_sys::macros::RSTRING_PTR(tmp).cast::<u8>(),
|
|
219
219
|
rb_sys::macros::RSTRING_LEN(tmp) as usize,
|
|
220
220
|
)
|
|
221
221
|
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//! The worker-side bridge: batched request intake and the fused
|
|
2
|
+
//! respond-and-take call, which make a request's take, env and answer
|
|
3
|
+
//! about one FFI crossing.
|
|
4
|
+
//!
|
|
5
|
+
//! Blocking discipline (everywhere in this crate): bounded `recv_timeout`
|
|
6
|
+
//! ticks + an AtomicBool interrupt flag. No flume::Selector: it loses
|
|
7
|
+
//! wakeups under churn, observed as workers going permanently deaf to a
|
|
8
|
+
//! non-empty queue after ~100k requests.
|
|
9
|
+
|
|
10
|
+
use std::time::Duration;
|
|
11
|
+
|
|
12
|
+
use crate::inline::InlineConn;
|
|
13
|
+
use crate::registry::BoxedCtx;
|
|
14
|
+
use crate::ws::WsEvent;
|
|
15
|
+
|
|
16
|
+
mod take;
|
|
17
|
+
#[cfg(test)]
|
|
18
|
+
pub(crate) mod testing;
|
|
19
|
+
#[cfg(test)]
|
|
20
|
+
mod tests;
|
|
21
|
+
mod worker;
|
|
22
|
+
|
|
23
|
+
#[cfg(test)]
|
|
24
|
+
pub(crate) use take::{Room, Take, Taker};
|
|
25
|
+
pub(crate) use worker::admit_inline;
|
|
26
|
+
#[cfg(test)]
|
|
27
|
+
pub(crate) use worker::enlist_inline;
|
|
28
|
+
pub use worker::{
|
|
29
|
+
lingering_loops, respond_and_take, respond_and_take_one, wake_worker, worker, Worker,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
pub const TICK: Duration = Duration::from_millis(50);
|
|
33
|
+
|
|
34
|
+
pub enum Item {
|
|
35
|
+
Ctx(BoxedCtx),
|
|
36
|
+
Ws(WsEvent),
|
|
37
|
+
Wake,
|
|
38
|
+
Conn(Box<InlineConn>),
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/// None = shutdown (queue closed, or slot halted past the deadline); the caller can't tell
|
|
42
|
+
/// the difference and doesn't need to.
|
|
43
|
+
pub(crate) type Taken = Option<Item>;
|
|
44
|
+
|
|
45
|
+
/// One bounded wait on `rx`, as a park's attempt reports it: None to wait on, Some(None) once
|
|
46
|
+
/// the channel is closed and empty.
|
|
47
|
+
#[inline]
|
|
48
|
+
pub(crate) fn recv_tick<T>(rx: &flume::Receiver<T>) -> Option<Option<T>> {
|
|
49
|
+
match rx.recv_timeout(TICK) {
|
|
50
|
+
Ok(found) => Some(Some(found)),
|
|
51
|
+
Err(flume::RecvTimeoutError::Timeout) => None,
|
|
52
|
+
Err(flume::RecvTimeoutError::Disconnected) => Some(None),
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
//! A worker's takes. What is ready goes first: its sockets' events and its wake, its lane, a
|
|
2
|
+
//! sibling's lane. With nothing ready a classic worker parks (Taker::take), and a worker_loop
|
|
3
|
+
//! arms its doorbell and leaves the wait to the app's scheduler (Taker::nonblock).
|
|
4
|
+
|
|
5
|
+
use std::sync::atomic::Ordering;
|
|
6
|
+
use std::sync::Arc;
|
|
7
|
+
|
|
8
|
+
use super::{recv_tick, Item, Taken, TICK};
|
|
9
|
+
use crate::gvl::{self, Back, Parker};
|
|
10
|
+
use crate::registry::{LaneItem, ServerInner};
|
|
11
|
+
use crate::slot::{WorkerSlot, HOLDING_ITEM};
|
|
12
|
+
|
|
13
|
+
/// Socket events a worker takes in a row while a request or a wake may wait;
|
|
14
|
+
/// then its lane gets a look and a due wake its turn. A wake counts as a whole
|
|
15
|
+
/// run, so wakes and requests alternate.
|
|
16
|
+
pub(super) const OWN_RUN: u32 = 8;
|
|
17
|
+
/// Park::own_run past a full run: a wake filled it (WAKE_RUN), or a due wake goes next (WAKE_TURN).
|
|
18
|
+
const WAKE_RUN: u32 = OWN_RUN + 1;
|
|
19
|
+
const WAKE_TURN: u32 = OWN_RUN + 2;
|
|
20
|
+
|
|
21
|
+
/// What a take that does not wait found.
|
|
22
|
+
pub(crate) enum Take {
|
|
23
|
+
Item(Item),
|
|
24
|
+
/// The worker's loop ends: its lane closed and its inbox is empty, or it retired with
|
|
25
|
+
/// nothing left.
|
|
26
|
+
Closed,
|
|
27
|
+
Nothing,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
impl Take {
|
|
31
|
+
fn or_else(self, next: impl FnOnce() -> Take) -> Take {
|
|
32
|
+
match self {
|
|
33
|
+
Take::Nothing => next(),
|
|
34
|
+
found => found,
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
fn item(&self) -> Option<&Item> {
|
|
39
|
+
match self {
|
|
40
|
+
Take::Item(item) => Some(item),
|
|
41
|
+
Take::Closed | Take::Nothing => None,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// As a wait's attempt reports it: None to wait on, Some(None) at the loop's end.
|
|
46
|
+
fn found(self) -> Option<Taken> {
|
|
47
|
+
match self {
|
|
48
|
+
Take::Item(item) => Some(Some(item)),
|
|
49
|
+
Take::Closed => Some(None),
|
|
50
|
+
Take::Nothing => None,
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
impl From<Option<Item>> for Take {
|
|
56
|
+
fn from(item: Option<Item>) -> Take {
|
|
57
|
+
item.map_or(Take::Nothing, Take::Item)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// Whether a lane's own worker idles on it and takes what it holds within moments. A parked
|
|
62
|
+
/// classic worker blocks on its lane. A parked worker_loop one waits on its doorbell, unless the
|
|
63
|
+
/// wait is a tick overdue: its thread is away on a job's fiber, and rings go unanswered. A
|
|
64
|
+
/// retired or crashed worker's lane is never left to it.
|
|
65
|
+
fn owner_waits(owner: &WorkerSlot, now: &mut Option<u64>) -> bool {
|
|
66
|
+
if !owner.park.is_parked() || owner.life.seems_retired() {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
if !owner.lane.is_worker_loop() {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
let now = *now.get_or_insert_with(crate::mono::mono_us);
|
|
73
|
+
let due = owner.load.last_started();
|
|
74
|
+
now < due.saturating_add(TICK.as_micros() as u64)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/// What a worker_loop take has room for (Kino::Worker::Loop#take).
|
|
78
|
+
/// A placed connection is taken whatever the room: one placed before the worker filled up sends
|
|
79
|
+
/// its first request to hyper, where queue_timeout bounds its wait.
|
|
80
|
+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
81
|
+
pub(crate) enum Room {
|
|
82
|
+
All,
|
|
83
|
+
Events,
|
|
84
|
+
Requests,
|
|
85
|
+
/// Room for neither: placed connections alone.
|
|
86
|
+
Connections,
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// A worker's side of the queues: its slot, and through the server its siblings' lanes or the
|
|
90
|
+
/// shared queue.
|
|
91
|
+
#[derive(Clone, Copy)]
|
|
92
|
+
pub(crate) struct Taker<'a> {
|
|
93
|
+
server: &'a ServerInner,
|
|
94
|
+
slot: &'a Arc<WorkerSlot>,
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
impl<'a> Taker<'a> {
|
|
98
|
+
pub(crate) fn new(server: &'a ServerInner, slot: &'a Arc<WorkerSlot>) -> Self {
|
|
99
|
+
Taker { server, slot }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/// A connection placed on the worker to serve inline, apart from its lane: no sibling's
|
|
103
|
+
/// steal meets one.
|
|
104
|
+
fn placed(self) -> Option<Item> {
|
|
105
|
+
let conn = self.slot.lane.placed_rx.as_ref()?.try_recv().ok()?;
|
|
106
|
+
Some(Item::Conn(conn))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// What the worker owes its own sockets and wake hook ahead of any request: the
|
|
110
|
+
/// next inbox event, else a due wake. None once a full run owes the lane a look.
|
|
111
|
+
fn own_item(self) -> Option<Item> {
|
|
112
|
+
let park = &self.slot.park;
|
|
113
|
+
let mut run = park.own_run.load(Ordering::Relaxed);
|
|
114
|
+
if run == WAKE_TURN {
|
|
115
|
+
if let Some(wake) = self.wake() {
|
|
116
|
+
return Some(wake);
|
|
117
|
+
}
|
|
118
|
+
run = 0;
|
|
119
|
+
park.own_run.store(run, Ordering::Relaxed);
|
|
120
|
+
}
|
|
121
|
+
if run >= OWN_RUN {
|
|
122
|
+
return None;
|
|
123
|
+
}
|
|
124
|
+
match self.slot.lane.ws_rx.as_ref()?.try_recv() {
|
|
125
|
+
Ok(event) => {
|
|
126
|
+
park.own_run.store(run + 1, Ordering::Relaxed);
|
|
127
|
+
Some(Item::Ws(event))
|
|
128
|
+
}
|
|
129
|
+
Err(_) => self.wake(),
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/// The wake hook's turn, if due; it fills the run.
|
|
134
|
+
fn wake(self) -> Option<Item> {
|
|
135
|
+
if !self.slot.lane.take_wake() {
|
|
136
|
+
return None;
|
|
137
|
+
}
|
|
138
|
+
self.slot.park.own_run.store(WAKE_RUN, Ordering::Relaxed);
|
|
139
|
+
Some(Item::Wake)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/// The run starts over (a full run hands a due wake the next turn); true if it deferred anything.
|
|
143
|
+
fn lane_looked(self) -> bool {
|
|
144
|
+
let own_run = &self.slot.park.own_run;
|
|
145
|
+
let run = own_run.load(Ordering::Relaxed);
|
|
146
|
+
if run != 0 {
|
|
147
|
+
let next = if run == OWN_RUN { WAKE_TURN } else { 0 };
|
|
148
|
+
own_run.store(next, Ordering::Relaxed);
|
|
149
|
+
}
|
|
150
|
+
run >= OWN_RUN
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/// A doorbell clears the pending flag before the inbox and the wake flag are
|
|
154
|
+
/// read, so what arrives meanwhile rings again.
|
|
155
|
+
fn lane_item(self, item: LaneItem) -> Option<Item> {
|
|
156
|
+
match item {
|
|
157
|
+
LaneItem::Request(ctx) => {
|
|
158
|
+
self.lane_looked();
|
|
159
|
+
Some(Item::Ctx(ctx))
|
|
160
|
+
}
|
|
161
|
+
LaneItem::Bell => {
|
|
162
|
+
self.slot.lane.took_bell();
|
|
163
|
+
self.own_item()
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/// What a take does when its bounded wait times out: keep waiting (Nothing),
|
|
169
|
+
/// or end the loop because the pool scaler retired this slot. A retired
|
|
170
|
+
/// lane worker first empties its own lane, one request per timeout: the
|
|
171
|
+
/// dispatcher stopped feeding it when the flag went up (see
|
|
172
|
+
/// WorkerSlot::retire_at for the ordering), so whatever is
|
|
173
|
+
/// still there is the last of it, and nothing already assigned is
|
|
174
|
+
/// orphaned. Only reached with the queue momentarily empty, so the fast
|
|
175
|
+
/// path pays nothing for it. A retired lane worker then waits for its
|
|
176
|
+
/// sockets' closes (their handlers live on its thread), and any worker
|
|
177
|
+
/// stays while its hijacked connections are open.
|
|
178
|
+
fn after_timeout(self) -> Take {
|
|
179
|
+
let slot = self.slot;
|
|
180
|
+
if !slot.life.retired() || slot.hijacked.load(Ordering::SeqCst) > 0 {
|
|
181
|
+
return Take::Nothing;
|
|
182
|
+
}
|
|
183
|
+
// Placed before the retirement: its own to serve (or close).
|
|
184
|
+
if let Some(conn) = self.placed() {
|
|
185
|
+
return Take::Item(conn);
|
|
186
|
+
}
|
|
187
|
+
let Some(lane_rx) = slot.lane.rx.as_ref() else {
|
|
188
|
+
return Take::Closed;
|
|
189
|
+
};
|
|
190
|
+
while let Ok(item) = lane_rx.try_recv() {
|
|
191
|
+
if let Some(taken) = self.lane_item(item) {
|
|
192
|
+
return Take::Item(taken);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if slot.sockets.open() > 0 {
|
|
196
|
+
return Take::Nothing;
|
|
197
|
+
}
|
|
198
|
+
// A socket's close reaches the inbox before it stops counting.
|
|
199
|
+
self.own_item().map_or(Take::Closed, Take::Item)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/// Requests only: a sibling's doorbell goes back to its owner, whose events stay with it.
|
|
203
|
+
/// A worker that `waited` (a tick woke it, or a ring for nothing) leaves a lane to its own
|
|
204
|
+
/// idle worker, which the send woke: taking from it would move one client's run of requests
|
|
205
|
+
/// to a cold worker whenever a tick falls between a send and its take.
|
|
206
|
+
fn steal(self, waited: bool) -> Option<Item> {
|
|
207
|
+
if self.slot.life.seems_retired() {
|
|
208
|
+
return None;
|
|
209
|
+
}
|
|
210
|
+
let slots = self.server.slots.read();
|
|
211
|
+
let mut now = None;
|
|
212
|
+
for other in slots.iter() {
|
|
213
|
+
if Arc::ptr_eq(other, self.slot) {
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if waited && owner_waits(other, &mut now) {
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if let Some(rx) = other.lane.rx.as_ref() {
|
|
220
|
+
match rx.try_recv() {
|
|
221
|
+
Ok(LaneItem::Request(ctx)) => return Some(Item::Ctx(ctx)),
|
|
222
|
+
Ok(LaneItem::Bell) => {
|
|
223
|
+
other.lane.took_bell();
|
|
224
|
+
other.notify();
|
|
225
|
+
}
|
|
226
|
+
Err(_) => {}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
None
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/// Inbox and wake (after the lane, once a full run owes it a look), a connection placed on
|
|
234
|
+
/// it, the lane, then a steal. Closed once the lane is closed and the inbox empty.
|
|
235
|
+
pub(crate) fn ready(self) -> Take {
|
|
236
|
+
self.ready_after(false)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/// `ready`, for a worker that `waited` before this look or comes straight from its work.
|
|
240
|
+
fn ready_after(self, waited: bool) -> Take {
|
|
241
|
+
let lane_rx = self
|
|
242
|
+
.slot
|
|
243
|
+
.lane
|
|
244
|
+
.rx
|
|
245
|
+
.as_ref()
|
|
246
|
+
.expect("a lane take without a lane");
|
|
247
|
+
if let Some(item) = self.own_item().or_else(|| self.placed()) {
|
|
248
|
+
return Take::Item(item);
|
|
249
|
+
}
|
|
250
|
+
loop {
|
|
251
|
+
match lane_rx.try_recv() {
|
|
252
|
+
Ok(item) => {
|
|
253
|
+
if let Some(taken) = self.lane_item(item) {
|
|
254
|
+
return Take::Item(taken);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
// Placed as the lane closed: still taken, as its send came first under the lane
|
|
258
|
+
// lock.
|
|
259
|
+
Err(flume::TryRecvError::Disconnected) => {
|
|
260
|
+
return Take::from(self.placed()).or_else(|| self.closing_item())
|
|
261
|
+
}
|
|
262
|
+
Err(flume::TryRecvError::Empty) => break,
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if self.lane_looked() {
|
|
266
|
+
if let Some(item) = self.own_item() {
|
|
267
|
+
return Take::Item(item);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
self.steal(waited).into()
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/// After shutdown closed the lane: the rest of the inbox past the run cap, so every
|
|
274
|
+
/// on_close runs before the worker leaves. No wake: a self-waking hook would never let go.
|
|
275
|
+
fn closing_item(self) -> Take {
|
|
276
|
+
let inbox = self.slot.lane.ws_rx.as_ref();
|
|
277
|
+
let event = inbox.and_then(|inbox| inbox.try_recv().ok());
|
|
278
|
+
event.map_or(Take::Closed, |event| Take::Item(Item::Ws(event)))
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/// Inbox and wake first (a doorbell that met a full lane is never sent), then a retired
|
|
282
|
+
/// worker's exit, then a steal, so a backlog behind a slow sibling cannot outlive a tick.
|
|
283
|
+
pub(crate) fn tick(self) -> Take {
|
|
284
|
+
Take::from(self.own_item())
|
|
285
|
+
.or_else(|| self.after_timeout())
|
|
286
|
+
.or_else(|| self.steal(true).into())
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/// The next item, waiting through `parker` (the GVL released; in place in tests). What is
|
|
290
|
+
/// ready goes first with the GVL held, sparing two scheduler round-trips under load;
|
|
291
|
+
/// a lane worker parks with its flag raised, so the dispatcher avoids it.
|
|
292
|
+
/// A VM interrupt only wakes the park, or raises out of it.
|
|
293
|
+
pub(super) fn take<P: Parker>(self, parker: &mut P) -> Result<Taken, P::Error> {
|
|
294
|
+
let (server, slot) = (self.server, self.slot);
|
|
295
|
+
if let Some(taken) = self.now().found() {
|
|
296
|
+
return Ok(taken);
|
|
297
|
+
}
|
|
298
|
+
let Some(lane) = &slot.lane.rx else {
|
|
299
|
+
return self.park_shared(parker);
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
// Park. The flag-then-recheck order closes the race with a dispatcher
|
|
303
|
+
// that read parked=false just before we set it: anything it sent lands
|
|
304
|
+
// in the lane, and recv_timeout checks the queue before sleeping.
|
|
305
|
+
slot.park.park(server);
|
|
306
|
+
let wait = || match lane.recv_timeout(TICK) {
|
|
307
|
+
Ok(item) => self.lane_item(item).map(Some),
|
|
308
|
+
Err(flume::RecvTimeoutError::Timeout) => self.tick().found(),
|
|
309
|
+
Err(flume::RecvTimeoutError::Disconnected) => self.closing_item().found(),
|
|
310
|
+
};
|
|
311
|
+
let put_back = |taken| self.put_back_lane(taken);
|
|
312
|
+
let taken = gvl::park(parker, slot.life.halted(), wait, put_back);
|
|
313
|
+
crate::shared_gvl::got_back(server, slot);
|
|
314
|
+
// Unpark before propagating any panic, or the dispatcher shuns this
|
|
315
|
+
// lane for good.
|
|
316
|
+
slot.park.unpark();
|
|
317
|
+
let taken = taken?.flatten();
|
|
318
|
+
slot.park.took(taken.as_ref());
|
|
319
|
+
Ok(taken)
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/// What is there without a wait, the GVL held: `ready` for a lane worker, else the shared
|
|
323
|
+
/// queue's next request, which a retired worker leaves.
|
|
324
|
+
pub(super) fn now(self) -> Take {
|
|
325
|
+
if self.slot.lane.rx.is_some() {
|
|
326
|
+
let take = self.ready();
|
|
327
|
+
if !matches!(take, Take::Nothing) {
|
|
328
|
+
self.slot.park.took(take.item());
|
|
329
|
+
}
|
|
330
|
+
return take;
|
|
331
|
+
}
|
|
332
|
+
if self.slot.life.seems_retired() {
|
|
333
|
+
return Take::Nothing;
|
|
334
|
+
}
|
|
335
|
+
match self.server.req_rx.try_recv() {
|
|
336
|
+
Ok(ctx) => Take::Item(Item::Ctx(ctx)),
|
|
337
|
+
Err(flume::TryRecvError::Disconnected) => Take::Closed,
|
|
338
|
+
Err(flume::TryRecvError::Empty) => Take::Nothing,
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/// A wait on the shared queue. A retired worker takes nothing new, and leaves as
|
|
343
|
+
/// after_timeout says, or at close.
|
|
344
|
+
fn park_shared<P: Parker>(self, parker: &mut P) -> Result<Taken, P::Error> {
|
|
345
|
+
let (queue, life) = (&self.server.req_rx, &self.slot.life);
|
|
346
|
+
let retired = || life.seems_retired();
|
|
347
|
+
let wait = || {
|
|
348
|
+
if !retired() {
|
|
349
|
+
return recv_tick(queue).map(|ctx| ctx.map(Item::Ctx));
|
|
350
|
+
}
|
|
351
|
+
if queue.is_disconnected() {
|
|
352
|
+
return Some(None);
|
|
353
|
+
}
|
|
354
|
+
self.after_timeout().found().or_else(|| {
|
|
355
|
+
std::thread::sleep(TICK);
|
|
356
|
+
None
|
|
357
|
+
})
|
|
358
|
+
};
|
|
359
|
+
let put_back = |taken| self.put_back_shared(taken);
|
|
360
|
+
let taken = gvl::park(parker, life.halted(), wait, put_back);
|
|
361
|
+
crate::shared_gvl::got_back(self.server, self.slot);
|
|
362
|
+
Ok(taken?.flatten())
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/// A request taken as a Thread#raise came goes back at the queue's end, for whichever worker
|
|
366
|
+
/// takes next; held when the queue is closed or full, so the raise answers it at once.
|
|
367
|
+
fn put_back_shared(self, taken: Taken) -> Back<Taken> {
|
|
368
|
+
let Some(Item::Ctx(ctx)) = taken else {
|
|
369
|
+
return Back::HandedOver(taken);
|
|
370
|
+
};
|
|
371
|
+
let Some(tx) = self.server.req_tx.lock().clone() else {
|
|
372
|
+
return Back::Held(Some(Item::Ctx(ctx)));
|
|
373
|
+
};
|
|
374
|
+
match tx.try_send(ctx) {
|
|
375
|
+
Ok(()) => Back::Waits,
|
|
376
|
+
Err(refused) => Back::Held(Some(Item::Ctx(refused.into_inner()))),
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/// A request taken as a Thread#raise came goes back at its lane's end for the next take (a
|
|
381
|
+
/// crash answers it with the rest of the lane), held when there is no room; a wake is due
|
|
382
|
+
/// again. A socket event is handed over as taken, so its socket's events stay in order: the
|
|
383
|
+
/// raise fires before its callback, losing only that event.
|
|
384
|
+
fn put_back_lane(self, taken: Taken) -> Back<Taken> {
|
|
385
|
+
match taken {
|
|
386
|
+
Some(Item::Ctx(ctx)) => match self.slot.send_lane(LaneItem::Request(ctx)) {
|
|
387
|
+
Ok(()) => Back::Waits,
|
|
388
|
+
Err(refused) => match refused.into_inner() {
|
|
389
|
+
LaneItem::Request(ctx) => Back::Held(Some(Item::Ctx(ctx))),
|
|
390
|
+
LaneItem::Bell => unreachable!("a request went in"),
|
|
391
|
+
},
|
|
392
|
+
},
|
|
393
|
+
Some(Item::Wake) if self.slot.wake() => Back::Waits,
|
|
394
|
+
Some(Item::Wake) => Back::Held(Some(Item::Wake)),
|
|
395
|
+
other => {
|
|
396
|
+
debug_assert!(
|
|
397
|
+
!matches!(other, Some(Item::Conn(_))),
|
|
398
|
+
"a park took a placed connection"
|
|
399
|
+
);
|
|
400
|
+
Back::HandedOver(other)
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/// worker_loop's take, of what its loop has `room` for. Nothing: the doorbell is armed, and
|
|
406
|
+
/// the loop waits on it; the ticker rings it meanwhile, so this is its lane tick too.
|
|
407
|
+
/// - All: what `ready` finds, else a retired worker's exit. Parked while it waits.
|
|
408
|
+
/// - Requests (its backlog of events full): its lane's requests; the inbox waits until the
|
|
409
|
+
/// backlog drains, pushing back on its sockets. Never Closed: the full take reports a
|
|
410
|
+
/// closed lane, after what the inbox still holds.
|
|
411
|
+
/// - Events (every job slot busy): socket events and the wake hook, never Closed. A full
|
|
412
|
+
/// worker is not idle.
|
|
413
|
+
pub(crate) fn nonblock(self, room: Room) -> Take {
|
|
414
|
+
// Parked: it comes from a wait on its doorbell, not from a job.
|
|
415
|
+
let waited = self.slot.park.is_parked();
|
|
416
|
+
let all = |taker: Self| taker.ready_after(waited).or_else(|| taker.after_timeout());
|
|
417
|
+
match room {
|
|
418
|
+
Room::All => self.take_with_room(all, true),
|
|
419
|
+
Room::Requests => {
|
|
420
|
+
let requests = |taker: Self| taker.placed().or_else(|| taker.lane_request());
|
|
421
|
+
self.take_with_room(|taker| requests(taker).into(), true)
|
|
422
|
+
}
|
|
423
|
+
Room::Events => {
|
|
424
|
+
let events = |taker: Self| taker.own_ready().or_else(|| taker.placed());
|
|
425
|
+
self.take_with_room(|taker| events(taker).into(), false)
|
|
426
|
+
}
|
|
427
|
+
Room::Connections => self.take_placed(),
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
fn doorbell(self) -> &'a crate::doorbell::Doorbell {
|
|
432
|
+
let doorbell = self.slot.lane.doorbell.as_ref();
|
|
433
|
+
doorbell.expect("a worker_loop take without a doorbell")
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/// One worker_loop take: `look`, and if it finds nothing, arm and look again, as a send just
|
|
437
|
+
/// before the arm found the doorbell disarmed and did not ring. Only a worker with a job slot
|
|
438
|
+
/// free is `idle`: it prunes its in-flight list (what still counts keeps dispatch away from
|
|
439
|
+
/// a full one) and parks while it waits.
|
|
440
|
+
fn take_with_room(self, look: impl Fn(Self) -> Take, idle: bool) -> Take {
|
|
441
|
+
let (server, slot, doorbell) = (self.server, self.slot, self.doorbell());
|
|
442
|
+
let now = slot.load.back_in_loop();
|
|
443
|
+
if idle && slot.load.keep_open() {
|
|
444
|
+
server.lane_room.notify();
|
|
445
|
+
}
|
|
446
|
+
let found = look(self).or_else(|| {
|
|
447
|
+
if idle {
|
|
448
|
+
slot.park.park(server);
|
|
449
|
+
}
|
|
450
|
+
doorbell.arm();
|
|
451
|
+
look(self)
|
|
452
|
+
});
|
|
453
|
+
match &found {
|
|
454
|
+
Take::Nothing => {
|
|
455
|
+
slot.load.waits_on_doorbell(now);
|
|
456
|
+
return found;
|
|
457
|
+
}
|
|
458
|
+
Take::Item(_) => slot.load.hold(HOLDING_ITEM),
|
|
459
|
+
Take::Closed => slot.load.loop_ended(),
|
|
460
|
+
}
|
|
461
|
+
match idle {
|
|
462
|
+
true => slot.park.awake(doorbell),
|
|
463
|
+
false => doorbell.disarm(),
|
|
464
|
+
}
|
|
465
|
+
slot.park.took(found.item());
|
|
466
|
+
found
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/// Requests alone: a doorbell found in the lane is dropped, the inbox read once the backlog
|
|
470
|
+
/// drains.
|
|
471
|
+
fn lane_request(self) -> Option<Item> {
|
|
472
|
+
let lane_rx = self.slot.lane.rx.as_ref().expect("a take without lane");
|
|
473
|
+
while let Ok(item) = lane_rx.try_recv() {
|
|
474
|
+
match item {
|
|
475
|
+
LaneItem::Request(ctx) => {
|
|
476
|
+
self.lane_looked();
|
|
477
|
+
return Some(Item::Ctx(ctx));
|
|
478
|
+
}
|
|
479
|
+
LaneItem::Bell => self.slot.lane.took_bell(),
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
None
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/// Room::Connections: it neither arms the doorbell nor parks the slot.
|
|
486
|
+
fn take_placed(self) -> Take {
|
|
487
|
+
let slot = self.slot;
|
|
488
|
+
let now = slot.load.back_in_loop();
|
|
489
|
+
let Some(conn) = self.placed() else {
|
|
490
|
+
slot.load.waits_on_doorbell(now);
|
|
491
|
+
return Take::Nothing;
|
|
492
|
+
};
|
|
493
|
+
slot.load.hold(HOLDING_ITEM);
|
|
494
|
+
slot.park.took(Some(&conn));
|
|
495
|
+
Take::Item(conn)
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/// With the lane out of play, a full run's look at it is moot: the run restarts, a due wake first.
|
|
499
|
+
fn own_ready(self) -> Option<Item> {
|
|
500
|
+
self.own_item()
|
|
501
|
+
.or_else(|| self.lane_looked().then(|| self.own_item()).flatten())
|
|
502
|
+
}
|
|
503
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
//! Test helpers for the takes: a worker's takes as its loop makes them,
|
|
2
|
+
//! and the dispatcher's and Ruby's parts.
|
|
3
|
+
|
|
4
|
+
use std::sync::Arc;
|
|
5
|
+
|
|
6
|
+
use super::worker::enlist;
|
|
7
|
+
use super::{Item, Room, Take, Taker, Worker};
|
|
8
|
+
use crate::registry::{BoxedCtx, ServerInner};
|
|
9
|
+
use crate::slot::WorkerSlot;
|
|
10
|
+
use crate::ws::WsEvent;
|
|
11
|
+
|
|
12
|
+
/// What a take found, by name.
|
|
13
|
+
pub fn kind(take: &Take) -> &'static str {
|
|
14
|
+
match take {
|
|
15
|
+
Take::Item(Item::Ctx(_)) => "request",
|
|
16
|
+
Take::Item(Item::Ws(_)) => "event",
|
|
17
|
+
Take::Item(Item::Wake) => "wake",
|
|
18
|
+
Take::Item(Item::Conn(_)) => "connection",
|
|
19
|
+
Take::Closed => "the end",
|
|
20
|
+
Take::Nothing => "nothing",
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/// What `slot`'s worker finds ready, by name (queue::testing::kind).
|
|
25
|
+
pub fn ready(server: &ServerInner, slot: &Arc<WorkerSlot>) -> &'static str {
|
|
26
|
+
kind(&Taker::new(server, slot).ready())
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// What `slot`'s parked worker finds at a tick, by name.
|
|
30
|
+
pub fn tick(server: &ServerInner, slot: &Arc<WorkerSlot>) -> &'static str {
|
|
31
|
+
kind(&Taker::new(server, slot).tick())
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/// What `slot`'s worker_loop worker takes with `room`, by name; "nothing" leaves it armed.
|
|
35
|
+
pub fn nonblock(server: &ServerInner, slot: &Arc<WorkerSlot>, room: Room) -> &'static str {
|
|
36
|
+
kind(&Taker::new(server, slot).nonblock(room))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/// The request a take found; panics naming what it found instead.
|
|
40
|
+
pub fn expect_request(take: Take) -> BoxedCtx {
|
|
41
|
+
match take {
|
|
42
|
+
Take::Item(Item::Ctx(ctx)) => ctx,
|
|
43
|
+
other => panic!("expected a request, took {}", kind(&other)),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// Whether the doorbell of `worker` (its handle) rang, as a scheduler
|
|
48
|
+
/// waiting on its ready_fd sees it; the ring is taken, as woke takes it.
|
|
49
|
+
pub fn rung(worker: &Worker) -> bool {
|
|
50
|
+
rings(Worker::ready_fd(worker).expect("a worker_loop worker")) > 0
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// How many times the doorbell waited on through `fd` rang since it
|
|
54
|
+
/// was last emptied, which this empties. The caller keeps `fd` open.
|
|
55
|
+
pub fn rings(fd: std::os::fd::RawFd) -> usize {
|
|
56
|
+
use std::io::Read;
|
|
57
|
+
use std::os::fd::BorrowedFd;
|
|
58
|
+
// SAFETY: the caller keeps the descriptor open; reading goes
|
|
59
|
+
// through a duplicate, closed on return.
|
|
60
|
+
let fd = unsafe { BorrowedFd::borrow_raw(fd) }
|
|
61
|
+
.try_clone_to_owned()
|
|
62
|
+
.expect("a duplicate of the doorbell");
|
|
63
|
+
let mut rings = [0u8; 64];
|
|
64
|
+
std::os::unix::net::UnixStream::from(fd)
|
|
65
|
+
.read(&mut rings)
|
|
66
|
+
.unwrap_or(0)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/// Slot `index` and the handle its worker holds from boot on, as
|
|
70
|
+
/// Kino::Native.worker makes it.
|
|
71
|
+
pub fn boot(server: &Arc<ServerInner>, index: usize) -> (Arc<WorkerSlot>, Worker) {
|
|
72
|
+
let slot = crate::testing::slot(server, index);
|
|
73
|
+
let worker = Worker::new(server, index, slot.clone());
|
|
74
|
+
(slot, worker)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/// A socket's client sends message `n` (its text is the number); it
|
|
78
|
+
/// lands in `slot`'s inbox, as the socket's reader puts it there.
|
|
79
|
+
pub fn socket_sends(slot: &WorkerSlot, n: usize) {
|
|
80
|
+
let event = WsEvent::Message {
|
|
81
|
+
id: 1,
|
|
82
|
+
data: bytes::Bytes::from(n.to_string()),
|
|
83
|
+
text: true,
|
|
84
|
+
};
|
|
85
|
+
let inbox = slot.lane.ws_tx.as_ref().expect("inbox");
|
|
86
|
+
inbox.try_send(event).expect("inbox has room");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// The next request `slot`'s worker_loop takes, admitted as the
|
|
90
|
+
/// take_nonblock native admits it (the env aside).
|
|
91
|
+
pub fn admit_next(server: &ServerInner, slot: &Arc<WorkerSlot>) -> BoxedCtx {
|
|
92
|
+
admitted(server, slot, Taker::new(server, slot).nonblock(Room::All))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/// The next request `slot`'s classic lane worker takes without
|
|
96
|
+
/// parking (its lane, then a steal), admitted as take_one admits it
|
|
97
|
+
/// (the env aside).
|
|
98
|
+
pub fn take_admitted(server: &ServerInner, slot: &Arc<WorkerSlot>) -> BoxedCtx {
|
|
99
|
+
admitted(server, slot, Taker::new(server, slot).ready())
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/// The request a take found, admitted by `slot`.
|
|
103
|
+
pub fn admitted(server: &ServerInner, slot: &Arc<WorkerSlot>, took: Take) -> BoxedCtx {
|
|
104
|
+
let mut ctx = expect_request(took);
|
|
105
|
+
enlist(server, slot, &mut ctx);
|
|
106
|
+
ctx
|
|
107
|
+
}
|