1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! kevy-rt — shared-nothing, thread-per-core runtime.
//!
//! Each core runs its own reactor (kqueue/epoll) and owns one **shard** of the
//! keyspace (`hash(key) % nshards`). There is no shared mutable state and no
//! lock on the hot path — cores communicate only by message passing over
//! channels, woken via a self-pipe ([`kevy_sys::Waker`]). Connections are spread
//! across cores by `SO_REUSEPORT`; a command whose key lives on another core is
//! forwarded to that core, executed there, and the reply routed back to the
//! originating connection.
//!
//! Per-connection reply ordering is preserved (RESP is pipelined): each command
//! gets a monotonic seq; replies are emitted only in contiguous seq order, so an
//! async cross-core reply never overtakes an earlier one.
//!
//! The cross-core channel currently uses `std::sync::mpsc` (pure Rust, zero
//! deps); swapping in a lock-free SPSC/MPSC ring is a perf-polish item.
//! Command semantics are injected via the [`Commands`] trait, keeping the
//! runtime independent of the concrete command set. Part of the [kevy] server.
//!
//! [kevy]: https://crates.io/crates/kevy
//!
//! # Module map
//!
//! - [`Runtime`] (in `runtime`) — public entry point; spawns one `shard` per core.
//! - `shard` — the per-core reactor: sockets, the inbound queue, reply flushing.
//! - `exec` — command semantics: routing, execution, and result reduction.
//! - `message` — internal cross-core work/result types.
//! - `conn` — per-connection state (input/output, seq ring, subscriptions).
//! - `reduce` — reply reduction (`materialize`) and pure helpers (set algebra,
//! shard hashing, pub/sub framing).
//!
//! # Example
//!
//! Implement [`Commands`] for your command set and run it. ([`Store`] is
//! re-exported so you don't need a separate dependency.)
//!
//! ```no_run
//! use kevy_rt::{ArgvView, Commands, Route, Runtime, Store, TxnKind};
//! use std::sync::Arc;
//! use std::sync::atomic::AtomicBool;
//!
//! #[derive(Clone)]
//! struct MyCommands;
//! impl Commands for MyCommands {
//! fn route<A: ArgvView + ?Sized>(&self, args: &A) -> Route {
//! if args.len() >= 2 { Route::Single(1) } else { Route::Local }
//! }
//! fn dispatch<A: ArgvView + ?Sized>(&self, _store: &mut Store, _args: &A) -> Vec<u8> {
//! b"+OK\r\n".to_vec()
//! }
//! fn is_quit<A: ArgvView + ?Sized>(&self, args: &A) -> bool {
//! args.first().is_some_and(|c| c.eq_ignore_ascii_case(b"QUIT"))
//! }
//! fn is_write<A: ArgvView + ?Sized>(&self, _args: &A) -> bool { false }
//! fn txn_kind<A: ArgvView + ?Sized>(&self, _args: &A) -> TxnKind { TxnKind::Other }
//! }
//!
//! // One shard per core, listening on 127.0.0.1:6379, until `stop` is set.
//! let rt = Runtime::builder(MyCommands).bind([127, 0, 0, 1], 6379).shards(4);
//! rt.run(Arc::new(AtomicBool::new(false))).unwrap();
//! ```
// Almost entirely safe: the only `unsafe` is in `uring_reactor` (Linux io_uring),
// which needs raw buffer pointers for zero-allocation completion I/O — on the hot
// path toward kevy's disk-I/O-ceiling goal, where a buffer-ownership safe wrapper
// would add per-op cost. Each such block documents its invariant; the
// epoll/kqueue path and every other module stay safe, and all libc lives in
// kevy-sys.
//! Every public item here is documented, and the lint keeps it that
//! way: kevy-rt is the reactor, and `warnings = "deny"` turns a new
//! gap into a compile error rather than a number that drifts. Closed
//! from 35 sites in v6 — all of them fields inside well-documented
//! variants, which is where prose review does not look.
// `--cfg loom` is a known custom cfg: it swaps the park/wake fence's atomic
// for loom's instrumented one and publishes `park_fence` so `tests/loom.rs`
// can schedule it. rustc cannot learn a RUSTFLAGS-set cfg name, so silence
// the lint rather than let it fail every normal build.
pub use counters as serve_counters;
// Private in every normal build. A `--cfg loom` build publishes it so
// `tests/loom.rs` — a separate crate — can schedule the real functions
// instead of a hand-built replica of them. The public API is unchanged
// for every build anyone ships.
// `test` too: pure, tested everywhere
// `test` too: pure, tested everywhere
/// Hard cap on a single connection's accumulated unflushed reply
/// bytes. A client that stops reading (or a slow pub/sub subscriber)
/// lets its per-conn output buffer grow without bound; past this it is
/// disconnected so it can't OOM the shard. Deliberately generous
/// (512 MiB, one max bulk's order of magnitude): a legitimate large
/// reply drains progressively and never accumulates near it — only a
/// non-draining reader does. Enforced out-of-band per tick by
/// `Shard::enforce_output_limit` / `uring_enforce_output_limit`.
pub const CLIENT_OUTPUT_HARD_LIMIT: usize = 512 * 1024 * 1024;
/// Cap on a conn's ACCUMULATED unparsed input (Redis's
/// client-query-buffer-limit, same 1GB default): a client streaming an
/// incomplete-but-valid giant frame (1M declared args × 512MB bulks)
/// would otherwise grow `conn.input` without bound and OOM the shard.
/// Overridable via `KEVY_DEBUG_INPUT_LIMIT` (a debug surface, like
/// `KEVY_DEBUG_STALL_MS`) so the guard is e2e-testable without
/// streaming a real gigabyte.
pub const CLIENT_INPUT_HARD_LIMIT: usize = 1024 * 1024 * 1024;
pub use ;
pub use ClientKillFilter;
pub use shard_slot_range;
pub use GeoHits;
pub use ;
pub use NotificationFlags;
pub use Fsync;
pub use ;
pub use Store;
pub use push_lua_wake_key;
pub use ;
pub use shard_of as shard_of_key;
pub use ;
pub use ;
pub use ReplicatedApplyGuard;
pub use ;
pub use Runtime;
pub use ;
pub use crateCommands;