Skip to main content

rust_web_server/server/
per_core.rs

1//! Per-core runtimes (`RWS_CONFIG_RUNTIME=per-core`, Unix only): one
2//! single-threaded tokio runtime per core, each accepting on its own
3//! `SO_REUSEPORT` socket, the way nginx runs one worker per core. A
4//! connection stays on the thread the kernel gave it to, so there's no work
5//! stealing, no cross-thread wake-ups and no idle workers searching for work.
6//!
7//! The `rws` binary runs per-core unless configured otherwise (since 18.0.0,
8//! [`default_to_per_core`]); for an application built on the library the
9//! default stays `shared`, tokio's multi-threaded runtime. `run_tls` and
10//! `run_plain` start the per-core accept loops ([`spawn`]) and keep the
11//! signals: SIGINT/SIGTERM stop every loop, SIGHUP reloads the config and
12//! hands the rebuilt TLS acceptor to every loop. HTTP/3, the redirect
13//! listener and ACME stay on the main runtime.
14//!
15//! Tokio I/O belongs to the runtime that created it, so pooled upstream
16//! connections are kept per thread ([`pool_key`]).
17
18use std::borrow::Cow;
19use std::cell::Cell;
20use std::net::SocketAddr;
21
22use tokio::sync::watch;
23
24/// `RWS_CONFIG_RUNTIME`: `shared` or `per-core` (the `rws` binary's default).
25pub const RWS_CONFIG_RUNTIME: &str = "RWS_CONFIG_RUNTIME";
26
27thread_local! {
28    /// 1-based index of this thread's per-core runtime; 0 elsewhere.
29    static SLOT: Cell<usize> = const { Cell::new(0) };
30}
31
32/// Whether `RWS_CONFIG_RUNTIME` asks for per-core runtimes (always `false`
33/// off Unix, where there is no `SO_REUSEPORT`).
34pub fn enabled() -> bool {
35    cfg!(unix)
36        && std::env::var(RWS_CONFIG_RUNTIME).is_ok_and(|v| v.trim().eq_ignore_ascii_case("per-core"))
37}
38
39/// Make `per-core` the runtime unless `RWS_CONFIG_RUNTIME` is already set —
40/// what the `rws` binary does before reading its config file and flags, so
41/// `runtime = "shared"` there (or in the environment) still wins. Library
42/// applications don't call it: their handlers may keep tokio I/O in
43/// process-wide structures, which per-core runtimes don't share.
44pub fn default_to_per_core() {
45    if std::env::var_os(RWS_CONFIG_RUNTIME).is_none() {
46        std::env::set_var(RWS_CONFIG_RUNTIME, "per-core");
47        crate::server_config::invalidate();
48    }
49}
50
51/// How many per-core runtimes to start: `TOKIO_WORKER_THREADS` when set
52/// (the same knob as the shared runtime), otherwise one per available CPU.
53pub fn runtime_count() -> usize {
54    std::env::var("TOKIO_WORKER_THREADS")
55        .ok()
56        .and_then(|v| v.trim().parse::<usize>().ok())
57        .filter(|n| *n > 0)
58        .unwrap_or_else(|| std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1))
59}
60
61/// The upstream pool key for `key` on this thread: unchanged on the shared
62/// runtime, suffixed with the runtime's index on a per-core one (its
63/// connections can only be driven by that runtime).
64pub(crate) fn pool_key(key: &str) -> Cow<'_, str> {
65    match SLOT.with(|s| s.get()) {
66        0 => Cow::Borrowed(key),
67        // Pushed rather than `format!`ed: this runs for every proxied request.
68        slot => {
69            let slot = crate::ext::decimal::Decimal::new(slot as u128);
70            let mut owned = String::with_capacity(key.len() + 1 + slot.as_str().len());
71            owned.push_str(key);
72            owned.push('#');
73            owned.push_str(slot.as_str());
74            Cow::Owned(owned)
75        }
76    }
77}
78
79/// A listening socket on `addr` that shares the port with the other
80/// runtimes' (`SO_REUSEPORT`: the kernel spreads connections across them).
81#[cfg(unix)]
82pub(crate) fn bind(addr: SocketAddr) -> std::io::Result<tokio::net::TcpListener> {
83    // Socket activation: every runtime accepts on a copy of systemd's socket
84    // (a new SO_REUSEPORT socket couldn't share the port with it).
85    if let Some(listener) = crate::systemd::tcp(addr, None) {
86        listener.set_nonblocking(true)?;
87        return tokio::net::TcpListener::from_std(listener);
88    }
89    let socket = if addr.is_ipv4() { tokio::net::TcpSocket::new_v4()? } else { tokio::net::TcpSocket::new_v6()? };
90    socket.set_reuseaddr(true)?;
91    socket.set_reuseport(true)?;
92    socket.bind(addr)?;
93    socket.listen(1024)
94}
95
96/// Start `count` threads, each running `accept_loop(listener)` on its own
97/// single-threaded runtime until `stop` turns `true`. Returns once every
98/// thread has bound its socket (or failed to), so the port accepts
99/// connections again when it returns — the setup listener was closed just
100/// before, and a client let in by it in between was reset. A thread whose
101/// runtime or socket can't be set up reports it and ends; the others keep
102/// serving.
103#[cfg(unix)]
104pub(crate) fn spawn<F, Fut>(
105    addr: SocketAddr,
106    count: usize,
107    stop: watch::Receiver<bool>,
108    accept_loop: F,
109) -> Vec<std::thread::JoinHandle<()>>
110where
111    F: Fn(tokio::net::TcpListener, watch::Receiver<bool>) -> Fut + Send + Clone + 'static,
112    Fut: std::future::Future<Output = ()>,
113{
114    let mut handles = Vec::with_capacity(count);
115    let (bound_tx, bound_rx) = std::sync::mpsc::channel::<()>();
116    for slot in 1..=count {
117        let bound = bound_tx.clone();
118        let stop = stop.clone();
119        let accept_loop = accept_loop.clone();
120        let spawned = std::thread::Builder::new().name(format!("rws-core-{}", slot)).spawn(move || {
121            SLOT.with(|s| s.set(slot));
122            let runtime = match tokio::runtime::Builder::new_current_thread().enable_all().build() {
123                Ok(r) => r,
124                Err(e) => return eprintln!("per-core runtime {}: {}", slot, e),
125            };
126            runtime.block_on(async move {
127                let listener = bind(addr);
128                drop(bound);
129                match listener {
130                    Ok(listener) => accept_loop(listener, stop).await,
131                    Err(e) => eprintln!("per-core runtime {}: unable to listen on {}: {}", slot, addr, e),
132                }
133            });
134        });
135        match spawned {
136            Ok(h) => handles.push(h),
137            Err(e) => eprintln!("per-core runtime {}: {}", slot, e),
138        }
139    }
140    // Every sender dropped: each thread has bound (or given up). Bounded,
141    // in case a thread never gets that far.
142    drop(bound_tx);
143    let _ = bound_rx.recv_timeout(std::time::Duration::from_secs(5));
144    handles
145}
146
147/// Resolves once `stop` is `true` (or its sender is gone).
148pub(crate) async fn stopped(stop: &mut watch::Receiver<bool>) {
149    let _ = stop.wait_for(|s| *s).await;
150}
151
152#[cfg(test)]
153mod tests {
154    use super::*;
155
156    #[cfg(unix)]
157    #[test]
158    fn the_port_accepts_connections_when_spawn_returns() {
159        let addr = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap();
160        let (stop_tx, stop_rx) = watch::channel(false);
161        let handles = spawn(addr, 2, stop_rx, |listener: tokio::net::TcpListener, mut stop: watch::Receiver<bool>| async move {
162            tokio::select! {
163                _ = async { while listener.accept().await.is_ok() {} } => {}
164                _ = stopped(&mut stop) => {}
165            }
166        });
167        assert_eq!(2, handles.len());
168        std::net::TcpStream::connect(addr).expect("listening as soon as spawn returns");
169        stop_tx.send(true).unwrap();
170        for handle in handles {
171            handle.join().unwrap();
172        }
173    }
174
175    #[test]
176    fn pool_key_is_shared_off_the_per_core_runtimes() {
177        assert!(matches!(pool_key("backend:80"), Cow::Borrowed("backend:80")));
178    }
179
180    #[test]
181    fn pool_key_is_per_runtime_on_a_per_core_thread() {
182        let keys = std::thread::spawn(|| {
183            SLOT.with(|s| s.set(3));
184            pool_key("backend:80").into_owned()
185        })
186        .join()
187        .unwrap();
188        assert_eq!("backend:80#3", keys);
189    }
190
191    #[test]
192    fn enabled_only_for_per_core() {
193        let _g = crate::test_env::lock();
194        let before = std::env::var(RWS_CONFIG_RUNTIME).ok();
195        std::env::remove_var(RWS_CONFIG_RUNTIME);
196        assert!(!enabled());
197        std::env::set_var(RWS_CONFIG_RUNTIME, "shared");
198        assert!(!enabled());
199        std::env::set_var(RWS_CONFIG_RUNTIME, " Per-Core ");
200        assert_eq!(cfg!(unix), enabled());
201        match before {
202            Some(v) => std::env::set_var(RWS_CONFIG_RUNTIME, v),
203            None => std::env::remove_var(RWS_CONFIG_RUNTIME),
204        }
205    }
206
207    #[test]
208    fn the_binary_defaults_to_per_core_unless_configured() {
209        let _g = crate::test_env::lock();
210        let before = std::env::var(RWS_CONFIG_RUNTIME).ok();
211        std::env::remove_var(RWS_CONFIG_RUNTIME);
212        default_to_per_core();
213        assert_eq!(Some("per-core".to_string()), std::env::var(RWS_CONFIG_RUNTIME).ok());
214        std::env::set_var(RWS_CONFIG_RUNTIME, "shared");
215        default_to_per_core();
216        assert_eq!(Some("shared".to_string()), std::env::var(RWS_CONFIG_RUNTIME).ok());
217        match before {
218            Some(v) => std::env::set_var(RWS_CONFIG_RUNTIME, v),
219            None => std::env::remove_var(RWS_CONFIG_RUNTIME),
220        }
221    }
222
223    #[test]
224    fn runtime_count_follows_tokio_worker_threads() {
225        let _g = crate::test_env::lock();
226        let before = std::env::var("TOKIO_WORKER_THREADS").ok();
227        std::env::set_var("TOKIO_WORKER_THREADS", "3");
228        assert_eq!(3, runtime_count());
229        std::env::set_var("TOKIO_WORKER_THREADS", "zero");
230        assert!(runtime_count() >= 1);
231        match before {
232            Some(v) => std::env::set_var("TOKIO_WORKER_THREADS", v),
233            None => std::env::remove_var("TOKIO_WORKER_THREADS"),
234        }
235    }
236
237    /// Two runtimes share one port; every connection is answered by one of
238    /// them, on its own thread, and both stop when told to.
239    #[cfg(unix)]
240    #[test]
241    fn runtimes_share_the_port_and_stop() {
242        use std::io::{Read, Write};
243        use tokio::io::AsyncWriteExt;
244
245        // A free port: bind, read it, release.
246        let addr = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap();
247        let (stop_tx, stop_rx) = watch::channel(false);
248        let accept_loop = |listener: tokio::net::TcpListener, mut stop: watch::Receiver<bool>| async move {
249            loop {
250                tokio::select! {
251                    result = listener.accept() => {
252                        if let Ok((mut stream, _)) = result {
253                            let slot = SLOT.with(|s| s.get());
254                            let _ = stream.write_all(slot.to_string().as_bytes()).await;
255                        }
256                    }
257                    _ = stopped(&mut stop) => break,
258                }
259            }
260        };
261        let handles = spawn(addr, 2, stop_rx, accept_loop);
262        assert_eq!(2, handles.len());
263
264        let mut answered = 0;
265        for _ in 0..50 {
266            let Ok(mut c) = std::net::TcpStream::connect(addr) else {
267                std::thread::sleep(std::time::Duration::from_millis(20)); // not listening yet
268                continue;
269            };
270            let _ = c.write_all(b"x");
271            let mut slot = String::new();
272            let _ = c.read_to_string(&mut slot);
273            if !slot.is_empty() {
274                assert!(slot == "1" || slot == "2", "slot {slot}");
275                answered += 1;
276            }
277        }
278        assert!(answered > 0);
279
280        stop_tx.send(true).unwrap();
281        for h in handles {
282            h.join().unwrap();
283        }
284        assert!(std::net::TcpStream::connect(addr).is_err());
285    }
286
287    #[cfg(unix)]
288    #[test]
289    fn bind_reports_an_address_in_use_without_reuseport() {
290        let taken = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
291        let addr = taken.local_addr().unwrap();
292        let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
293        runtime.block_on(async { assert!(bind(addr).is_err()) });
294    }
295}