#async #singleflight #tokio

async_singleflight

Async singleflight

12 releases

0.5.2 Nov 30, 2022
0.5.1 Nov 30, 2022
0.5.0 Jun 30, 2022
0.4.0 Oct 20, 2021
0.0.6 Feb 16, 2021

#819 in Asynchronous

Download history 24/week @ 2024-07-19 65/week @ 2024-07-26 16/week @ 2024-08-02 12/week @ 2024-08-09 9/week @ 2024-08-16 14/week @ 2024-08-23 17/week @ 2024-08-30 3/week @ 2024-09-06 8/week @ 2024-09-13 15/week @ 2024-09-20 8/week @ 2024-09-27 11/week @ 2024-10-04 19/week @ 2024-10-11 850/week @ 2024-10-18 1576/week @ 2024-10-25 1281/week @ 2024-11-01

3,735 downloads per month
Used in 2 crates

MIT/Apache

14KB
319 lines

Async singleflight

Documentation: async_singleflight.


lib.rs:

A singleflight implementation for tokio.

Inspired by singleflight.

Examples

use futures::future::join_all;
use std::sync::Arc;
use std::time::Duration;

use async_singleflight::Group;

const RES: usize = 7;

async fn expensive_fn() -> Result<usize, ()> {
    tokio::time::sleep(Duration::new(1, 500)).await;
    Ok(RES)
}

#[tokio::main]
async fn main() {
    let g = Arc::new(Group::<_, ()>::new());
    let mut handlers = Vec::new();
    for _ in 0..10 {
        let g = g.clone();
        handlers.push(tokio::spawn(async move {
            let res = g.work("key", expensive_fn()).await.0;
            let r = res.unwrap();
            println!("{}", r);
        }));
    }

    join_all(handlers).await;
}

Dependencies

~5–11MB
~110K SLoC