Crates.io | meltdown |
lib.rs | meltdown |
version | 0.3.0 |
source | src |
created_at | 2021-11-21 23:14:38.959696 |
updated_at | 2024-11-03 01:42:04.622243 |
description | A lightweight service manager to help with graceful shutdown of asynchronous applications. |
homepage | |
repository | https://github.com/joshua-cooper/meltdown |
max_upload_size | |
id | 485493 |
size | 19,366 |
A lightweight service manager to help with graceful shutdown of asynchronous applications.
Meltdown provides a simple way to manage multiple asynchronous services and coordinate their graceful shutdown. Perfect for web servers, background workers, or any async application that needs clean shutdown handling.
Add this to your Cargo.toml
:
[dependencies]
meltdown = "0.3.0"
Basic usage example with tokio
and axum
:
use axum::{routing, Router};
use meltdown::{Meltdown, Token};
async fn api_service(token: Token) -> Result<(), Box<dyn std::error::Error>> {
let router = Router::new().route("/", routing::get("Hello!"));
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
axum::serve(listener, router)
.with_graceful_shutdown(token)
.await
.map_err(Into::into)
}
async fn signal_service(token: Token) -> Result<(), Box<dyn std::error::Error>> {
tokio::select! {
() = token => Ok(()),
result = tokio::signal::ctrl_c() => result.map_err(Into::into),
}
}
#[tokio::main]
async fn main() {
let mut meltdown = Meltdown::new()
.register(api_service)
.register(signal_service);
// Trigger shutdown when first service completes
if let Some(result) = meltdown.next().await {
println!("Got {result:?}, shutting down");
meltdown.trigger();
}
// Wait for remaining services
while let Some(result) = meltdown.next().await {
println!("Got {result:?}");
}
}
For detailed usage and examples, check out the documentation.
Licensed under the 0BSD license.