Struct bastion::distributor::Distributor [−][src]
pub struct Distributor(_);
Expand description
The Distributor
is the main message passing mechanism we will use.
it provides methods that will allow us to send messages
and add/remove actors to the Distribution list
Implementations
Create a new distributor to send messages to
Example
let distributor = Distributor::named("my target group");
// distributor is now ready to use
Ask a question to a recipient attached to the Distributor
and wait for a reply.
This can be achieved manually using a MessageHandler
and ask_one
.
Ask a question to a recipient attached to the Distributor
// attach a named distributor to the children
children
.with_distributor(Distributor::named("my distributor"))
.with_exec(|ctx: BastionContext| {
async move {
loop {
// The message handler needs an `on_question` section
// that matches the `question` you're going to send,
// and that will reply with the Type the request expects.
// In our example, we ask a `&str` question, and expect a `bool` reply.
MessageHandler::new(ctx.recv().await?)
.on_question(|message: &str, sender| {
if message == "is it raining today?" {
sender.reply(true).unwrap();
}
});
}
Ok(())
}
})
let distributor = Distributor::named("my distributor");
let reply: Result<String, SendError> = distributor
.request("is it raining today?")
.await
.expect("couldn't receive reply");
Ask a question to a recipient attached to the Distributor
and wait for a reply.
this is the sync variant of the request
function, backed by a futures::channel::oneshot
Example
// attach a named distributor to the children
children
.with_distributor(Distributor::named("my distributor"))
.with_exec(|ctx: BastionContext| {
async move {
loop {
// The message handler needs an `on_question` section
// that matches the `question` you're going to send,
// and that will reply with the Type the request expects.
// In our example, we ask a `&str` question, and expect a `bool` reply.
MessageHandler::new(ctx.recv().await?)
.on_question(|message: &str, sender| {
if message == "is it raining today?" {
sender.reply(true).unwrap();
}
});
}
Ok(())
}
})
let distributor = Distributor::named("my distributor");
let reply: Result<bool, SendError> = distributor
.request_sync("is it raining today?")
.recv()
.expect("couldn't receive reply"); // Ok(true)
Ask a question to a recipient attached to the Distributor
Example
children
.with_redundancy(1)
.with_distributor(Distributor::named("my distributor"))
.with_exec(|ctx: BastionContext| { // ...
})
let distributor = Distributor::named("my distributor");
let answer: Answer = distributor.ask_one("hello?").expect("couldn't send question");
Ask a question to all recipients attached to the Distributor
Requires a Message
that implements Clone
. (it will be cloned and passed to each recipient)
Example
let distributor = Distributor::named("my distributor");
let answer: Vec<Answer> = distributor.ask_everyone("hello?".to_string()).expect("couldn't send question");
Send a Message to a recipient attached to the Distributor
Example
let distributor = Distributor::named("my distributor");
let answer: () = distributor.tell_one("hello?").expect("couldn't send question");
Send a Message to each recipient attached to the Distributor
Requires a Message
that implements Clone
. (it will be cloned and passed to each recipient)
Example
let distributor = Distributor::named("my distributor");
let answer: () = distributor.tell_one("hello?").expect("couldn't send question");
subscribe a ChildRef
to the named Distributor
let child_ref = children.elems()[0].clone();
let distributor = Distributor::named("my distributor");
// child_ref will now be elligible to receive messages dispatched through distributor
distributor.subscribe(child_ref).expect("couldn't subscribe child to distributor");
unsubscribe a ChildRef
to the named Distributor
let child_ref = children.elems()[0].clone();
let distributor = Distributor::named("my distributor");
// child_ref will not receive messages dispatched through the distributor anymore
distributor.unsubscribe(child_ref).expect("couldn't unsubscribe child to distributor");
Trait Implementations
Performs the conversion.
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
This method tests for !=
.
Auto Trait Implementations
impl RefUnwindSafe for Distributor
impl Send for Distributor
impl Sync for Distributor
impl Unpin for Distributor
impl UnwindSafe for Distributor
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more