Skip to content

Nekidev/turboflight

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

turboflight

A simple single-flight implementation for heavy concurrency.

Simple Usage

use turboflight::SingleFlight;
use tokio::time::{self, Duration};

#[tokio::main]
async fn main() {
    let group = SingleFlight::new();

    let mut tasks = vec![];

    for _ in 0..10 {
        let group = group.clone();  // The inner state of the group is an Arc so it's cheaply clonable.
        tasks.push(tokio::spawn(async move {
            group.work(1, async {
                println!("Doing work...");
                time::sleep(Duration::from_secs(1)).await;
                println!("Work done!");

                100
            }).await
        }));
        println!("Task spawned");
    }

    let results = futures::future::join_all(tasks).await;

    assert!(results.into_iter().all(|res| res.unwrap() == 100));
}

Documentation

For the full documentation, check docs.rs/turboflight.

About

A simple single-flight implementation for heavy concurrency.

Topics

Resources

Stars

Watchers

Forks

Languages