Struct aldrin_broker::Broker
source · pub struct Broker { /* private fields */ }
Expand description
Aldrin broker.
This is the central message broker present in every Aldrin bus. After creating a Broker
with
new
, it must be turned into future with run
and then polled to
completion.
BrokerHandle
s are used to interact with a running Broker
and can be acquired
with the handle
method. Through a BrokerHandle
, you can add new
connections to the Broker
as well as shut it down again.
The Broker
will automatically shut down, when there are no active connections and the last
BrokerHandle
has been dropped.
Examples
use aldrin_broker::Broker;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new broker:
let broker = Broker::new();
// Acquire a BrokerHandle:
let mut handle = broker.handle().clone();
// Run the broker:
let join = tokio::spawn(broker.run());
// Add connections to the broker:
// ...
// Shut down the broker:
handle.shutdown().await;
join.await?;
Ok(())
}
Implementations§
source§impl Broker
impl Broker
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new broker.
After creating a Broker
, it must be turned into a future with run
and
polled to completion.
sourcepub fn handle(&self) -> &BrokerHandle
pub fn handle(&self) -> &BrokerHandle
Returns a reference to the broker handle.
It is important to acquire at least one BrokerHandle
before run
ning the
Broker
. BrokerHandle
s are the only way to add new connections to the Broker
.
Note also, that this method returns only a reference. However, BrokerHandle
s are cheap to
clone
.
sourcepub async fn run(self)
pub async fn run(self)
Runs the broker.
This is a long running method, that will only return when explicitly shut down or when there
are no connected clients and the last BrokerHandle
has been dropped.
Make sure to acquire a BrokerHandle
before running the Broker
.