How to implement a server with async queue

Hi folks,

I'm trying to build my first Rust project and got stuck with this issue.
Basically I have a singleton server instance (implemented as a struct) which has unique access to some external IO device. There are multiple clients in different threads submitting request to the IO device (through the server) and waiting for responses.

Ideally I see this as the server having a single queue where clients would use and server taking requests from the queue one-by-one and executing them. Clients upon submitting a request would get back a Future which resolves once there's a response.

server struct is not thread safe so I can't just pass it around in a mutex and this is where my knowledge of Rust ecosystem/idioms ends. I'd really appreciate any hints/links/examples of how something like the above can be implemented.

Such things are usually solved using channels. You'd have your server on its own thread, listening for requests from a channel, and execute them one by one. Channels are uni-directional, so it's up to you how you send the response back. The request may contain another channel, or sink, or just be a callback function.

Thanks kornel!
Actually, how to communicate back the response is the main issue for me atm.
Especially it is unclear how to do this in the async/await style, and if it makes sense to do it this way at all.

The simplest thing to do is to expand on what @kornel said by using one queue in each direction, one for requests and one for responses.

If you want to send a message of type M with a response of type R, then your message should be a (M, Sender<R>) where the Sender is an oneshot channel. This way the receiver can respond on the included channel.

Thanks, Alice! The oneshot Sender looks like what I need.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.