pub struct ShutdownController { /* private fields */ }
Expand description

A ShutdownController is used to control the shutdown of an application.

ShutdownController::shutdown is used signal that the application is shutting down and should wait for all pending tasks to complete.

This is useful for things like web servers and database connections, etc where you want to allow all in-flight processing to complete before shutting down in order to maintain a consistent state.

Examples

use lib_wc::sync::ShutdownController;
use tokio::task::spawn;

#[tokio::main]
async fn main() {
    let shutdown = ShutdownController::new();
     
    let t = spawn({
        let mut shutdown_listener = shutdown.subscribe();
        assert!(!shutdown_listener.is_shutdown());
        async move {
            shutdown_listener.recv().await;
            assert!(shutdown_listener.is_shutdown());
        }
    });

    shutdown.shutdown().await;
}

Implementations§

source§

impl ShutdownController

source

pub fn new() -> Self

Create a new ShutdownController.

Examples
let shutdown = lib_wc::sync::ShutdownController::new();
source

pub fn subscribe(&self) -> ShutdownListener

Create a new ShutdownListener instance that can listen for the shutdown signal.

Examples
use lib_wc::sync::{ShutdownController, ShutdownListener};

let shutdown = ShutdownController::new();
let shutdown_listener = shutdown.subscribe();
source

pub async fn shutdown(self)

Begin shutting down and wait for all ShutdownListener instances to be dropped.

Examples
use std::time::Duration;
use tokio::time::interval;
use tokio::task::spawn;
use tokio::sync::{broadcast, mpsc};
use lib_wc::sync::{ShutdownController, ShutdownListener};

async fn task(mut shutdown: ShutdownListener) {
    let mut interval = interval(Duration::from_nanos(100));  
    while !shutdown.is_shutdown() {
       tokio::select! {
          _ = interval.tick() => {
            println!("tick");
         }
         _ = shutdown.recv() => {
           println!("shutdown");
           break;
         }
       }
   }
}   

#[tokio::main]
async fn main() {
 let shutdown = ShutdownController::new();

 // Spawn a task
 let t = spawn({
  let shutdown_listener = shutdown.subscribe();
  async move { task(shutdown_listener).await }
 });

// Wait for the task to finish
shutdown.shutdown().await;
}

Trait Implementations§

source§

impl Default for ShutdownController

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> Pointable for T

source§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V