When doing server-side streaming in tonic, the generated async function is expected to return something which implements Stream
. If I construct my stream by taking a receiver and then calling .map(my_very_expensive_function)
on it, will my_very_expensive_function
run on a tokio worker thread? Or somewhere it’s safe to do blocking operations?
Eg.
use proto::MyService;
struct RpcService;
impl MyService for RpcService {
type MyFnStream = Pin<Box<dyn Stream<Item=Result<Bar, tonic::Status>> + Send>>
async fn my_fn(&self, request: tonic::Request<Foo>) -> Result<Response<Self::MyFnStream>, tonic::Status> {
let (sender, receiver) = tokio::sync::mpsc::channel();
register_sender(sender);
Box::new(ReceiverStream(receiver).map(my_very_expensive_function))
}
}
fn register_sender(sender: tokio::sync::mpsc::Sender<Foo>) {
...
}
fn my_very_expensive_function(foo: Foo) -> Bar {
... // This should not run in a tokio worker thread
}