15 releases

0.6.1 Aug 27, 2025
0.5.3 Aug 25, 2025
0.5.2 Nov 30, 2022
0.5.0 Jun 30, 2022
0.3.0 Feb 23, 2021

#335 in Asynchronous

Download history 3332/week @ 2025-10-03 3173/week @ 2025-10-10 4234/week @ 2025-10-17 3520/week @ 2025-10-24 3438/week @ 2025-10-31 5087/week @ 2025-11-07 5229/week @ 2025-11-14 3755/week @ 2025-11-21 2083/week @ 2025-11-28 2269/week @ 2025-12-05 3090/week @ 2025-12-12 1275/week @ 2025-12-19 755/week @ 2025-12-26 2091/week @ 2026-01-02 4018/week @ 2026-01-09 2823/week @ 2026-01-16

9,812 downloads per month
Used in 5 crates (2 directly)

MIT/Apache

23KB
483 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::DefaultGroup;

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(DefaultGroup::<usize>::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;
            let r = res.unwrap();
            println!("{}", r);
        }));
    }

    join_all(handlers).await;
}

Dependencies

~2–3MB
~49K SLoC