sigterm 0.3.10

Signal-aware async control and cancellation primitives for Tokio.
Documentation
/* examples/cancellation.rs */

//! Example of using `CancellationToken` with parent-child relationships.

use sigterm::CancellationToken;
use std::time::Duration;
use tokio::time::sleep;

#[tokio::main]
async fn main() {
	let root_token = CancellationToken::new();

	// Spawn a "service" with its own child token
	let service_token = root_token.child_token();
	tokio::spawn(async move {
		println!("Service started...");

		// Spawn a "request" handler as a child of the service
		let request_token = service_token.child_token();
		tokio::spawn(async move {
			println!("  Request handler started...");
			request_token.cancelled().await;
			println!("  Request handler stopped.");
		});

		service_token.cancelled().await;
		println!("Service stopped.");
	});

	sleep(Duration::from_secs(1)).await;

	println!("Cancelling root token...");
	root_token.cancel();

	// Wait for tasks to clean up
	sleep(Duration::from_millis(100)).await;
	println!("Main exited.");
}