adaptive-timeout 0.0.1-alpha.2

Adaptive timeout computation based on observed latency percentiles
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Thread-safe latency tracker using [`DashMap`] for concurrent access.
//!
//! [`SyncLatencyTracker`] provides the same per-destination sliding-window
//! histogram tracking as [`LatencyTracker`](crate::LatencyTracker), but
//! allows concurrent recording and querying from multiple threads without
//! an external `Mutex`.
//!
//! Internally, each destination's histogram lives in a [`DashMap`] entry
//! with per-shard locking. Concurrent operations on **different**
//! destinations never contend. Operations on the **same** destination
//! serialize, but the critical section is very short (~40-80ns).

use std::hash::{BuildHasher, Hash};
use std::time::Duration;

use dashmap::DashMap;

use crate::clock;
use crate::config::{SIGNIFICANT_VALUE_DIGITS, TrackerConfig};
use crate::histogram::SlidingWindowHistogram;
use crate::tracker::DEFAULT_SUB_WINDOWS;

/// Thread-safe latency tracker backed by [`DashMap`].
///
/// Drop-in concurrent replacement for [`LatencyTracker`](crate::LatencyTracker).
/// All methods take `&self` instead of `&mut self`, and the type is both
/// `Send` and `Sync`.
///
/// # Type parameters
///
/// Same as [`LatencyTracker`](crate::LatencyTracker):
/// - `D` — destination key.
/// - `I` — time source (defaults to [`std::time::Instant`]).
/// - `N` — number of sub-windows (defaults to [`DEFAULT_SUB_WINDOWS`]).
///
/// # Example
///
/// ```
/// use std::time::Instant;
/// use adaptive_timeout::SyncLatencyTracker;
///
/// let now = Instant::now();
/// let tracker = SyncLatencyTracker::<u32>::default();
///
/// // Can be called from any thread via &tracker.
/// tracker.record_latency_ms(&1u32, 50, now);
/// let p99 = tracker.quantile_ms(&1u32, 0.99, now);
/// ```
pub struct SyncLatencyTracker<
    D,
    I: clock::Instant = std::time::Instant,
    H = foldhash::fast::RandomState,
    const N: usize = DEFAULT_SUB_WINDOWS,
> {
    config: TrackerConfig,
    histograms: DashMap<D, SlidingWindowHistogram<I, N>, H>,
}

impl<D, I> Default for SyncLatencyTracker<D, I, foldhash::fast::RandomState, DEFAULT_SUB_WINDOWS>
where
    D: Hash + Eq + Clone + Send + Sync,
    I: clock::Instant,
{
    fn default() -> Self {
        Self::new(TrackerConfig::default())
    }
}

impl<D, I, H, const N: usize> SyncLatencyTracker<D, I, H, N>
where
    D: Hash + Eq + Clone + Send + Sync,
    I: clock::Instant,
    H: BuildHasher + Default + Clone,
{
    /// Creates a new tracker with the given configuration.
    pub fn new(config: TrackerConfig) -> Self {
        Self {
            config,
            histograms: DashMap::default(),
        }
    }
}

impl<D, I, H, const N: usize> SyncLatencyTracker<D, I, H, N>
where
    D: Hash + Eq + Clone + Send + Sync,
    I: clock::Instant,
    H: BuildHasher + Clone,
{
    pub fn with_hasher_and_config(hasher: H, config: TrackerConfig) -> Self {
        Self {
            config,
            histograms: DashMap::with_hasher(hasher),
        }
    }

    /// Records a latency sample given two instants. Returns the computed
    /// duration.
    #[inline]
    pub fn record_latency_from(&self, dest: &D, earlier: I, now: I) -> Duration {
        let latency = now.duration_since(earlier);
        self.record_latency_ms(dest, latency.as_millis() as u64, now);
        latency
    }

    /// Records a latency sample as a [`Duration`].
    #[inline]
    pub fn record_latency(&self, dest: &D, latency: Duration, now: I) {
        self.record_latency_ms(dest, latency.as_millis() as u64, now);
    }

    /// Records a latency sample in milliseconds.
    ///
    /// Takes `&self` — safe to call from multiple threads concurrently.
    /// Operations on different destinations proceed in parallel; operations
    /// on the same destination serialize briefly.
    #[inline]
    pub fn record_latency_ms(&self, dest: &D, latency_ms: u64, now: I) {
        if let Some(mut entry) = self.histograms.get_mut(dest) {
            entry.value_mut().record(latency_ms, now);
            return;
        }
        self.record_latency_ms_cold(dest.clone(), latency_ms, now);
    }

    #[cold]
    fn record_latency_ms_cold(&self, dest: D, latency_ms: u64, now: I) {
        let mut histogram = self.new_histogram(now);
        histogram.record(latency_ms, now);
        self.histograms.insert(dest, histogram);
    }

    /// Returns the estimated latency in milliseconds at the given quantile,
    /// or `None` if insufficient data.
    ///
    /// Takes `&self` — safe to call from multiple threads concurrently.
    #[inline]
    pub fn quantile_ms(&self, dest: &D, quantile: f64, now: I) -> Option<u64> {
        let mut entry = self.histograms.get_mut(dest)?;
        entry
            .value_mut()
            .quantile(quantile, self.config.min_samples as u64, now)
    }

    /// Returns the estimated latency as a [`Duration`] at the given quantile,
    /// or `None` if insufficient data.
    #[inline]
    pub fn quantile(&self, dest: &D, quantile: f64, now: I) -> Option<Duration> {
        self.quantile_ms(dest, quantile, now)
            .map(Duration::from_millis)
    }

    /// Clears all tracked state.
    pub fn clear(&self) {
        self.histograms.clear();
    }

    /// Returns a reference to the tracker configuration.
    #[inline]
    pub fn config(&self) -> &TrackerConfig {
        &self.config
    }

    fn new_histogram(&self, now: I) -> SlidingWindowHistogram<I, N> {
        SlidingWindowHistogram::new(
            self.config.window(),
            SIGNIFICANT_VALUE_DIGITS,
            self.config.max_trackable_latency_ms as u64,
            now,
        )
    }
}

#[cfg(test)]
mod tests {
    use std::time::Instant;

    use super::*;

    type TestTracker = SyncLatencyTracker<u32>;

    fn make_tracker() -> TestTracker {
        let config = TrackerConfig {
            min_samples: 5,
            ..TrackerConfig::default()
        };
        SyncLatencyTracker::new(config)
    }

    #[test]
    fn no_data_returns_none() {
        let now = Instant::now();
        let tracker = make_tracker();
        assert_eq!(tracker.quantile(&1, 0.5, now), None);
    }

    #[test]
    fn record_latency_directly() {
        let now = Instant::now();
        let tracker = make_tracker();

        for _ in 0..10 {
            tracker.record_latency(&1, Duration::from_millis(100), now);
        }

        let p50 = tracker.quantile(&1, 0.5, now).unwrap();
        assert_eq!(p50, Duration::from_millis(100));
    }

    #[test]
    fn record_latency_ms_directly() {
        let now = Instant::now();
        let tracker = make_tracker();

        for _ in 0..10 {
            tracker.record_latency_ms(&1, 100, now);
        }

        let p50 = tracker.quantile_ms(&1, 0.5, now).unwrap();
        assert_eq!(p50, 100);
    }

    #[test]
    fn record_latency_from_computes_duration() {
        let now = Instant::now();
        let tracker = make_tracker();
        let later = now + Duration::from_millis(42);

        for _ in 0..10 {
            let d = tracker.record_latency_from(&1, now, later);
            assert_eq!(d, Duration::from_millis(42));
        }

        let p50 = tracker.quantile_ms(&1, 0.5, later).unwrap();
        assert_eq!(p50, 42);
    }

    #[test]
    fn per_destination_isolation() {
        let now = Instant::now();
        let tracker = make_tracker();

        for _ in 0..10 {
            tracker.record_latency(&1, Duration::from_millis(100), now);
            tracker.record_latency(&2, Duration::from_millis(500), now);
        }

        let p1 = tracker.quantile(&1, 0.5, now).unwrap();
        let p2 = tracker.quantile(&2, 0.5, now).unwrap();

        assert_eq!(p1, Duration::from_millis(100));
        assert!(
            p2 >= Duration::from_millis(495) && p2 <= Duration::from_millis(505),
            "p2 was {p2:?}"
        );

        assert_eq!(tracker.quantile(&3, 0.5, now), None);
    }

    #[test]
    fn clear_resets_all_state() {
        let now = Instant::now();
        let tracker = make_tracker();

        for _ in 0..10 {
            tracker.record_latency(&1, Duration::from_millis(100), now);
        }

        tracker.clear();

        assert_eq!(tracker.quantile(&1, 0.5, now), None);
    }

    #[test]
    fn insufficient_samples_returns_none() {
        let now = Instant::now();
        let tracker = make_tracker(); // min_samples = 5

        for _ in 0..4 {
            tracker.record_latency(&1, Duration::from_millis(100), now);
        }

        assert_eq!(tracker.quantile(&1, 0.5, now), None);

        // 5th sample tips it over.
        tracker.record_latency(&1, Duration::from_millis(100), now);
        assert!(tracker.quantile(&1, 0.5, now).is_some());
    }

    #[test]
    fn is_send_and_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<SyncLatencyTracker<u32>>();
        assert_send_sync::<SyncLatencyTracker<String>>();
    }

    // -----------------------------------------------------------------------
    // Concurrent stress tests
    // -----------------------------------------------------------------------

    #[test]
    fn concurrent_record_same_destination() {
        use std::sync::Arc;
        use std::thread;

        let now = Instant::now();
        let tracker = Arc::new(make_tracker());
        let num_threads = 8;
        let samples_per_thread = 1_000;

        let handles: Vec<_> = (0..num_threads)
            .map(|_| {
                let tracker = Arc::clone(&tracker);
                thread::spawn(move || {
                    for _ in 0..samples_per_thread {
                        tracker.record_latency_ms(&1, 50, now);
                    }
                })
            })
            .collect();

        for h in handles {
            h.join().unwrap();
        }

        let p50 = tracker.quantile_ms(&1, 0.5, now).unwrap();
        assert_eq!(p50, 50);
    }

    #[test]
    fn concurrent_record_different_destinations() {
        use std::sync::Arc;
        use std::thread;

        let now = Instant::now();
        let tracker = Arc::new(make_tracker());
        let num_threads = 8;
        let samples_per_thread = 1_000;

        let handles: Vec<_> = (0..num_threads)
            .map(|tid| {
                let tracker = Arc::clone(&tracker);
                thread::spawn(move || {
                    let dest = tid as u32;
                    for _ in 0..samples_per_thread {
                        tracker.record_latency_ms(&dest, (tid as u64 + 1) * 10, now);
                    }
                })
            })
            .collect();

        for h in handles {
            h.join().unwrap();
        }

        // Each destination should have its own latency.
        for tid in 0..num_threads {
            let dest = tid as u32;
            let expected = (tid as u64 + 1) * 10;
            let p50 = tracker.quantile_ms(&dest, 0.5, now).unwrap();
            assert_eq!(p50, expected, "dest {dest}");
        }
    }

    #[test]
    fn concurrent_read_and_write() {
        use std::sync::Arc;
        use std::thread;

        let now = Instant::now();
        let tracker = Arc::new(make_tracker());

        // Pre-fill with enough data for quantile to return Some.
        for _ in 0..100 {
            tracker.record_latency_ms(&1, 50, now);
        }

        let num_writers = 4;
        let num_readers = 4;
        let iterations = 5_000;

        let mut handles = Vec::new();

        // Writers
        for _ in 0..num_writers {
            let tracker = Arc::clone(&tracker);
            handles.push(thread::spawn(move || {
                for _ in 0..iterations {
                    tracker.record_latency_ms(&1, 50, now);
                }
            }));
        }

        // Readers
        for _ in 0..num_readers {
            let tracker = Arc::clone(&tracker);
            handles.push(thread::spawn(move || {
                for _ in 0..iterations {
                    if let Some(p) = tracker.quantile_ms(&1, 0.5, now) {
                        assert_eq!(p, 50, "unexpected quantile: {p}");
                    }
                }
            }));
        }

        for h in handles {
            h.join().unwrap();
        }
    }
}