Crate async_singleflight[][src]

Expand description

A singleflight implementation for tokio.

Inspired by singleflight.


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;

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 ="key", expensive_fn()).await.0;
            let r = res.unwrap();
            println!("{}", r);



Group represents a class of work and creates a space in which units of work can be executed with duplicate suppression.