tokio-futures-respawn

Crates.iotokio-futures-respawn
lib.rstokio-futures-respawn
version0.1.3
sourcesrc
created_at2023-04-21 20:46:55.591855
updated_at2023-04-21 21:45:15.965069
descriptionUtility function to respawn failed long running tasks
homepage
repository
max_upload_size
id845634
size9,359
Emilio Wuerges (wuerges)

documentation

README

Utility function to respawn failed long running tasks

Create a Task factory that will be used to respawn a new task, if the running one fails.

impl FutureFactory to define how the task will be respawned.

impl ErrorHandler to customize error handling.

Call make_future_respawnable to put your factory to work, with the error handler.

struct TaskReturnsResult;

impl FutureFactory<Result<(), io::Error>> for TaskReturnsResult {
    fn build_future(&mut self) -> Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send>> {
        Box::pin(async { panic!("boom") })
    }
}

let factory = TaskReturnsResult;

let handler = AlwaysRespawnAndTrace {
    duration: std::time::Duration::from_millis(1),
};

let join_handle = tokio::spawn(make_future_respawnable(handler, factory));

sleep(Duration::from_millis(10)).await;

join_handle.abort();

let err = join_handle.await.unwrap_err();
assert!(!err.is_panic(), "{:?}", err);
assert!(err.is_cancelled(), "{:?}", err);
Commit count: 0

cargo fmt