I am using the grpcio
crate to set up a grpc server. One of the calls I support is a long-lived bidirectional stream. I would like my main thread to be able to broadcast a message through this bidirectional stream to all clients currently connected to the server. I would also like my main thread to get messages from any of these "handler" threads (though for my use case I don't need to know which client the request came from).
I've included the approach I'm currently using below. As you can see, my previous thought was to use futures::sync::mpsc::channel
s, but I don't think this approach will work (certainly not for broadcasting to multiple clients), and I am open to other approaches to having my main thread communicate with my "handler" threads. Thanks for your help!
foo.proto
syntax = "proto3";
message FooInbound {
uint32 val = 1;
}
message FooOutbound {
uint32 val = 1;
}
service FooService {
rpc BidiFoo(stream FooInbound) returns (stream FooOutbound);
}
lib.rs
extern crate grpcio;
use foo::*
use foo_grpc::*;
use grpcio::*;
#[derive(Clone, Debug)]
pub struct InboundMsg {
val: u32,
}
#[derive(Clone, Debug)]
pub struct OutboundMsg {
val: u32,
}
#[derive(Clone)]
pub struct FooServiceImpl {
rx: Arc<Mutex<Receiver<OutboundMsg>>>,
tx: Sender<InboundMsg>,
}
impl FooService for FooServiceImpl {
fn bidi_foo(
&mut self,
ctx: RpcContext,
stream: RequestStream<FooInbound>,
sink: DuplexSink<FooOutbound>,
) {
// Here I would like to put code that will transform `FooInbound` to `InboundMsg`
// and pass it on to my main thread.
// I also want to listen for `OutboundMsg`s from my main thread and transform them
// to `FooOutbound` before sending them to the client.
}
}