Is there a way to get Rust tonic gRPC client streams to start without waiting for the first message?

670 views Asked by At

It seems that the following code blocks until the first streamed object arrives:

let mut stream = client
        .stream_something(StreamRequest {})
        .await
        .unwrap()
        .into_inner();

Specifically I want to start the stream, and then send other RPCs that may trigger a state change that sends a message on the stream.

But I can't start the stream first, because that can block forever. And I don't want to send the other RPC first, because then I may miss the streamed update triggered by my other RPC.

I hope that I don't have to resort to wrapping the whole thing, and provide a new interface such as mpsc.

1

There are 1 answers

2
smfx On

I was in a similar situation, but we had access to the server.

I was considering two options:

  1. Modify the server (written in python) to give a 200 response (in my case, just an empty message).

  2. Spawn a separate task using channels so that it waits for a response to the request.

I chose the first option. I haven't tried the second one, but here's a piece of code that might be useful: https://gist.github.com/kyle-mccarthy/73ab6c78e6d3bf0819fc7c00b90161f4