1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use crate::cache::Cache;
use std::collections::HashMap;
use std::future::Future;
use std::hash::Hash;
use std::num::NonZeroUsize;
use std::pin::Pin;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::Weak;
use parking_lot::Mutex;
use thiserror::Error;
use tokio::sync::broadcast;
/// Boxed Future yielding an optional value.
pub type DeduplicateFuture<V> = Pin<Box<dyn Future<Output = Option<V>> + Send>>;
type WaitMap<K, V> = Arc<Mutex<HashMap<K, Weak<broadcast::Sender<Option<V>>>>>>;
const DEFAULT_CACHE_CAPACITY: usize = 512;
/// Deduplication errors.
#[derive(Debug, Error)]
pub enum DeduplicateError {
/// The delegated get failed.
#[error("Delegated get failed")]
Failed,
/// There is no cache in this instance.
#[error("Cache not enabled")]
NoCache,
}
/// Query de-duplication with optional cache.
///
/// When trying to avoid multiple slow or expensive gets, use this.
#[derive(Clone)]
pub struct Deduplicate<G, K, V>
where
G: Fn(K) -> DeduplicateFuture<V>,
K: Clone + Send + Eq + Hash,
V: Clone + Send,
{
delegate: G,
storage: Option<Cache<K, V>>,
wait_map: WaitMap<K, V>,
request_deduplicated_counter: Arc<AtomicU64>,
request_total_counter: Arc<AtomicU64>,
}
impl<G, K, V> Deduplicate<G, K, V>
where
G: Fn(K) -> DeduplicateFuture<V>,
K: Clone + Send + Eq + Hash + 'static,
V: Clone + Send + 'static,
{
/// Create a new deduplicator for the provided delegate with default cache capacity: 512.
pub fn new(delegate: G) -> Self {
Self::with_capacity(delegate, DEFAULT_CACHE_CAPACITY)
}
/// Create a new deduplicator for the provided delegate with specified cache capacity.
/// Note: If capacity is 0, then caching is disabled.
pub fn with_capacity(delegate: G, capacity: usize) -> Self {
let storage = if capacity > 0 {
let val = unsafe { NonZeroUsize::new_unchecked(capacity) };
Some(Cache::new(val))
} else {
None
};
Self {
delegate,
wait_map: Arc::new(Mutex::new(HashMap::new())),
storage,
request_deduplicated_counter: Arc::new(AtomicU64::new(0)),
request_total_counter: Arc::new(AtomicU64::new(0)),
}
}
/// Clear the internal cache. This will also reset the request counters.
pub fn clear(&self) {
if let Some(storage) = &self.storage {
storage.clear();
}
self.request_deduplicated_counter
.store(0, Ordering::Relaxed);
self.request_total_counter.store(0, Ordering::Relaxed);
}
/// Return the number of cache entries in use. Will return 0 if no cache is configured.
pub fn count(&self) -> usize {
match &self.storage {
Some(s) => s.count(),
None => 0,
}
}
/// Return the deduplicated request count.
pub fn request_deduplicated_count(&self) -> u64 {
self.request_deduplicated_counter.load(Ordering::Relaxed)
}
/// Return the total request count.
pub fn request_count(&self) -> u64 {
self.request_total_counter.load(Ordering::Relaxed)
}
/// Use the delegate to get a value.
///
/// Many concurrent accessors can attempt to get the same key, but the underlying get will only
/// be called once. If the delegate panics or is cancelled, any concurrent accessors will get the
/// error: [`DeduplicateError::Failed`].
// Disable clippy false positive. We are explicitly dropping our lock, so clippy is wrong.
#[allow(clippy::await_holding_lock)]
pub async fn get(&self, key: K) -> Result<Option<V>, DeduplicateError> {
self.request_total_counter.fetch_add(1, Ordering::Relaxed);
let mut locked_wait_map = self.wait_map.lock();
match locked_wait_map.get(&key) {
Some(weak) => {
self.request_deduplicated_counter
.fetch_add(1, Ordering::Relaxed);
if let Some(strong) = weak.upgrade() {
let mut receiver = strong.subscribe();
// Very important to drop this...
drop(strong);
drop(locked_wait_map);
// Note. It may seem that there is a race condition here, but we have managed
// to upgrade our weak reference and we have subscribed to our broadcast before
// we dropped our lock. Because we only send messages whilst holding the lock,
// we know that it must be safe to recv() here. We still handle errors, but
// don't expect to receive any.
receiver.recv().await.map_err(|_| DeduplicateError::Failed)
} else {
// In the normal run of things, we won't reach this code. However, if a
// delegate panics and fails to complete or a task is cancelled at an .await
// then we may find ourselves here. If so, we may have lost our sender
// and there may still be an entry in the wait_map which our receiver has not
// yet removed. In which case, we don't have a value and we'll never get a
// value, so let's just remove the wait_map entry and return Failed.
let _ = locked_wait_map.remove(&key);
Err(DeduplicateError::Failed)
}
}
None => {
let (sender, mut receiver) = broadcast::channel(1);
let sender = Arc::new(sender);
locked_wait_map.insert(key.clone(), Arc::downgrade(&sender));
if let Some(storage) = &self.storage {
if let Some(value) = storage.get(&key) {
self.request_deduplicated_counter
.fetch_add(1, Ordering::Relaxed);
let _ = locked_wait_map.remove(&key);
let _ = sender.send(Some(value.clone()));
return Ok(Some(value));
}
}
drop(locked_wait_map);
let fut = (self.delegate)(key.clone());
tokio::spawn(async move {
let value = fut.await;
// If we panic before this future completes, we'll get an error in the receiver
// Either way, the wait_map lock will be cleaned up.
let _ = sender.send(value);
});
// We only want one receiver to clean up the wait map, so this is the right place
// to do it.
let result = receiver.recv().await.map_err(|_| DeduplicateError::Failed);
let mut locked_wait_map = self.wait_map.lock();
let _ = locked_wait_map.remove(&key);
let res = result?;
if let Some(storage) = &self.storage {
if let Some(v) = &res {
storage.insert(key, v.clone());
}
}
Ok(res)
}
}
}
/// Insert an entry directly into the cache. If there is no cache , this
/// will fail with [`DeduplicateError::NoCache`].
pub fn insert(&self, key: K, value: V) -> Result<(), DeduplicateError> {
if let Some(storage) = &self.storage {
storage.insert(key, value);
Ok(())
} else {
Err(DeduplicateError::NoCache)
}
}
/// Update the delegate to use for future gets. This will also clear the internal cache and
/// reset the request counters.
pub fn set_delegate(&mut self, delegate: G) {
self.clear();
self.delegate = delegate;
}
/// Return all the cached entries.
pub fn entries(&self) -> Vec<(K, V)> {
match &self.storage {
Some(storage) => storage.entries(),
None => {
vec![]
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use rand::Rng;
use std::time::Instant;
fn get(_key: usize) -> DeduplicateFuture<String> {
let fut = async {
let num = rand::rng().random_range(1000..2000);
tokio::time::sleep(tokio::time::Duration::from_millis(num)).await;
if num % 2 == 0 {
panic!("BAD NUMBER");
}
Some("test".to_string())
};
Box::pin(fut)
}
async fn test_harness<G>(deduplicate: Deduplicate<G, usize, String>)
where
G: Fn(usize) -> DeduplicateFuture<String>,
{
// Let's create our normal getter and use our deduplicating delegate.
// (The same functionality as `get`, but without panicking.)
let no_panic_get = |_x: usize| async {
let num = rand::rng().random_range(1000..2000);
tokio::time::sleep(tokio::time::Duration::from_millis(num)).await;
Some("test".to_string())
};
let deduplicate = Arc::new(deduplicate);
// We are going to perform the work 5 times to be sure our de-duplicator is working
for i in 1..6 {
let mut dedup_hdls = vec![];
let mut slower_hdls = vec![];
let start = Instant::now();
// Create our lists of dedup and non-dedup futures
for _i in 0..100 {
let my_deduplicate = deduplicate.clone();
dedup_hdls.push(async move {
let is_ok = my_deduplicate.get(5).await.is_ok();
(Instant::now(), is_ok)
});
slower_hdls.push(async move {
let is_ok = (no_panic_get)(5).await.is_some();
(Instant::now(), is_ok)
});
}
// Execute our futures and collect the results
let mut dedup_result: Vec<(Instant, bool)> = futures::future::join_all(dedup_hdls)
.await
.into_iter()
.collect();
dedup_result.sort();
let mut slower_result: Vec<(Instant, bool)> = futures::future::join_all(slower_hdls)
.await
.into_iter()
.collect();
slower_result.sort();
// Calculate the range of timings for each set of futures
let dedup_range = dedup_result.last().unwrap().0 - dedup_result.first().unwrap().0;
let slower_range = slower_result.last().unwrap().0 - slower_result.first().unwrap().0;
println!("iteration: {i}");
println!("dedup_range: {dedup_range:?}");
println!("slower_range: {slower_range:?}");
// The dedup range should be a few ms. The slower range will tend towards 1 second.
// It's very unlikely that this assertion will be false, but I should note that it is
// possible... In which case, ignore it and re-run the test.
assert!(dedup_range <= slower_range);
// The number of passing tests will be <= slower for dedup because of the possibility
// of a panic
let dedup_passed = dedup_result
.iter()
.fold(0, |acc, x| if x.1 { acc + 1 } else { acc });
let slower_passed = slower_result
.iter()
.fold(0, |acc, x| if x.1 { acc + 1 } else { acc });
// for dedup, panic == 0 passes, no panic == 100 passes
assert!(dedup_passed == 0 || dedup_passed == 100);
assert_eq!(slower_passed, 100);
assert!(dedup_passed <= slower_passed);
println!("dedup passed: {dedup_passed:?}");
println!("slower passed: {slower_passed:?}");
println!("elapsed: {:?}\n", Instant::now() - start);
}
}
// Test that deduplication works with a default cache.
#[tokio::test]
async fn it_deduplicates_correctly_with_cache() {
let no_panic_get = |_x: usize| -> DeduplicateFuture<String> {
let fut = async {
let num = rand::rng().random_range(1000..2000);
tokio::time::sleep(tokio::time::Duration::from_millis(num)).await;
Some("test".to_string())
};
Box::pin(fut)
};
test_harness(Deduplicate::new(no_panic_get)).await
}
// Test that deduplication works with no cache.
#[tokio::test]
async fn it_deduplicates_correctly_without_cache() {
test_harness(Deduplicate::with_capacity(get, 0)).await
}
// Test that entries works correctly
#[tokio::test]
async fn it_returns_entries() {
let get_x = |x: usize| -> DeduplicateFuture<String> {
let fut = async move { Some(x.to_string()) };
Box::pin(fut)
};
let dedup = Deduplicate::new(get_x);
assert_eq!(
dedup
.get(1)
.await
.expect("it works")
.expect("it finds something"),
1.to_string()
);
assert_eq!(
dedup
.get(2)
.await
.expect("it works")
.expect("it finds something"),
2.to_string()
);
let mut entries = dedup.entries();
entries.sort();
assert_eq!(entries, vec![(1, 1.to_string()), (2, 2.to_string())]);
}
}