net_backend_server 0.2.0

A framework for game backend servers: tokio + axum, modules with hooks, MySQL / PostgreSQL / SQLite, migrations, OpenAPI.
Documentation
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Serving on a real loopback socket: graceful shutdown (in-flight requests drain, modules and
//! hooks shut down in order, the listener closes), the grace deadline, a failing start, and
//! migrations on start. Bounded: every wait has a timeout, nothing is left running.
#![cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]

mod common;

use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use axum::routing::get;
use common::http_config;
use futures_util::future::BoxFuture;
use net_backend_server::{AppState, Error, Module, NetBackendServer};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::oneshot;

/// The upper bound of every wait. Tests wait for a condition, never a fixed time; CI runners are slow
/// and shared, so the bound is generous (a passing run never comes close).
const WAIT: Duration = Duration::from_secs(30);

type Log = Arc<Mutex<Vec<String>>>;

fn push(log: &Log, line: impl Into<String>) {
    if let Ok(mut log) = log.lock() {
        log.push(line.into());
    }
}

fn lines(log: &Log) -> Vec<String> {
    log.lock().map(|l| l.clone()).unwrap_or_default()
}

/// A module logging its start and shutdown; `fail_start` makes its start fail.
struct Lifecycle {
    name: &'static str,
    log: Log,
    fail_start: bool,
}

impl Module for Lifecycle {
    fn name(&self) -> &'static str {
        self.name
    }

    fn start<'a>(&'a self, _state: &'a AppState) -> BoxFuture<'a, Result<(), Error>> {
        Box::pin(async move {
            if self.fail_start {
                return Err(Error::Startup("cannot start".into()));
            }
            push(&self.log, format!("start {}", self.name));
            Ok(())
        })
    }

    fn shutdown<'a>(&'a self, _state: &'a AppState) -> BoxFuture<'a, ()> {
        Box::pin(async move { push(&self.log, format!("shutdown {}", self.name)) })
    }
}

/// A minimal HTTP/1.1 GET over a fresh connection: (status, body).
async fn http_get(addr: SocketAddr, path: &str) -> std::io::Result<(u16, String)> {
    let mut stream = TcpStream::connect(addr).await?;
    stream.write_all(format!("GET {path} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n").as_bytes()).await?;
    let mut raw = Vec::new();
    stream.read_to_end(&mut raw).await?;
    let text = String::from_utf8_lossy(&raw).into_owned();
    let status = text.split(' ').nth(1).and_then(|s| s.parse().ok()).unwrap_or(0);
    let body = text.split("\r\n\r\n").nth(1).unwrap_or_default().to_string();
    Ok((status, body))
}

async fn wait_until_up(addr: SocketAddr) {
    let deadline = Instant::now() + WAIT;
    while Instant::now() < deadline {
        if let Ok((200, _)) = http_get(addr, "/healthz").await {
            return;
        }
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
    panic!("the server did not come up");
}

#[tokio::test]
async fn graceful_shutdown_drains_and_runs_callbacks_in_order() {
    let log: Log = Arc::default();
    let (log_start, log_stop) = (log.clone(), log.clone());
    let entered = Arc::new(tokio::sync::Notify::new());
    let entered_by_handler = entered.clone();
    let server = NetBackendServer::new(http_config())
        .module(Lifecycle { name: "first", log: log.clone(), fail_start: false })
        .module(Lifecycle { name: "second", log: log.clone(), fail_start: false })
        .on_start(move |_ctx| {
            let log = log_start.clone();
            async move {
                push(&log, "start hook");
                Ok(())
            }
        })
        .on_shutdown(move |_ctx| {
            let log = log_stop.clone();
            async move { push(&log, "shutdown hook") }
        })
        .route(
            "/v1/game/slow",
            get(move || {
                let entered = entered_by_handler.clone();
                async move {
                    entered.notify_one();
                    tokio::time::sleep(Duration::from_millis(600)).await;
                    "done"
                }
            }),
        );
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("addr");
    let (stop, stopped) = oneshot::channel::<()>();
    let running = tokio::spawn(server.serve_with_shutdown(listener, async move {
        let _ = stopped.await;
    }));
    wait_until_up(addr).await;

    let in_flight = tokio::spawn(http_get(addr, "/v1/game/slow"));
    // The request is in its handler (a condition, not a fixed sleep: CI runners are slow).
    tokio::time::timeout(WAIT, entered.notified()).await.expect("the handler started");
    let _ = stop.send(());
    let answer = tokio::time::timeout(WAIT, in_flight).await.expect("in time").expect("task").expect("io");
    assert_eq!(answer, (200, "done".to_string()), "the in-flight request finished");
    let result = tokio::time::timeout(WAIT, running).await.expect("server stopped in time").expect("task");
    assert!(result.is_ok(), "{result:?}");
    assert_eq!(lines(&log), ["start first", "start second", "start hook", "shutdown second", "shutdown first", "shutdown hook"]);
    // The listener is closed.
    assert!(http_get(addr, "/healthz").await.is_err());
}

#[tokio::test]
async fn grace_deadline_bounds_the_shutdown() {
    let mut config = http_config();
    config.server.shutdown_grace_secs = 1;
    let finished = Arc::new(AtomicBool::new(false));
    let dropped = Arc::new(AtomicBool::new(false));
    let entered = Arc::new(tokio::sync::Notify::new());
    let (flag, drop_flag, entered_by_handler) = (finished.clone(), dropped.clone(), entered.clone());
    /// Sets its flag when the handler's future is dropped.
    struct OnDrop(Arc<AtomicBool>);
    impl Drop for OnDrop {
        fn drop(&mut self) {
            self.0.store(true, Ordering::SeqCst);
        }
    }
    let log: Log = Arc::default();
    let server = NetBackendServer::new(config).module(Lifecycle { name: "pool_user", log: log.clone(), fail_start: false }).route(
        "/v1/game/stuck",
        get(move || {
            let (flag, guard, entered) = (flag.clone(), OnDrop(drop_flag.clone()), entered_by_handler.clone());
            async move {
                let _guard = guard;
                entered.notify_one();
                tokio::time::sleep(Duration::from_secs(120)).await;
                flag.store(true, Ordering::SeqCst);
                "late"
            }
        }),
    );
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("addr");
    let (stop, stopped) = oneshot::channel::<()>();
    let running = tokio::spawn(server.serve_with_shutdown(listener, async move {
        let _ = stopped.await;
    }));
    wait_until_up(addr).await;
    let stuck = tokio::spawn(http_get(addr, "/v1/game/stuck"));
    tokio::time::timeout(WAIT, entered.notified()).await.expect("the handler started");
    let started = Instant::now();
    let _ = stop.send(());
    let result = tokio::time::timeout(WAIT, running).await.expect("stopped within the deadline").expect("task");
    assert!(result.is_ok(), "{result:?}");
    let took = started.elapsed();
    // At the 1 s grace, long before the handler's 120 s (generous upper bound: slow runners).
    assert!(took >= Duration::from_millis(900) && took < Duration::from_secs(60), "{took:?}");
    assert_eq!(lines(&log), ["start pool_user", "shutdown pool_user"]);
    // The handler was dropped at the deadline (its future is gone, it never finishes), and the
    // client gets no answer.
    assert!(dropped.load(Ordering::SeqCst), "the handler was not dropped at the deadline");
    let answer = tokio::time::timeout(WAIT, stuck).await.expect("client returns").expect("task");
    assert!(!matches!(answer, Ok((200, _))), "{answer:?}");
    assert!(!finished.load(Ordering::SeqCst), "the handler completed after shutdown");
}

#[tokio::test]
async fn slow_headers_are_cut_off() {
    let mut config = http_config();
    config.server.header_read_timeout_secs = 1;
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("addr");
    let (stop, stopped) = oneshot::channel::<()>();
    let running = tokio::spawn(NetBackendServer::new(config).serve_with_shutdown(listener, async move {
        let _ = stopped.await;
    }));
    wait_until_up(addr).await;
    // A half-sent request: the request line and one header, never the blank line.
    let mut stream = TcpStream::connect(addr).await.expect("connect");
    stream.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\n").await.expect("write");
    let started = Instant::now();
    let mut rest = Vec::new();
    let closed = tokio::time::timeout(WAIT, stream.read_to_end(&mut rest)).await;
    assert!(closed.is_ok(), "the connection is still open after {WAIT:?}");
    assert!(started.elapsed() >= Duration::from_millis(500), "{:?}", started.elapsed());
    // An idle keep-alive connection is closed after the same time.
    let mut idle = TcpStream::connect(addr).await.expect("connect");
    let mut buf = [0u8; 16];
    let closed = tokio::time::timeout(WAIT, idle.read(&mut buf)).await;
    assert!(matches!(closed, Ok(Ok(0)) | Ok(Err(_))), "{closed:?}");
    let _ = stop.send(());
    let _ = tokio::time::timeout(WAIT, running).await;
}

/// A module whose start or shutdown misbehaves.
struct Misbehaving {
    start: &'static str,
    stop: &'static str,
    log: Log,
}

impl Module for Misbehaving {
    fn name(&self) -> &'static str {
        "misbehaving"
    }

    fn start<'a>(&'a self, _state: &'a AppState) -> BoxFuture<'a, Result<(), Error>> {
        Box::pin(async move {
            match self.start {
                "panic" => panic!("start bug"),
                "hang" => std::future::pending().await,
                _ => Ok(()),
            }
        })
    }

    fn shutdown<'a>(&'a self, _state: &'a AppState) -> BoxFuture<'a, ()> {
        Box::pin(async move {
            match self.stop {
                "panic" => panic!("shutdown bug"),
                "hang" => std::future::pending().await,
                _ => push(&self.log, "misbehaving down"),
            }
        })
    }
}

#[tokio::test]
async fn module_start_and_shutdown_are_bounded_and_contained() {
    let mut config = http_config();
    config.server.module_start_timeout_secs = 1;
    config.server.module_shutdown_timeout_secs = 1;
    let log: Log = Arc::default();
    for (start, expected) in [("panic", "start panicked"), ("hang", "did not finish within 1 s")] {
        let server = NetBackendServer::new(config.clone()).module(Lifecycle { name: "before", log: log.clone(), fail_start: false }).module(Misbehaving {
            start,
            stop: "ok",
            log: log.clone(),
        });
        let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
        let result = tokio::time::timeout(WAIT, server.serve_with_shutdown(listener, std::future::pending())).await.expect("returns");
        assert!(matches!(&result, Err(Error::Startup(m)) if m.contains(expected)), "{result:?}");
    }
    assert_eq!(lines(&log), ["start before", "shutdown before", "start before", "shutdown before"]);

    // A shutdown that panics or hangs does not stop the others or the stop itself.
    for stop_kind in ["panic", "hang"] {
        let log: Log = Arc::default();
        let server = NetBackendServer::new(config.clone()).module(Lifecycle { name: "first", log: log.clone(), fail_start: false }).module(Misbehaving {
            start: "ok",
            stop: stop_kind,
            log: log.clone(),
        });
        let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
        let addr = listener.local_addr().expect("addr");
        let (stop, stopped) = oneshot::channel::<()>();
        let running = tokio::spawn(server.serve_with_shutdown(listener, async move {
            let _ = stopped.await;
        }));
        wait_until_up(addr).await;
        let _ = stop.send(());
        let result = tokio::time::timeout(WAIT, running).await.expect("stopped").expect("task");
        assert!(result.is_ok(), "{result:?}");
        assert_eq!(lines(&log), ["start first", "shutdown first"], "{stop_kind}");
    }
}

#[tokio::test]
async fn a_failing_start_shuts_down_what_started() {
    let log: Log = Arc::default();
    let server = NetBackendServer::new(http_config())
        .module(Lifecycle { name: "good", log: log.clone(), fail_start: false })
        .module(Lifecycle { name: "bad", log: log.clone(), fail_start: true })
        .module(Lifecycle { name: "never", log: log.clone(), fail_start: false });
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let result = tokio::time::timeout(WAIT, server.serve_with_shutdown(listener, std::future::pending())).await.expect("returns");
    assert!(matches!(&result, Err(Error::Startup(m)) if m.contains("module `bad`")), "{result:?}");
    assert_eq!(lines(&log), ["start good", "shutdown good"]);

    // A failing start hook too.
    let server = NetBackendServer::new(http_config()).on_start(|_ctx| async { Err(net_backend_server::AppError::conflict("no")) });
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let result = tokio::time::timeout(WAIT, server.serve_with_shutdown(listener, std::future::pending())).await.expect("returns");
    assert!(matches!(&result, Err(Error::Startup(m)) if m.contains("start hook 0 failed")), "{result:?}");
}

/// A module whose shutdown takes `stop` (forever with `None`).
struct Slow {
    name: &'static str,
    stop: Option<Duration>,
    log: Log,
}

impl Module for Slow {
    fn name(&self) -> &'static str {
        self.name
    }

    fn shutdown<'a>(&'a self, _state: &'a AppState) -> BoxFuture<'a, ()> {
        Box::pin(async move {
            match self.stop {
                Some(time) => tokio::time::sleep(time).await,
                None => std::future::pending().await,
            }
            push(&self.log, format!("shutdown {}", self.name));
        })
    }
}

#[tokio::test]
async fn modules_shut_down_at_the_same_time_within_one_budget() {
    let mut config = http_config();
    config.server.module_shutdown_timeout_secs = 2;
    let log: Log = Arc::default();
    let slow = |name, stop| Slow { name, stop, log: log.clone() };
    let one = Some(Duration::from_millis(1200));
    let server = NetBackendServer::new(config).module(slow("a", one)).module(slow("b", one)).module(slow("c", one)).module(slow("stuck", None));
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("addr");
    let (stop, stopped) = oneshot::channel::<()>();
    let running = tokio::spawn(server.serve_with_shutdown(listener, async move {
        let _ = stopped.await;
    }));
    wait_until_up(addr).await;
    let asked = Instant::now();
    let _ = stop.send(());
    let result = tokio::time::timeout(WAIT, running).await.expect("stopped").expect("task");
    assert!(result.is_ok(), "{result:?}");
    // One after the other: 3 × 1.2 s + the stuck module's 2 s; at the same time: the 2 s budget.
    let took = asked.elapsed();
    assert!(took >= Duration::from_secs(2) && took < Duration::from_millis(3500), "{took:?}");
    let mut done = lines(&log);
    done.sort();
    assert_eq!(done, ["shutdown a", "shutdown b", "shutdown c"]);
}

#[cfg(feature = "sqlite")]
#[tokio::test]
async fn migrate_on_start() {
    struct Notes;
    impl Module for Notes {
        fn name(&self) -> &'static str {
            "notes"
        }
        fn migrations(&self, _dialect: net_backend_server::Dialect) -> Vec<net_backend_server::Migration> {
            vec![net_backend_server::Migration::new(1, "create_notes", "CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT NOT NULL)")]
        }
    }
    let dir = common::temp_dir("serve-migrate");
    let url = format!("sqlite:{}", dir.join("game.db").display().to_string().replace('\\', "/"));
    let mut config = http_config();
    config.database.url = net_backend_server::SecretString::new(url.clone());
    config.database.migrate_on_start = true;
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("addr");
    let (stop, stopped) = oneshot::channel::<()>();
    let running = tokio::spawn(NetBackendServer::new(config.clone()).module(Notes).serve_with_shutdown(listener, async move {
        let _ = stopped.await;
    }));
    wait_until_up(addr).await;
    let _ = stop.send(());
    let result = tokio::time::timeout(WAIT, running).await.expect("stopped").expect("task");
    assert!(result.is_ok(), "{result:?}");
    let db = net_backend_server::Db::connect(&config.database).await.expect("connect");
    db.execute_script("INSERT INTO notes (id, body) VALUES (1, 'x')").await.expect("the table exists");
    db.close().await;
}

/// Without `migrate_on_start`, `serve` refuses to start while a migration is pending (the error
/// names it and says to run `migrate`); after `migrate` it serves.
#[cfg(feature = "sqlite")]
#[tokio::test]
async fn pending_migrations_stop_serve() {
    struct Notes;
    impl Module for Notes {
        fn name(&self) -> &'static str {
            "notes"
        }
        fn migrations(&self, _dialect: net_backend_server::Dialect) -> Vec<net_backend_server::Migration> {
            vec![net_backend_server::Migration::new(1, "create_notes", "CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT NOT NULL)")]
        }
    }
    let dir = common::temp_dir("serve-pending");
    let url = format!("sqlite:{}", dir.join("game.db").display().to_string().replace('\\', "/"));
    let mut config = http_config();
    config.database.url = net_backend_server::SecretString::new(url);
    config.database.migrate_on_start = false;
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let refused = tokio::time::timeout(WAIT, NetBackendServer::new(config.clone()).module(Notes).serve_with_shutdown(listener, std::future::pending()))
        .await
        .expect("refused in time");
    let message = refused.err().map(|e| e.to_string()).unwrap_or_default();
    assert!(message.contains("notes 0001_create_notes") && message.contains("`migrate`"), "{message}");
    let prepared = NetBackendServer::new(config.clone()).module(Notes).build().await.expect("build");
    prepared.migrate().await.expect("migrate");
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("addr");
    let (stop, stopped) = oneshot::channel::<()>();
    let running = tokio::spawn(prepared.serve_with_shutdown(listener, async move {
        let _ = stopped.await;
    }));
    wait_until_up(addr).await;
    let _ = stop.send(());
    let result = tokio::time::timeout(WAIT, running).await.expect("stopped").expect("task");
    assert!(result.is_ok(), "{result:?}");
}