cetra3 / tmq

Rust ZeroMQ bindings for Tokio
151 stars 28 forks source link

Feature : `recv` and `send` methods for the request_reply sockets that takes a mutable reference of the `RequestReceiver` and the `RequestSender` #41

Open eldoccc opened 1 year ago

eldoccc commented 1 year ago

I believe recv(mut self) and send(mut self, mut msg: Multipart) in the request_reply sockets were designed to take self instead of &mut self so you cannot make multiple send before getting the reply and rcv before sending the reply. However in the case of tokio::select! inside of a loop :

let (tx,rx) = mpsc::channel(64);

loop {
            tokio::select! {
                _  = rx.recv() => {}
                _  = request_receiver.recv() => {}
            }
        }

because recv()consumes self, the compiler won't allow for it to be used in this case.

A barbaric solution would be to spawn a task containing the recv() and use channels to communicate back and forth with the select!, however i'd rather have an appropriate method for this case.

Without wanting to alter the current structure, a quick fix for me was the create a struct RequestReply contaning both the send(&mut self, mut msg: Multipart) and the recv(&mut self)methods in the cloned repo.

cetra3 commented 1 year ago

Yeah I'm not sure whether it's cancel safe at the moment, which is what is required for select!, but definitely interested if there's a solution here.