singleflight-async

Crates.iosingleflight-async
lib.rssingleflight-async
version
sourcesrc
created_at2022-03-11 08:42:56.660152
updated_at2024-11-06 07:04:07.327895
descriptionSingleflight in async style.
homepage
repositoryhttps://github.com/ihciah/singleflight-async
max_upload_size
id548089
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
(TennyZhuang)

documentation

README

Singleflight Async

Crates.io MIT/Apache-2 licensed

Singleflight in async style.

Key Features

  • Execute an async task only once for the same key at the same time.
  • Cancel safe when the task is dropped.
  • Not requires the future to be Send/Sync, or 'static.
  • Works for all kind of runtimes including tokio, monoio, or others.

Example

use singleflight_async::SingleFlight;

#[tokio::main]
async fn main() {
    let group = SingleFlight::new();
    let mut futures = Vec::new();
    for _ in 0..10 {
        futures.push(group.work("key", || async {
            println!("will sleep to simulate async task");
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
            println!("real task done");
            "my-result"
        }));
    }

    let begin = std::time::Instant::now();
    for fut in futures.into_iter() {
        assert_eq!(fut.await, "my-result");
        println!("task finished");
    }
    println!("time elapsed: {:?}", begin.elapsed());
}

The output will be like:

will sleep to simulate async task
real task done
task finished
task finished
task finished
task finished
task finished
task finished
task finished
task finished
task finished
task finished
time elapsed: 100.901321ms
Commit count: 5

cargo fmt