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,138 @@
|
|
|
1
|
+
//! A slot's lane: the queue the dispatcher feeds, its worker's inbox of socket events, and
|
|
2
|
+
//! the bell and wake flags.
|
|
3
|
+
|
|
4
|
+
use std::sync::atomic::{AtomicBool, Ordering};
|
|
5
|
+
|
|
6
|
+
use parking_lot::Mutex;
|
|
7
|
+
|
|
8
|
+
use super::LANE_DEPTH;
|
|
9
|
+
use crate::doorbell::Doorbell;
|
|
10
|
+
use crate::registry::LaneItem;
|
|
11
|
+
use crate::ws::WsEvent;
|
|
12
|
+
|
|
13
|
+
/// Lane mode only: the queue the dispatcher feeds, the sockets' events in order, and
|
|
14
|
+
/// whether a doorbell already sits in the lane and a wake is due (wakes coalesce).
|
|
15
|
+
#[derive(Default)]
|
|
16
|
+
pub struct Lane {
|
|
17
|
+
pub tx: LaneTx,
|
|
18
|
+
pub rx: Option<flume::Receiver<LaneItem>>,
|
|
19
|
+
pub ws_tx: Option<flume::Sender<WsEvent>>,
|
|
20
|
+
pub ws_rx: Option<flume::Receiver<WsEvent>>,
|
|
21
|
+
pub bell_pending: AtomicBool,
|
|
22
|
+
pub wake_pending: AtomicBool,
|
|
23
|
+
/// worker_loop only: what its worker's scheduler waits on.
|
|
24
|
+
pub doorbell: Option<Doorbell>,
|
|
25
|
+
/// Placed inline connections not taken yet, apart from its lane: no sibling's steal meets
|
|
26
|
+
/// one.
|
|
27
|
+
pub placed_tx: Option<flume::Sender<Box<crate::inline::InlineConn>>>,
|
|
28
|
+
pub placed_rx: Option<flume::Receiver<Box<crate::inline::InlineConn>>>,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// The dispatcher's lock on the lane, which it takes for every send: on a line of its own, apart
|
|
32
|
+
/// from what the worker reads on every take.
|
|
33
|
+
#[derive(Default)]
|
|
34
|
+
#[repr(align(128))]
|
|
35
|
+
pub struct LaneTx(Mutex<Option<flume::Sender<LaneItem>>>);
|
|
36
|
+
|
|
37
|
+
impl std::ops::Deref for LaneTx {
|
|
38
|
+
type Target = Mutex<Option<flume::Sender<LaneItem>>>;
|
|
39
|
+
|
|
40
|
+
fn deref(&self) -> &Self::Target {
|
|
41
|
+
&self.0
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
impl Lane {
|
|
46
|
+
/// A worker_loop lane is unbounded: its depth follows its worker's room (lane_depth).
|
|
47
|
+
/// Only an `inline` server's gets an inbox for placed connections.
|
|
48
|
+
pub(super) fn new(lanes: bool, inbox: usize, doorbell: Option<Doorbell>, inline: bool) -> Self {
|
|
49
|
+
if !lanes {
|
|
50
|
+
return Lane::default();
|
|
51
|
+
}
|
|
52
|
+
let (tx, rx) = match doorbell {
|
|
53
|
+
None => flume::bounded(LANE_DEPTH),
|
|
54
|
+
Some(_) => flume::unbounded(),
|
|
55
|
+
};
|
|
56
|
+
let (ws_tx, ws_rx) = flume::bounded(inbox);
|
|
57
|
+
let (placed_tx, placed_rx) = match &doorbell {
|
|
58
|
+
Some(_) if inline => {
|
|
59
|
+
let (tx, rx) = flume::unbounded();
|
|
60
|
+
(Some(tx), Some(rx))
|
|
61
|
+
}
|
|
62
|
+
_ => (None, None),
|
|
63
|
+
};
|
|
64
|
+
Lane {
|
|
65
|
+
tx: LaneTx(Mutex::new(Some(tx))),
|
|
66
|
+
rx: Some(rx),
|
|
67
|
+
ws_tx: Some(ws_tx),
|
|
68
|
+
ws_rx: Some(ws_rx),
|
|
69
|
+
doorbell,
|
|
70
|
+
placed_tx,
|
|
71
|
+
placed_rx,
|
|
72
|
+
..Lane::default()
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/// Whether the slot has a lane at all, which wakes and WebSocket events need.
|
|
77
|
+
pub fn exists(&self) -> bool {
|
|
78
|
+
self.rx.is_some()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// Whether its worker runs a worker_loop: it has a doorbell.
|
|
82
|
+
#[inline]
|
|
83
|
+
pub fn is_worker_loop(&self) -> bool {
|
|
84
|
+
self.doorbell.is_some()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/// Rings a worker_loop worker's doorbell; nothing for any other.
|
|
88
|
+
pub fn ring_doorbell(&self) {
|
|
89
|
+
if let Some(doorbell) = &self.doorbell {
|
|
90
|
+
doorbell.ring();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/// Anything in the lane, a bell included.
|
|
95
|
+
#[inline]
|
|
96
|
+
pub fn holds_items(&self) -> bool {
|
|
97
|
+
self.rx.as_ref().is_some_and(|lane| !lane.is_empty())
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/// A wake due, a socket event in the inbox, or anything in the lane.
|
|
101
|
+
pub fn has_waiting(&self) -> bool {
|
|
102
|
+
self.wake_pending.load(Ordering::Relaxed)
|
|
103
|
+
|| self.ws_rx.as_ref().is_some_and(|inbox| !inbox.is_empty())
|
|
104
|
+
|| self.holds_items()
|
|
105
|
+
|| self.placed_waiting()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
pub fn placed_waiting(&self) -> bool {
|
|
109
|
+
let placed = self.placed_rx.as_ref();
|
|
110
|
+
placed.is_some_and(|placed| !placed.is_empty())
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/// The wake hook's turn, if due. The load first keeps the every-take check from writing
|
|
114
|
+
/// the flag.
|
|
115
|
+
pub fn take_wake(&self) -> bool {
|
|
116
|
+
self.wake_pending.load(Ordering::SeqCst) && self.wake_pending.swap(false, Ordering::SeqCst)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/// A take found the lane's bell: the flag goes down before the inbox and the wake flag
|
|
120
|
+
/// are read, so what arrives meanwhile rings again.
|
|
121
|
+
#[inline]
|
|
122
|
+
pub fn took_bell(&self) {
|
|
123
|
+
self.bell_pending.store(false, Ordering::SeqCst);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/// Requests waiting in the lane (None without one); not a bell, so sockets don't grow
|
|
127
|
+
/// the pool.
|
|
128
|
+
pub fn requests(&self) -> Option<usize> {
|
|
129
|
+
let bell = self.bell_pending.load(Ordering::Relaxed);
|
|
130
|
+
Some(self.rx.as_ref()?.len().saturating_sub(usize::from(bell)))
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
#[inline]
|
|
134
|
+
pub fn open_and_empty(&self) -> bool {
|
|
135
|
+
let lane = self.rx.as_ref();
|
|
136
|
+
lane.is_some_and(|rx| !rx.is_disconnected() && rx.is_empty())
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
//! Which worker is on a slot, and how it is to leave.
|
|
2
|
+
|
|
3
|
+
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
|
4
|
+
|
|
5
|
+
use parking_lot::Mutex;
|
|
6
|
+
|
|
7
|
+
/// Which worker is on the slot, and how it is to leave.
|
|
8
|
+
#[derive(Default)]
|
|
9
|
+
#[repr(align(128))]
|
|
10
|
+
pub struct Life {
|
|
11
|
+
/// Moves on whenever a worker leaves the slot or the slot goes to a new one, so a Waker
|
|
12
|
+
/// or a retirement meant for one worker cannot reach a later one.
|
|
13
|
+
pub generation: AtomicU64,
|
|
14
|
+
/// Generation plus one of the worker handle holding the slot (queue::Worker) until it lets
|
|
15
|
+
/// go, else 0, as a Ractor's threads may outlive Ractor#value (hold, holds, let_go).
|
|
16
|
+
pub holder: AtomicU64,
|
|
17
|
+
/// Set by the scaler, or by abort: dispatch skips the slot and the take loop ends at its
|
|
18
|
+
/// next idle tick. Cleared by reset_under.
|
|
19
|
+
pub retired: AtomicBool,
|
|
20
|
+
/// Abandoned as wedged: out of wedge detection, busy_ms reported as 0.
|
|
21
|
+
pub quarantined: AtomicBool,
|
|
22
|
+
/// Past the shutdown deadline (see WorkerSlot::halt): the slot's waits, polling it between
|
|
23
|
+
/// bounded ticks, end as a shutdown. Kino's alone: never the target of a VM unblock
|
|
24
|
+
/// function, and lowered only for the slot's next worker. Private, so that nothing but
|
|
25
|
+
/// halt and reset_halt writes it.
|
|
26
|
+
halted: AtomicBool,
|
|
27
|
+
/// worker_loop: when serve returned (mono us) and its label, for lingering_loops' warning.
|
|
28
|
+
pub drained: Mutex<Option<(u64, String)>>,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
impl Life {
|
|
32
|
+
pub fn generation(&self) -> u64 {
|
|
33
|
+
self.generation.load(Ordering::SeqCst)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// A worker's handle takes the slot at its generation, which it returns: the handle's name
|
|
37
|
+
/// for its hold.
|
|
38
|
+
pub fn hold(&self) -> u64 {
|
|
39
|
+
let generation = self.generation();
|
|
40
|
+
self.holder.store(generation + 1, Ordering::SeqCst);
|
|
41
|
+
generation
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// Whether the handle that took the slot at `generation` still holds it.
|
|
45
|
+
pub fn holds(&self, generation: u64) -> bool {
|
|
46
|
+
self.holder.load(Ordering::SeqCst) == generation + 1
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/// That handle lets go, once: false when it held the slot no more.
|
|
50
|
+
pub fn let_go(&self, generation: u64) -> bool {
|
|
51
|
+
let (held, seq) = (generation + 1, Ordering::SeqCst);
|
|
52
|
+
self.holder.compare_exchange(held, 0, seq, seq).is_ok()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// Whether any handle holds the slot: its last worker's keeps it from the next.
|
|
56
|
+
pub fn is_held(&self) -> bool {
|
|
57
|
+
self.holder.load(Ordering::SeqCst) != 0
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/// Exact under the lane lock (see WorkerSlot::retire_at), and for the worker's own exit.
|
|
61
|
+
#[inline]
|
|
62
|
+
pub fn retired(&self) -> bool {
|
|
63
|
+
self.retired.load(Ordering::SeqCst)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// Advisory: a look that a later exact one, or the next tick, corrects.
|
|
67
|
+
#[inline]
|
|
68
|
+
pub fn seems_retired(&self) -> bool {
|
|
69
|
+
self.retired.load(Ordering::Relaxed)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// The stop flag a park or a blocked body read polls.
|
|
73
|
+
#[inline]
|
|
74
|
+
pub fn halted(&self) -> &AtomicBool {
|
|
75
|
+
&self.halted
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
#[inline]
|
|
79
|
+
pub fn is_halted(&self) -> bool {
|
|
80
|
+
self.halted.load(Ordering::SeqCst)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// Past the shutdown deadline: the slot's waits end.
|
|
84
|
+
pub(super) fn halt(&self) {
|
|
85
|
+
self.halted.store(true, Ordering::SeqCst);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/// For the slot's next worker.
|
|
89
|
+
pub(super) fn reset_halt(&self) {
|
|
90
|
+
self.halted.store(false, Ordering::SeqCst);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/// worker_loop: its serve returned now, under `label`.
|
|
94
|
+
pub fn drained(&self, label: String) {
|
|
95
|
+
*self.drained.lock() = Some((crate::mono::mono_us(), label));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// worker_loop: the label of a loop whose serve returned by `due` (mono us), once.
|
|
99
|
+
pub fn take_drained_by(&self, due: u64) -> Option<String> {
|
|
100
|
+
let mut drained = self.drained.lock();
|
|
101
|
+
match drained.as_ref() {
|
|
102
|
+
Some((at, _)) if *at <= due => drained.take().map(|(_, label)| label),
|
|
103
|
+
_ => None,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/// Advisory, for the stats.
|
|
108
|
+
pub fn quarantined(&self) -> bool {
|
|
109
|
+
self.quarantined.load(Ordering::Relaxed)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
//! What a slot's worker is serving.
|
|
2
|
+
|
|
3
|
+
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
|
4
|
+
use std::sync::Arc;
|
|
5
|
+
|
|
6
|
+
use parking_lot::Mutex;
|
|
7
|
+
|
|
8
|
+
use crate::response::Responder;
|
|
9
|
+
|
|
10
|
+
/// Load::holding for an item that is not an admitted request: no responder lives there.
|
|
11
|
+
pub const HOLDING_ITEM: usize = 1;
|
|
12
|
+
|
|
13
|
+
/// What the supervisor 500s if the worker dies, or its next take if it let go of one unanswered.
|
|
14
|
+
type Current = smallvec::SmallVec<[Arc<Responder>; 8]>;
|
|
15
|
+
|
|
16
|
+
/// Relaxed, advisory. in_flight counts a socket callback or wake hook too (a worker_loop
|
|
17
|
+
/// worker's: its open requests); last_started_us drives wedge reporting and steers dispatch
|
|
18
|
+
/// off a slow item.
|
|
19
|
+
#[derive(Default)]
|
|
20
|
+
#[repr(align(128))]
|
|
21
|
+
pub struct Load {
|
|
22
|
+
/// A batch may hold several.
|
|
23
|
+
pub current: Mutex<Current>,
|
|
24
|
+
pub in_flight: AtomicUsize,
|
|
25
|
+
pub served: AtomicU64,
|
|
26
|
+
pub last_started_us: AtomicU64,
|
|
27
|
+
/// worker_loop: what its thread holds, its request's responder address until answered, else
|
|
28
|
+
/// HOLDING_ITEM; 0 back in its loop. Lane dispatch prefers an idle sibling to a holder.
|
|
29
|
+
pub holding: AtomicUsize,
|
|
30
|
+
/// worker_loop mode: the most requests its worker runs at once (0 = no limit).
|
|
31
|
+
pub capacity: AtomicUsize,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
impl Load {
|
|
35
|
+
#[inline]
|
|
36
|
+
pub fn started(&self) -> u64 {
|
|
37
|
+
let now = crate::mono::mono_us();
|
|
38
|
+
self.last_started_us.store(now, Ordering::Relaxed);
|
|
39
|
+
now
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/// A request joins what the worker serves at `now`, a clock read the caller made, counted
|
|
43
|
+
/// in under the list's lock, as `answered` reads it.
|
|
44
|
+
#[inline]
|
|
45
|
+
pub fn admit(&self, responder: &Arc<Responder>, now: std::time::Instant) {
|
|
46
|
+
self.served.fetch_add(1, Ordering::Relaxed);
|
|
47
|
+
let now = crate::mono::us_at(now);
|
|
48
|
+
self.last_started_us.store(now, Ordering::Relaxed);
|
|
49
|
+
let mut current = self.current.lock();
|
|
50
|
+
current.push(responder.clone());
|
|
51
|
+
self.in_flight.fetch_add(1, Ordering::Relaxed);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/// A classic worker comes back for its next take, or a worker leaves its slot: what is listed
|
|
55
|
+
/// unanswered is answered now (Responder::let_go), as nothing it started still runs.
|
|
56
|
+
pub fn let_go(&self) {
|
|
57
|
+
for responder in self.current.lock().drain(..) {
|
|
58
|
+
responder.let_go();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/// The worker came back for its next take: the previous batch is fully answered, what it
|
|
63
|
+
/// left unanswered first (let_go).
|
|
64
|
+
#[inline]
|
|
65
|
+
pub fn idle(&self) {
|
|
66
|
+
self.let_go();
|
|
67
|
+
self.in_flight.store(0, Ordering::Relaxed);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// A socket callback or a wake hook counts as one item, busy until the next take, so one
|
|
71
|
+
/// that never returns reads as a wedge.
|
|
72
|
+
#[inline]
|
|
73
|
+
pub fn runs_item(&self) {
|
|
74
|
+
self.started();
|
|
75
|
+
self.in_flight.store(1, Ordering::Relaxed);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// When its last item started (mono us), busy or not: a clock read a take need not repeat.
|
|
79
|
+
#[inline]
|
|
80
|
+
pub fn last_started(&self) -> u64 {
|
|
81
|
+
self.last_started_us.load(Ordering::Relaxed)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// A dead worker holds nothing: no phantom busy worker in the stats.
|
|
85
|
+
pub fn clear(&self) {
|
|
86
|
+
self.in_flight.store(0, Ordering::Relaxed);
|
|
87
|
+
self.last_started_us.store(0, Ordering::Relaxed);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// When its current work started (mono us); None while nothing is in flight.
|
|
91
|
+
#[inline]
|
|
92
|
+
pub fn busy_since(&self) -> Option<u64> {
|
|
93
|
+
if self.in_flight.load(Ordering::Relaxed) == 0 {
|
|
94
|
+
return None;
|
|
95
|
+
}
|
|
96
|
+
Some(self.last_started_us.load(Ordering::Relaxed))
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/// worker_loop: when its thread last left its loop (mono us), or the end of the tick a
|
|
100
|
+
/// doorbell wait gets; None before its first take and while it takes with no job slot.
|
|
101
|
+
#[inline]
|
|
102
|
+
pub fn away_since(&self) -> Option<u64> {
|
|
103
|
+
match self.last_started_us.load(Ordering::Relaxed) {
|
|
104
|
+
0 => None,
|
|
105
|
+
started => Some(started),
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// worker_loop: its thread holds `what`, a request's responder address or HOLDING_ITEM.
|
|
110
|
+
#[inline]
|
|
111
|
+
pub fn hold(&self, what: usize) {
|
|
112
|
+
self.holding.store(what, Ordering::Relaxed);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// worker_loop: its thread holds a request or an item it has not come back from.
|
|
116
|
+
#[inline]
|
|
117
|
+
pub fn is_holding(&self) -> bool {
|
|
118
|
+
self.holding.load(Ordering::Relaxed) != 0
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/// worker_loop: how long its thread stays away from here is its busy age. Returns now.
|
|
122
|
+
#[inline]
|
|
123
|
+
pub fn back_in_loop(&self) -> u64 {
|
|
124
|
+
self.hold(0);
|
|
125
|
+
self.started()
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/// worker_loop: a doorbell wait is time in the loop, back within a TICK (a send or the
|
|
129
|
+
/// ticker rings it), so its busy age starts only once the wait is overdue.
|
|
130
|
+
#[inline]
|
|
131
|
+
pub fn waits_on_doorbell(&self, now: u64) {
|
|
132
|
+
let tick = crate::queue::TICK.as_micros() as u64;
|
|
133
|
+
self.last_started_us.store(now + tick, Ordering::Relaxed);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/// worker_loop: its loop ended, so what still waits on the worker no longer makes it busy.
|
|
137
|
+
pub fn loop_ended(&self) {
|
|
138
|
+
self.last_started_us.store(0, Ordering::Relaxed);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/// worker_loop: the most requests its worker runs at once, set by its serve.
|
|
142
|
+
pub fn set_capacity(&self, capacity: usize) {
|
|
143
|
+
self.capacity.store(capacity, Ordering::Relaxed);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/// worker_loop: the job slots its worker has free.
|
|
147
|
+
#[inline]
|
|
148
|
+
pub fn room(&self) -> usize {
|
|
149
|
+
let capacity = self.capacity.load(Ordering::Relaxed);
|
|
150
|
+
capacity.saturating_sub(self.in_flight.load(Ordering::Relaxed))
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
pub fn at_capacity(&self) -> bool {
|
|
154
|
+
let capacity = self.capacity.load(Ordering::Relaxed);
|
|
155
|
+
capacity > 0 && self.in_flight.load(Ordering::Relaxed) >= capacity
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/// worker_loop: prunes the in-flight list; whether that took it below its capacity.
|
|
159
|
+
pub fn keep_open(&self) -> bool {
|
|
160
|
+
let mut current = self.current.lock();
|
|
161
|
+
let full = self.at_capacity();
|
|
162
|
+
self.count_open(&mut current);
|
|
163
|
+
full && !self.at_capacity()
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/// worker_loop: its thread no longer holds the request, and its open ones are counted now,
|
|
167
|
+
/// not at its next take, which may wait for the job's hooks. At capacity only a take with
|
|
168
|
+
/// room lowers the count, telling a request waiting for lane room; the list's lock keeps
|
|
169
|
+
/// this out of its way.
|
|
170
|
+
pub fn answered(&self, responder: &Arc<Responder>) {
|
|
171
|
+
let request = Arc::as_ptr(responder) as usize;
|
|
172
|
+
let relaxed = Ordering::Relaxed;
|
|
173
|
+
let _ = self.holding.compare_exchange(request, 0, relaxed, relaxed);
|
|
174
|
+
let mut current = self.current.lock();
|
|
175
|
+
if !self.at_capacity() {
|
|
176
|
+
self.count_open(&mut current);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/// Under the list's lock, which admit counts a request in under too.
|
|
181
|
+
fn count_open(&self, current: &mut Current) {
|
|
182
|
+
current.retain(|responder| responder.open());
|
|
183
|
+
self.in_flight.store(current.len(), Ordering::Relaxed);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
pub fn in_flight(&self) -> usize {
|
|
187
|
+
self.in_flight.load(Ordering::Relaxed)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
pub fn served(&self) -> u64 {
|
|
191
|
+
self.served.load(Ordering::Relaxed)
|
|
192
|
+
}
|
|
193
|
+
}
|