I have a rust app using tokio::main where I use the tonic crate to service a gRPC server for incoming gRPC data, which is all in a tokio::spawn() thread. That thread receives the gRPC data, sends it through a tokio::mpsc::channel and then another tokio::spawn() thread with the tokio::mpsc::channel receiver awaits that data and stores it into an embedded rocksdb database. I need another thread that one by on in a FIFO style operation takes the most recent data stored in the database and reads it out and performs blocking operations on it.
Right now, I have it so the gRPC server code is in a tokio::spawn() and I can see it is successfully storing the data into the database. My question is, how do properly I add the synchronous operations of monitoring the most recent data stored in the database and perform synchronous blocking operations on it in the same app?
I have it working where the logic is like this below, but I am not sure if this is the best approach. It has a tokio spawn_blocking thread with a loop in it forever, but I am stuck on if this is bad practice or not.
tokio::task::spawn_blocking(move || {
loop {
// Read out of database when there is new data
// Perform other synchronous things..
}
}).await;
Any input or tips on how to handle infinite loop synchronous operations in a tokio app like this would be extremely helpful. Thank you!