tonic icon indicating copy to clipboard operation
tonic copied to clipboard

Use a `Sink` argument instead of returning a `Stream` for response streaming

Open chebbyChefNEQ opened this issue 11 months ago • 1 comments

Feature Request

Love the project! Just one request on response streaming.

Most (because I haven't used all of them) gRPC implementation passes a sink/writer handle into the handler stub. In the stub implementation, users may call sink.send(response) or write.write(response) to stream data to the client. However, tonic makes the handler return a Resonse<RPCStream> type.

This has two issues

  1. This usually leaks tokio::spawn handles; or that user needs to build special apparatus to keep track of these handles.
  2. We exit Layers before stream is completed

Crates

I think just tonic?

Motivation

  1. stream back from inside the handler, instead of a stray tokio task
  2. able to have more control over streaming response in Layers. -- concurrent stream limits, response coalescing, etc.

Proposal

TBD

Alternatives

  • maybe tonic runtime can take a join handle in the handler return value and wait for the task to finish before exiting the layers? Less powerful then Sink interface but solves the control issue in Layer

chebbyChefNEQ avatar Mar 13 '24 21:03 chebbyChefNEQ

The returned stream type is generic, that means that it can be whatever you want. One thing that you can do is move the join handles into the returned stream and make sure cancellation occurs on drop. The other note is that you do not need to ever be doing the spawn directly, but rather can move the logic to a returned stream type -- i.e. you don't need to use something like ReceiverStream.

I think the stream limiting point is the most legit here.

ajwerner avatar Jul 13 '24 18:07 ajwerner