Key Stream
I needed a small async library to send and receive messages within a single process by key, but I couldn't find one that met all of the following criteria:
- Asynchronous
- Allows subscription and publishing by key
- Avoids copying every message to every receiver and then filter
- Lightweight
So, I decided to create my own.
What This Library Is Not
- Not for networking: messages are sent only within a single process.
- No persistence: if there are no subscribers, messages are not saved. It is expected that you obtain a snapshot elsewhere and subscribe only for updates. If no subscribers exist, it means no one is interested at the moment and a full snapshot can be retrieved later. It also means that the key and associated sender are deleted, so memory will not grow indefinitely.
Usage
Simple Usage
let key_stream = new;
let sender = key_stream.sender;
let mut receiver = sender.subscribe.await;
sender
.send
.await
.expect;
assert_eq!;
Receiver Dropping
If all receivers are dropped, the related key is also removed.
Messages sent to it will be ignored.
The deletion of keys is implemented as a background task in KeyStream.
let key_stream = new;
let sender = key_stream.sender;
let receiver = sender.subscribe.await;
assert_eq!;
drop;
// give the key drop task a chance to run
yield_now.await;
let result = sender
.send
.await
.unwrap;
assert_eq!;
assert_eq!;