1

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.

Thomas
  • 4,208
  • 2
  • 29
  • 31
  • Don't `await` after the call to `stream_something`, instead send the other RPC, but don't `await` that either. Instead use `join` to `await` both of them simultaneously. (or `spawn` a task to process your stream and send the RPC from the main task). – Jmb Jun 02 '23 at 07:58
  • The join solution is also a race condition. Your second solution is what I meant by wrapping the whole thing, sending over a channel, that I hope would not be necessary. – Thomas Jun 03 '23 at 09:27

1 Answers1

0

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

smfx
  • 1
  • 3
  • 1
    A link-only solution is not encouraged on StackOverflow. Please try to describe the solution or quote from your source to make a self-contained answer. – soundflix Jul 25 '23 at 20:43
  • @soundflix Thanks for pointing out my mistake! It's hard for me to provide sample code, as my solution only fits if there is a way to change the server (I can only show you a piece of python server code). I've attached a link to code that may be useful if it's not possible to change the server, but I haven't tried it myself. I hope my answer is a little better now, thanks to you! – smfx Jul 26 '23 at 07:23