Skip to main content

ShutdownController

Struct ShutdownController 

Source
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: CancellationToken

Parent cancellation token. Cancelled by shutdown_local.

Implementations§

Source§

impl ShutdownController

Source

pub fn new(gate: CommandGate) -> Self

Creates a controller that owns the connector’s command gate.

Source

pub fn child_token(&self) -> CancellationToken

A token for one background task. It is cancelled when the controller shuts down.

Source

pub fn gate(&self) -> &CommandGate

The command gate, for checking commands.

Source

pub fn gate_mut(&mut self) -> &mut CommandGate

The command gate, for marking it reconciled or degraded.

Source

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.

  1. Closes the command gate: new commands fail with [brokapi_core::Error::ShuttingDown].
  2. In [ShutdownMode::Graceful] calls unsubscribe once. It returns how many subscriptions were closed and how many failed. In [ShutdownMode::Immediate] it is not called and both counts are zero.
  3. Moves in-flight orders to Unknown (see OrderRuntime::abandon_inflight).
  4. 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.

Source

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.

Trait Implementations§

Source§

impl Debug for ShutdownController

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more