AppSpindown

Struct AppSpindown 

Source
pub struct AppSpindown;
Expand description

A facade for interacting with the application’s global spindown registry.

Allows registering arbitrary workloads and later waiting for all registered workloads to signal their graceful completion (within a configurable timeout).

§Problem statement

Every application may choose to run asynchronous background tasks that hold some kind of resource. An example here would be a pool of database connections that is owned by a background task that lends access to the pool to any task that requests it. However, when the application is shut down, all background tasks are unceremoniously killed, which prevents proper clean-up, such as closing the database connections.

§Spindown

To solve the problem, this crate enables the following flow.

§Main thread

The main thread:

  • spawns background tasks,
  • waits until the global context is terminated(e.g., in a select block, while also waiting for the main logic to finish),
  • waits for all background tasks to signal completion,
  • returns from the main function.

§Background tasks

Meanwhile, each spawned background task:

  • registers with the global spindown registry,
  • also waits until the global context is terminated (e.g., in a select block, while also doing other work),
  • performs clean-up procedures (e.g., closes connections, etc.),
  • punches out the spindown token to signal completion.

Implementations§

Source§

impl AppSpindown

Source

pub fn register(name: impl AsRef<str>) -> AppSpindownToken

Informs the application’s global spindown registry that a workload with the given name (an arbitrary human-readable string) will need to be awaited during the application spindown, giving it some time to perform clean-up.

The returned AppSpindownToken must be used by the registering workload to signal back to the registry once it has gracefully completed.

Source

pub fn set_timeout_secs(timeout_secs: impl Into<u64>)

Allows customizing the spindown timeout for the global singleton registry. Importantly, this method must be called early on, before any interaction with the global spindown registry, such as registering a workload. If called later, this method will have no effect.

Source

pub async fn completed()

Collects all previously registered workloads, and then waits (within a timeout) for them to signal completion.

This function is destructive, as it consumes the internally stored list of workloads.

The spindown is performed in repeated cycles (within a single shared timeout). If new workloads are registered while previous ones are being spun down, a new cycle is initiated to wait for the next batch. This is repeated until no more registered workloads are found.

Importantly, this function does not signal to the workloads to begin their spindown. This is the job of the global AppContext.

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.

Source§

impl<T> Instrument for T

Source§

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

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

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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