#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

#847 in Asynchronous

Download history 1025/week @ 2024-11-16 585/week @ 2024-11-23 1060/week @ 2024-11-30 1098/week @ 2024-12-07 684/week @ 2024-12-14 127/week @ 2024-12-21 586/week @ 2024-12-28 1678/week @ 2025-01-04 1602/week @ 2025-01-11 1864/week @ 2025-01-18 1661/week @ 2025-01-25 2153/week @ 2025-02-01 2237/week @ 2025-02-08 1647/week @ 2025-02-15 1738/week @ 2025-02-22 1253/week @ 2025-03-01

7,516 downloads per month
Used in 6 crates (2 directly)

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
~109K SLoC