How to save the data when the tonic is closed! #725
-
The user declared variable is moved to the structure, and the Route does not provide a way to access it, is there any good way ?
I found following code in tonic/src/transport/server/mod.rs
|
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Jul 15, 2021
Replies: 1 comment 5 replies
-
The data is in an I haven't tested this code but should work #[tokio::main]
async fn main() {
// pull this out into a variable so it can be shared between tasks
let data = Arc::new(Mutex::new(vec![]));
let game = Game { data: Arc::clone(&data) };
let mut route = Server::builder().add_service(GameServer::new(game));
// register callback when shutdown
route.serve_with_shutdown(addr, async move {
tokio::signal::ctrl_c().await.unwrap();
}).await.unwrap();
// somehow actually save the data
let data = data.lock();
println!("saving {:?}", data);
} |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
davidpdrsn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The data is in an
Arc
so you can share it between theGameServer
and whatever code runs after the server has been shutdown.I haven't tested this code but should work