mirror of
https://github.com/rwf2/Rocket.git
synced 2025-02-10 10:42:07 +00:00
Launch fairings are now fallible and take the place of attach fairings, but they are only run, as the name implies, at launch time. This is is a fundamental shift from eager execution of set-up routines, including the now defunct attach fairings, to lazy execution, precipitated by the transition to `async`. The previous functionality, while simple, caused grave issues: 1. A instance of 'Rocket' with async attach fairings requires an async runtime to be constructed. 2. The instance is accessible in non-async contexts. 3. The async attach fairings have no runtime in which to be run. Here's an example: ```rust let rocket = rocket::ignite() .attach(AttachFairing::from(|rocket| async { Ok(rocket.manage(load_from_network::<T>().await)) })); let state = rocket.state::<T>(); ``` This had no real meaning previously yet was accepted by running the attach fairing future in an isolated runtime. In isolation, this causes no issue, but when attach fairing futures share reactor state with other futures in Rocket, panics ensue. The new Rocket application lifecycle is this: * Build - A Rocket instance is constructed. No fairings are run. * Ignition - All launch fairings are run. * Liftoff - If all launch fairings succeeded, the server is started. New 'liftoff' fairings are run in this third phase.
62 lines
1.8 KiB
Rust
62 lines
1.8 KiB
Rust
#[macro_use] extern crate rocket;
|
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
use rocket::State;
|
|
use rocket::fairing::AdHoc;
|
|
use rocket::http::Method;
|
|
|
|
#[derive(Default)]
|
|
struct Counter {
|
|
attach: AtomicUsize,
|
|
get: AtomicUsize,
|
|
}
|
|
|
|
#[get("/")]
|
|
fn index(counter: State<'_, Counter>) -> String {
|
|
let attaches = counter.attach.load(Ordering::Relaxed);
|
|
let gets = counter.get.load(Ordering::Acquire);
|
|
format!("{}, {}", attaches, gets)
|
|
}
|
|
|
|
fn rocket() -> rocket::Rocket {
|
|
rocket::ignite()
|
|
.mount("/", routes![index])
|
|
.attach(AdHoc::on_launch("Outer", |rocket| async {
|
|
let counter = Counter::default();
|
|
counter.attach.fetch_add(1, Ordering::Relaxed);
|
|
let rocket = rocket.manage(counter)
|
|
.attach(AdHoc::on_request("Inner", |req, _| {
|
|
Box::pin(async move {
|
|
if req.method() == Method::Get {
|
|
let counter = req.guard::<State<'_, Counter>>()
|
|
.await.unwrap();
|
|
counter.get.fetch_add(1, Ordering::Release);
|
|
}
|
|
})
|
|
}));
|
|
|
|
Ok(rocket)
|
|
}))
|
|
}
|
|
|
|
mod nested_fairing_attaches_tests {
|
|
use super::*;
|
|
use rocket::local::blocking::Client;
|
|
|
|
#[test]
|
|
fn test_counts() {
|
|
let client = Client::debug(rocket()).unwrap();
|
|
let response = client.get("/").dispatch();
|
|
assert_eq!(response.into_string(), Some("1, 1".into()));
|
|
|
|
let response = client.get("/").dispatch();
|
|
assert_eq!(response.into_string(), Some("1, 2".into()));
|
|
|
|
client.get("/").dispatch();
|
|
client.get("/").dispatch();
|
|
let response = client.get("/").dispatch();
|
|
assert_eq!(response.into_string(), Some("1, 5".into()));
|
|
}
|
|
}
|