bastion-rs / bastion

Highly-available Distributed Fault-tolerant Runtime
https://www.bastion-rs.com
Apache License 2.0
2.78k stars 101 forks source link

proposal: distributor request fn #321

Closed o0Ignition0o closed 3 years ago

o0Ignition0o commented 3 years ago

Proposal: Distributor request fn.

as shown here making a whole request can sometimes be a bit convoluted. the Distributor::request() function aims to be syntactic sugar to wrap this.

let answer = probes
    .ask_one(url.clone())
    .expect("couldn't send message to probes");

// Handle a reply
MessageHandler::new(answer.await.expect("couldn't get an answer")).on_tell(
    |probe_result: ProbeRunStatus, _| {
        info!("probe is done: {:?}", probe_result);
    },
);

will then become

let probe_run_status: Result<ProbeRunStatus, SendError> = probes
        .request(url.clone())
        .await
        .expect("couldn't receive reply");
 info!("probe is done: {:?}", probe_result);

It uses a channel() behind the scene, as shown in the signature:

pub fn request<R: Message>(
    &self,
    question: impl Message
) -> Receiver<Result<R, SendError>>

Here's what the documentation looks like so far:

image

there's also a request_sync variant which is backed by a mpsc::channel(), and can be called like this:

let reply: Result<ProbeRunStatus, SendError> = distributor
   .request_sync(url.clone())
   .recv()
   .expect("couldn't receive reply");
 info!("probe is done: {:?}", probe_result);
Checklist