pub struct ShutdownController {
pub root: CancellationToken,
/* private fields */
}Expand description
Shutdown coordinator for one connector instance: it closes the command gate, gives up waiting for in-flight commands and cancels the background tasks.
The controller does no network I/O. The connector supplies the work that needs the network (the
unsubscribe calls) as a closure. Orders at the broker are never cancelled by a shutdown: only
the local wait for an answer is given up (orders in InFlight become Unknown).
§Examples
use std::time::{Duration, Instant};
use brokapi_core::{ClientOrderId, Error, ShutdownMode};
use brokapi_runtime::{CommandGate, OrderRuntime, ShutdownController};
let mut gate = CommandGate::new();
gate.mark_reconciled();
let mut ctl = ShutdownController::new(gate);
let task_token = ctl.child_token();
let mut orders = OrderRuntime::new();
let now = Instant::now();
let cid = ClientOrderId::new_v4();
orders.register_place(cid, now);
// Two subscriptions were closed, none failed.
let report = ctl.shutdown_local(
ShutdownMode::Graceful { deadline: Duration::from_secs(5) },
now,
&mut orders,
|| (2, 0),
);
assert_eq!((report.unsubscribed, report.unsubscribe_failed), (2, 0));
assert_eq!(report.inflight_abandoned, vec![cid]);
assert!(task_token.is_cancelled());
assert!(matches!(ctl.gate().check_command(), Err(Error::ShuttingDown)));Fields§
§root: CancellationTokenParent cancellation token. Cancelled by shutdown_local.
Implementations§
Source§impl ShutdownController
impl ShutdownController
Sourcepub fn new(gate: CommandGate) -> Self
pub fn new(gate: CommandGate) -> Self
Creates a controller that owns the connector’s command gate.
Sourcepub fn child_token(&self) -> CancellationToken
pub fn child_token(&self) -> CancellationToken
A token for one background task. It is cancelled when the controller shuts down.
Sourcepub fn gate(&self) -> &CommandGate
pub fn gate(&self) -> &CommandGate
The command gate, for checking commands.
Sourcepub fn gate_mut(&mut self) -> &mut CommandGate
pub fn gate_mut(&mut self) -> &mut CommandGate
The command gate, for marking it reconciled or degraded.
Sourcepub fn shutdown_local(
&mut self,
mode: ShutdownMode,
now: Instant,
orders: &mut OrderRuntime,
unsubscribe: impl FnMut() -> (usize, usize),
) -> ShutdownReport
pub fn shutdown_local( &mut self, mode: ShutdownMode, now: Instant, orders: &mut OrderRuntime, unsubscribe: impl FnMut() -> (usize, usize), ) -> ShutdownReport
Runs the local shutdown steps in order and returns a report.
- Closes the command gate: new commands fail with [
brokapi_core::Error::ShuttingDown]. - In [
ShutdownMode::Graceful] callsunsubscribeonce. It returns how many subscriptions were closed and how many failed. In [ShutdownMode::Immediate] it is not called and both counts are zero. - Moves in-flight orders to
Unknown(seeOrderRuntime::abandon_inflight). - Cancels the root token, which stops every task made with
child_token.
The deadline of Graceful is not applied here. This method runs to completion. Use
wait_graceful_deadline to wait for it. Calling the method
twice is harmless: the second report has no abandoned orders.
Sourcepub async fn wait_graceful_deadline(&self, mode: ShutdownMode)
pub async fn wait_graceful_deadline(&self, mode: ShutdownMode)
Sleeps for the deadline of a Graceful mode, and returns at once for Immediate.
It always sleeps the full deadline. It does not return early when the work is done, so use it only to bound a wait together with your own completion signal.