As a first trial project for Rust, I wanted to implement a (simple) form of a network service used at my workplace. It is basically a key-value store for multiple clients.
Wanted functionality: many clients can connect to the server, which stores key-value pairs. Any client can store a new entry, and if the value is new or has changed, all other connected clients are notified. A client can also explicitly ask for a certain key.
The basic code structure I wanted to have is to have a server thread (the main thread), and two threads for each connection: one handles reads from the client, and one writes back replies and notifications. I eventually managed to get request-reply on a single connection working with a Channel (read thread -> write thread) per connection, and a shared DB struct that contains the key-value map and is wrapped in an
Arc<Mutex<>> to be able to access it from any thread.
But I’m not sure how to implement the notify feature. It requires that one thread can send messages to all other connections, which means that it has to somehow have either a a reference to all other connection handlers, or to the server (which then is a cyclic reference, that I think is a bad idea).
Another idea would be to have more channels, but since channels apparently cannot be shared between threads I have to create N^2 channels, where N is the number of connections…
Anybody have a good idea how to restructure my objects to get rid of this dilemma? Thanks!