2020-12-17 17:35:11 +04:00
|
|
|
<div align="center">
|
|
|
|
<h1>Message Bus</h1>
|
|
|
|
<p>
|
|
|
|
<strong>Async Message Bus for Rust</strong>
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
Inspired by Actix
|
|
|
|
|
|
|
|
### Basics
|
|
|
|
1. Can deliver messages between actors using receivers (usually a queue implementations)
|
|
|
|
2. Messages distincts and delivers by TypeId
|
2021-07-02 20:21:53 +04:00
|
|
|
3. Messages delivers ether in a broadcast fashion to many receivers (Cloned) or addressed by recevier id, balanced (depends on queue load) or random
|
2020-12-17 17:35:11 +04:00
|
|
|
4. There are different kind of receivers implemented:
|
2021-06-24 14:14:53 +04:00
|
|
|
- BufferUnordered Receiver (sync and async)
|
|
|
|
- Synchronized (sync and async)
|
|
|
|
- BatchedBufferUnordered Receiver (sync and async)
|
|
|
|
- BatchedSynchronized (sync and async)
|
|
|
|
5. Request/response api. There is an example is [demo_req_resp.rs](./examples/demo_req_resp.rs)
|
|
|
|
|
|
|
|
Here are the list of implmented handler kinds:
|
2020-12-17 17:35:11 +04:00
|
|
|
```rust
|
|
|
|
pub trait Handler<M: Message>: Send + Sync {
|
2021-07-02 20:21:53 +04:00
|
|
|
type Error: StdSyncSendError;
|
2021-06-24 14:14:53 +04:00
|
|
|
type Response: Message;
|
|
|
|
|
|
|
|
fn handle(&self, msg: M, bus: &Bus) -> Result<Self::Response, Self::Error>;
|
|
|
|
fn sync(&self, _bus: &Bus) -> Result<(), Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-17 17:35:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait AsyncHandler<M: Message>: Send + Sync {
|
2021-07-02 20:21:53 +04:00
|
|
|
type Error: StdSyncSendError;
|
2021-06-24 14:14:53 +04:00
|
|
|
type Response: Message;
|
|
|
|
|
|
|
|
async fn handle(&self, msg: M, bus: &Bus) -> Result<Self::Response, Self::Error>;
|
|
|
|
async fn sync(&self, _bus: &Bus) -> Result<(), Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-17 17:35:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait SynchronizedHandler<M: Message>: Send {
|
2021-07-02 20:21:53 +04:00
|
|
|
type Error: StdSyncSendError;
|
2021-06-24 14:14:53 +04:00
|
|
|
type Response: Message;
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: M, bus: &Bus) -> Result<Self::Response, Self::Error>;
|
|
|
|
fn sync(&mut self, _bus: &Bus) -> Result<(), Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-17 17:35:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait AsyncSynchronizedHandler<M: Message>: Send {
|
2021-07-02 20:21:53 +04:00
|
|
|
type Error: StdSyncSendError;
|
2021-06-24 14:14:53 +04:00
|
|
|
type Response: Message;
|
|
|
|
|
|
|
|
async fn handle(&mut self, msg: M, bus: &Bus) -> Result<Self::Response, Self::Error>;
|
|
|
|
async fn sync(&mut self, _bus: &Bus) -> Result<(), Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait BatchHandler<M: Message>: Send + Sync {
|
2021-07-02 20:21:53 +04:00
|
|
|
type Error: StdSyncSendError + Clone;
|
2021-06-24 14:14:53 +04:00
|
|
|
type Response: Message;
|
2021-07-02 20:21:53 +04:00
|
|
|
type InBatch: FromIterator<M> + Send;
|
|
|
|
type OutBatch: IntoIterator<Item = Self::Response> + Send;
|
2021-06-24 14:14:53 +04:00
|
|
|
|
2021-07-02 20:21:53 +04:00
|
|
|
fn handle(&self, msg: Self::InBatch, bus: &Bus) -> Result<Self::OutBatch, Self::Error>;
|
2021-06-24 14:14:53 +04:00
|
|
|
fn sync(&self, _bus: &Bus) -> Result<(), Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait AsyncBatchHandler<M: Message>: Send + Sync {
|
2021-07-02 20:21:53 +04:00
|
|
|
type Error: StdSyncSendError + Clone;
|
2021-06-24 14:14:53 +04:00
|
|
|
type Response: Message;
|
2021-07-02 20:21:53 +04:00
|
|
|
type InBatch: FromIterator<M> + Send;
|
|
|
|
type OutBatch: IntoIterator<Item = Self::Response> + Send;
|
2021-06-24 14:14:53 +04:00
|
|
|
|
2021-07-02 20:21:53 +04:00
|
|
|
async fn handle(&self, msg: Self::InBatch, bus: &Bus) -> Result<Self::OutBatch, Self::Error>;
|
2021-06-24 14:14:53 +04:00
|
|
|
async fn sync(&self, _bus: &Bus) -> Result<(), Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-17 17:35:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait BatchSynchronizedHandler<M: Message>: Send {
|
2021-07-02 20:21:53 +04:00
|
|
|
type Error: StdSyncSendError + Clone;
|
2021-06-24 14:14:53 +04:00
|
|
|
type Response: Message;
|
2021-07-02 20:21:53 +04:00
|
|
|
type InBatch: FromIterator<M> + Send;
|
|
|
|
type OutBatch: IntoIterator<Item = Self::Response> + Send;
|
2021-06-24 14:14:53 +04:00
|
|
|
|
2021-07-02 20:21:53 +04:00
|
|
|
fn handle(&mut self, msg: Self::InBatch, bus: &Bus) -> Result<Self::OutBatch, Self::Error>;
|
2021-06-24 14:14:53 +04:00
|
|
|
fn sync(&mut self, _bus: &Bus) -> Result<(), Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-17 17:35:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait AsyncBatchSynchronizedHandler<M: Message>: Send {
|
2021-07-02 20:21:53 +04:00
|
|
|
type Error: StdSyncSendError + Clone;
|
2021-06-24 14:14:53 +04:00
|
|
|
type Response: Message;
|
2021-07-02 20:21:53 +04:00
|
|
|
type InBatch: FromIterator<M> + Send;
|
|
|
|
type OutBatch: IntoIterator<Item = Self::Response> + Send;
|
2021-06-24 14:14:53 +04:00
|
|
|
|
2021-07-02 20:21:53 +04:00
|
|
|
async fn handle(&mut self, msg: Self::InBatch, bus: &Bus) -> Result<Self::OutBatch, Self::Error>;
|
2021-06-24 14:14:53 +04:00
|
|
|
async fn sync(&mut self, _bus: &Bus) -> Result<(), Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-17 17:35:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
```
|
2021-06-24 14:14:53 +04:00
|
|
|
6. Implemented handler kinds:
|
|
|
|
1. No Synchronization needed (Handler implements `Send` and `Sync`)
|
|
|
|
* Not batched operations
|
2020-12-17 17:35:11 +04:00
|
|
|
- sync (spawn_blocking)
|
|
|
|
- async (spawn)
|
|
|
|
* Batched
|
|
|
|
- sync (spawn_blocking)
|
|
|
|
- async (spawn)
|
2021-06-24 14:14:53 +04:00
|
|
|
2. Synchronization needed (Handler implements only `Send` but not implements `Sync`)
|
|
|
|
* Not batched operations
|
2020-12-17 17:35:11 +04:00
|
|
|
- sync (spawn_blocking)
|
|
|
|
- async (spawn)
|
2021-06-24 14:14:53 +04:00
|
|
|
* Batched
|
2020-12-17 17:35:11 +04:00
|
|
|
- sync (spawn_blocking)
|
|
|
|
- async (spawn)
|
2021-06-24 14:14:53 +04:00
|
|
|
|
|
|
|
7. Not yet implemented handler kinds:
|
|
|
|
1. Synchronization needed and thread dedicated (Handler is `!Sync` and `!Send`)
|
2020-12-17 17:35:11 +04:00
|
|
|
* Not batched operations
|
|
|
|
- sync (spawn_blocking)
|
|
|
|
- async (spawn)
|
|
|
|
* Batched
|
|
|
|
- sync (spawn_blocking)
|
|
|
|
- async (spawn)
|
|
|
|
|
2021-06-24 14:14:53 +04:00
|
|
|
8. Example:
|
2020-12-17 17:35:11 +04:00
|
|
|
```rust
|
2021-06-24 14:14:53 +04:00
|
|
|
use messagebus::{error::Error, receivers, AsyncHandler, Bus};
|
2020-12-17 17:35:11 +04:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
|
|
struct TmpReceiver;
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl AsyncHandler<i32> for TmpReceiver {
|
2021-06-24 14:14:53 +04:00
|
|
|
type Error = Error;
|
|
|
|
type Response = ();
|
|
|
|
|
|
|
|
async fn handle(&self, msg: i32, bus: &Bus) -> Result<Self::Response, Self::Error> {
|
2020-12-17 17:35:11 +04:00
|
|
|
println!("---> i32 {}", msg);
|
|
|
|
|
|
|
|
bus.send(2i64).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl AsyncHandler<i64> for TmpReceiver {
|
2021-06-24 14:14:53 +04:00
|
|
|
type Error = Error;
|
|
|
|
type Response = ();
|
|
|
|
|
|
|
|
async fn handle(&self, msg: i64, _bus: &Bus) -> Result<Self::Response, Self::Error> {
|
2020-12-17 17:35:11 +04:00
|
|
|
println!("---> i64 {}", msg);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let (b, poller) = Bus::build()
|
|
|
|
.register(TmpReceiver)
|
2021-06-24 14:14:53 +04:00
|
|
|
.subscribe::<i32, receivers::BufferUnorderedAsync<_>, _, _>(8, Default::default())
|
|
|
|
.subscribe::<i64, receivers::BufferUnorderedAsync<_>, _, _>(8, Default::default())
|
2020-12-17 17:35:11 +04:00
|
|
|
.done()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
b.send(1i32).await.unwrap();
|
2021-06-24 14:14:53 +04:00
|
|
|
|
|
|
|
println!("flush");
|
|
|
|
b.flush().await;
|
|
|
|
|
|
|
|
println!("close");
|
|
|
|
b.close().await;
|
|
|
|
|
|
|
|
println!("closed");
|
|
|
|
|
|
|
|
poller.await;
|
|
|
|
println!("[done]");
|
2020-12-17 17:35:11 +04:00
|
|
|
}
|
|
|
|
```
|