Expand description
A singleflight implementation for tokio.
Inspired by async_singleflight.
§Examples
use std::sync::Arc;
use std::time::Duration;
use futures::future::join_all;
use xet_runtime::utils::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;
}Re-exports§
pub use super::errors::SingleflightError;
Structs§
- Group
- Group represents a class of work and creates a space in which units of work can be executed with duplicate suppression.
Traits§
- Result
Error - Indicates the Error type for a singleflight Group. The response might have been generated on a separate thread, thus, we need this type to be Send + Sync.
- Result
Type - ResultType indicates the success type for a singleflight Group. Since the actual processing might occur on a separate thread, we need to type to be Send + Sync. It also needs to be Clone so that we can clone the response across many tasks
- Task
Future - Futures provided to a singleflight Group must produce a Result<T, E> for some T, E. This future must also be Send as it could be spawned as a tokio task.