Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ All notable changes to this project will be documented in this file.

### New features

* Implement `Singleflight` pattern for deduplicating concurrent requests.
* `singleflight::Group` now supports custom hashers for keys.

## [0.6.0] - 2025-01-04
## [0.6.1] - 2026-01-11

### New features

* Implement `singleflight` pattern for deduplicating concurrent requests.

## [0.6.0] - 2026-01-04

### Breaking changes

Expand Down
29 changes: 23 additions & 6 deletions mea/src/singleflight/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
//! Singleflight provides a duplicate function call suppression mechanism.

use std::collections::HashMap;
use std::hash::BuildHasher;
use std::hash::Hash;
use std::hash::RandomState;
use std::sync::Arc;

use crate::internal::Mutex;
Expand All @@ -27,31 +29,46 @@ mod tests;
/// Group represents a class of work and forms a namespace in which
/// units of work can be executed with duplicate suppression.
#[derive(Debug)]
pub struct Group<K, V> {
map: Mutex<HashMap<K, Arc<OnceCell<V>>>>,
pub struct Group<K, V, S = RandomState> {
map: Mutex<HashMap<K, Arc<OnceCell<V>>, S>>,
}

impl<K, V> Default for Group<K, V>
impl<K, V, S> Default for Group<K, V, S>
where
K: Eq + Hash + Clone,
V: Clone,
S: BuildHasher + Clone + Default,
{
fn default() -> Self {
Self::new()
Self::with_hasher(S::default())
}
}

impl<K, V> Group<K, V>
impl<K, V> Group<K, V, RandomState>
where
K: Eq + Hash + Clone,
V: Clone,
{
/// Creates a new Group.
/// Creates a new Group with the default hasher.
pub fn new() -> Self {
Self {
map: Mutex::new(HashMap::new()),
}
}
}

impl<K, V, S> Group<K, V, S>
where
K: Eq + Hash + Clone,
V: Clone,
S: BuildHasher + Clone,
{
/// Creates a new Group with the given hasher.
pub fn with_hasher(hasher: S) -> Self {
Self {
map: Mutex::new(HashMap::with_hasher(hasher)),
}
}

/// Executes and returns the results of the given function, making sure that only one execution
/// is in-flight for a given key at a time.
Expand Down