Crate context_async

Source
Expand description

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

§Example

Here is a simple example. We use Timer to control an async function to finish in 5 seconds.

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

async fn a_heavy_async_function(a: u8) -> bool {
    return true;
}

 let timer = Timer::with_timeout(time::Duration::from_secs(5));

 let _ = timer.handle(a_heavy_async_function(0)).await;

 // or:

 use context_async::With;
 let _ = a_heavy_async_function(0).with(&timer).await;

Timer implements Context, you can pass a Context to your async function:


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

async fn a_heavy_async_function(a: u8) -> bool {
    return true;
}

async fn my_function<Ctx: Context>(ctx: Ctx) -> Result<bool, Error> {
    a_heavy_async_function(0)
        .with(ctx)
        .await
}

let timer = Timer::with_timeout(time::Duration::from_secs(5));
let _ = my_function(&timer).await;

§Error

Context returns Error, one of Error::ContextCancelled or Error::ContextTimeout.

§Features

  • actix-web-from-request: implement actix-web::FromRequest for Timer.
  • name: create a name for each Context.
  • tracing: enable tracing and do tracing::trace!(...) logging.

Structs§

Enums§

Traits§

  • The Context trait defines the required methods for Context. It can define a duration, be cancellable, and immediately cancel async functions if the processing time exceeds the allowed duration without being cancelled. Additionally, the Context can spawn child contexts.

Type Aliases§

Attribute Macros§