#async-context #context #async #cancel #timeout #handle #timer

context-async

context handle async future timeout or cancel

14 releases (4 breaking)

new 0.5.1 Dec 21, 2024
0.5.0 Dec 19, 2024
0.4.1 Dec 19, 2024
0.3.3 Dec 18, 2024
0.1.2 Dec 13, 2024

#259 in HTTP server

Download history 170/week @ 2024-12-07 782/week @ 2024-12-14

952 downloads per month

MIT license

30KB
384 lines

context-async

This lib provide an trait Context and a Timer to control async function.

It's based on tokio.

Crates.io Documentation License

Usage

Add this lib as dependencies in Cargo.toml.

[dependencies]
context-async = { version = "*" }

In your code, you can simple use Timer:

use std::time;
use context_async::{Context, Timer, Error, With};

async fn a_heavy_function_or_something_else(a: u8, b: u128) -> Result<(), ()>{
    // ....
    
    Ok(())
}

#[tokio::main]
async fn main() {
    let timer = Timer::with_timeout(time::Duration::from_secs(3));
    
    let fut = a_heavy_function_or_something_else(1, 1024);
    let result = timer.handle(fut).await; // use `handle`
    
    // or:
    // let result = fut.with(timer).await; // trait `With`
    
    let result = match result {
        Err(err) => match err {
            Error::ContextCancelled => "context cancelled",
            Error::ContextTimeout => "context timeout",
        },
        Ok(Err(_)) => "async function error",
        Ok(Ok(_)) => "async function ok",
    };
}

Timer and Context implements Clone, which creates a new Context (same as itself).

Timer and Context can spawn children contexts, and ensure the following conditions are met:

  1. The life cycle of the child context will not exceed the parent context;
  2. If the parent context is cancelled, all child contexts will be cancelled.
  3. The cancellation of the child context will be chained to all child contexts, but will not affect the parent context.

for more information, see examples or visit the documentation.

documentation

Dependencies

~2–13MB
~155K SLoC