Skip to main content

command

Function command 

pub async fn command<T>(
    timeout: Duration,
    fut: impl Future<Output = Result<Response<T>, Status>>,
) -> CommandOutcome<T>
Expand description

Runs a trading command (place, cancel, replace) under a deadline.

Waits for fut at most timeout. Statuses that prove the broker did not act become CommandOutcome::Refused (see map_command_status). A timeout, a broken connection or a server fault is CommandOutcome::Unknown: the broker may have acted. The caller must never repeat the command after Unknown, because a repeated place could create a second order.

§Examples

use std::time::Duration;
use brokapi_tbank::util::{command, CommandOutcome};
use tonic::{Response, Status};

let done = command(Duration::from_secs(1), async { Ok::<_, Status>(Response::new(1)) }).await;
assert!(matches!(done, CommandOutcome::Done(1)));

let refused = command(Duration::from_secs(1), async {
    Err::<Response<()>, _>(Status::invalid_argument("30042"))
})
.await;
assert!(matches!(refused, CommandOutcome::Refused(..)));

// A fault after sending hides what the broker did.
let lost = command(Duration::from_secs(1), async { Err::<Response<()>, _>(Status::unavailable("reset")) }).await;
assert!(matches!(lost, CommandOutcome::Unknown));