holochain_p2p 0.7.0-dev.23

holochain specific wrapper around more generic p2p module
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
use futures::stream::FuturesUnordered;
use futures::StreamExt;
use kitsune2_api::{DynSpace, Url};
use std::collections::{HashMap, VecDeque};
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::sync::mpsc;
use tokio::task::AbortHandle;

/// Maximum number of latency samples kept per peer URL in the rolling window.
const MAX_SAMPLES: usize = 10;

/// Estimates are considered stale after `recorded_at + EXPIRY_DURATION`.
const EXPIRY_DURATION: Duration = Duration::from_secs(60 * 60);

/// Entries older than this are permanently evicted from the store to bound memory.
/// Set to twice [`EXPIRY_DURATION`] so stale fallback data is available during re-ping.
const EVICTION_DURATION: Duration = Duration::from_secs(2 * 60 * 60);

/// How far before [`EXPIRY_DURATION`] to schedule a proactive refresh ping.
const REFRESH_BUFFER: Duration = Duration::from_secs(10 * 60);

/// Number of sequential ping samples to collect per peer URL.
const PING_SAMPLE_COUNT: usize = 10;

/// Maximum number of peer URLs to ping concurrently in the background worker.
const MAX_CONCURRENT_PINGS: usize = 8;

/// After this many consecutive all-failure ping rounds, a peer is
/// considered to have failed pings and will not be pinged again until
/// re-touched.
const CONSECUTIVE_FAILURE_THRESHOLD: u32 = 3;

/// Default tick interval for the worker when no peer is immediately due.
const WORKER_TICK_INTERVAL: Duration = Duration::from_secs(60);

/// Function type for sending a single ping and measuring RTT.
///
/// The actor provides an implementation that sends a `PingReq` wire message
/// and waits for the `PingRes`, returning the measured round-trip time.
pub(crate) type PingFn = Arc<
    dyn Fn(DynSpace, Url) -> Pin<Box<dyn Future<Output = Option<Duration>> + Send>> + Send + Sync,
>;

/// Rolling-window latency estimate for a single peer URL.
///
/// Maintains up to [`MAX_SAMPLES`] RTT samples and a cached average.
/// Entries are considered stale after `recorded_at + EXPIRY_DURATION`.
#[derive(Debug)]
pub(crate) struct LatencyEstimate {
    samples: VecDeque<Duration>,
    average: Duration,
    recorded_at: Instant,
}

impl LatencyEstimate {
    /// Creates a new estimate seeded with a single sample.
    fn new(sample: Duration) -> Self {
        let mut samples = VecDeque::with_capacity(MAX_SAMPLES);
        samples.push_back(sample);
        Self {
            samples,
            average: sample,
            recorded_at: Instant::now(),
        }
    }

    /// Appends a sample to the rolling window, evicting the oldest when
    /// the window is full, then recomputes the cached average and
    /// updates `recorded_at`.
    fn push_sample(&mut self, rtt: Duration) {
        if self.samples.len() == MAX_SAMPLES {
            let _ = self.samples.pop_front();
        }
        self.samples.push_back(rtt);
        self.recorded_at = Instant::now();

        let total = self
            .samples
            .iter()
            .copied()
            .fold(Duration::ZERO, |acc, sample| acc + sample);
        self.average = total / (self.samples.len() as u32);
    }

    /// Returns `true` if this estimate is older than [`EXPIRY_DURATION`]
    /// relative to the given reference instant.
    fn is_expired_at(&self, now: Instant) -> bool {
        now.duration_since(self.recorded_at) >= EXPIRY_DURATION
    }

    /// Returns `true` when this estimate is within [`REFRESH_BUFFER`] of
    /// [`EXPIRY_DURATION`], indicating it should be proactively refreshed
    /// before it expires.
    fn is_due_for_refresh(&self, now: Instant) -> bool {
        let refresh_threshold = EXPIRY_DURATION.saturating_sub(REFRESH_BUFFER);
        now.duration_since(self.recorded_at) >= refresh_threshold
    }

    /// Returns the selection weight for this estimate: `1 / latency_ms`.
    ///
    /// Lower-latency peers get higher weights. Latency is clamped to a
    /// minimum of 1 ms to avoid division by zero.
    pub(crate) fn weight(&self) -> f64 {
        1.0 / (self.average.as_secs_f64() * 1000.0).max(1.0)
    }
}

/// Pure latency data: estimates and failure bookkeeping. No async, no tasks.
#[derive(Debug, Default)]
pub(crate) struct LatencyData {
    estimates: HashMap<Url, LatencyEstimate>,
    consecutive_failures: HashMap<Url, u32>,
}

impl LatencyData {
    /// Records a new RTT sample for the given URL.
    ///
    /// Maintains a rolling window of at most [`MAX_SAMPLES`] entries,
    /// evicting the oldest when full. Recalculates the cached average
    /// and updates `recorded_at`. Also clears any consecutive failure count.
    pub(crate) fn record_sample(&mut self, url: Url, rtt: Duration) {
        self.consecutive_failures.remove(&url);

        match self.estimates.get_mut(&url) {
            Some(entry) => entry.push_sample(rtt),
            None => {
                self.estimates.insert(url, LatencyEstimate::new(rtt));
            }
        }
    }

    /// Returns the rolling-average latency for the given URL if the entry
    /// exists and has not expired. Returns `None` otherwise.
    #[cfg(test)]
    fn get_latency(&self, url: &Url) -> Option<Duration> {
        self.get_latency_at(url, Instant::now())
    }

    /// Returns the rolling-average latency relative to the given reference
    /// instant. Used in tests to avoid platform-dependent `Instant` arithmetic.
    #[cfg(test)]
    fn get_latency_at(&self, url: &Url, now: Instant) -> Option<Duration> {
        self.estimates.get(url).and_then(|entry| {
            if entry.is_expired_at(now) {
                None
            } else {
                Some(entry.average)
            }
        })
    }

    /// Returns the rolling-average latency even if the entry has expired.
    /// Used as a soft fallback during re-ping to avoid sudden weight changes.
    #[cfg(test)]
    fn get_latency_including_stale(&self, url: &Url) -> Option<Duration> {
        self.estimates.get(url).map(|entry| entry.average)
    }

    /// Returns the selection weight for any known URL.
    ///
    /// Returns `None` if the URL has never been seen. Freshness is not
    /// checked here — the worker proactively refreshes entries before
    /// [`EXPIRY_DURATION`] via [`REFRESH_BUFFER`], and [`evict_stale`]
    /// removes entries past [`EVICTION_DURATION`]. Any entry still in
    /// the store is considered usable for weighting.
    pub(crate) fn get_weight(&self, url: &Url) -> Option<f64> {
        self.estimates.get(url).map(|entry| entry.weight())
    }

    /// Records a full-round ping failure for a URL, incrementing the
    /// consecutive failure counter.
    pub(crate) fn record_failure(&mut self, url: &Url) {
        *self.consecutive_failures.entry(url.clone()).or_insert(0) += 1;
    }

    /// Returns `true` if the URL has exceeded [`CONSECUTIVE_FAILURE_THRESHOLD`]
    /// consecutive ping failures and should not be pinged again.
    ///
    /// This is a ping-specific signal distinct from the transport-level
    /// unresponsive marker maintained in the peer meta store.
    pub(crate) fn has_failed_pings(&self, url: &Url) -> bool {
        self.consecutive_failures
            .get(url)
            .is_some_and(|&count| count >= CONSECUTIVE_FAILURE_THRESHOLD)
    }

    /// Clears the consecutive failure counter for a URL, re-enabling pings.
    fn clear_failures(&mut self, url: &Url) {
        self.consecutive_failures.remove(url);
    }

    /// Returns `true` if the URL has no estimate, an expired estimate,
    /// or an estimate due for proactive refresh.
    fn needs_ping(&self, url: &Url) -> bool {
        self.needs_ping_at(url, Instant::now())
    }

    /// Returns `true` if the URL has no estimate, an expired estimate,
    /// or an estimate due for proactive refresh, relative to the given
    /// reference instant.
    fn needs_ping_at(&self, url: &Url, now: Instant) -> bool {
        self.estimates
            .get(url)
            .is_none_or(|e| e.is_expired_at(now) || e.is_due_for_refresh(now))
    }

    /// Removes entries whose `recorded_at` is older than [`EVICTION_DURATION`],
    /// bounding memory growth under peer churn.
    fn evict_stale(&mut self) {
        self.evict_stale_at(Instant::now());
    }

    /// Removes entries whose `recorded_at` is older than [`EVICTION_DURATION`]
    /// relative to the given reference instant.
    fn evict_stale_at(&mut self, now: Instant) {
        self.estimates
            .retain(|_, entry| now.duration_since(entry.recorded_at) < EVICTION_DURATION);
    }
}

/// Request to register or refresh a peer URL for latency tracking.
struct TouchRequest {
    url: Url,
    space: DynSpace,
}

/// Per-URL bookkeeping inside the background worker.
struct PeerWorkState {
    space: DynSpace,
    last_touched_at: Instant,
}

/// Active latency-estimation service.
///
/// Owns a single background Tokio task that pings peers on a schedule.
/// The actor interacts only through [`touch`](Self::touch) and
/// [`store`](Self::store).
pub(crate) struct PeerLatencyService {
    touch_tx: mpsc::UnboundedSender<TouchRequest>,
    store: Arc<Mutex<LatencyData>>,
    abort_handle: AbortHandle,
}

impl PeerLatencyService {
    /// Spawns the background worker task and returns the service handle.
    pub(crate) fn new(ping_fn: PingFn) -> Self {
        let (touch_tx, touch_rx) = mpsc::unbounded_channel();
        let store = Arc::new(Mutex::new(LatencyData::default()));
        let task_store = Arc::clone(&store);
        let handle = tokio::spawn(run_worker(touch_rx, task_store, ping_fn));
        let abort_handle = handle.abort_handle();
        Self {
            touch_tx,
            store,
            abort_handle,
        }
    }

    /// Notifies the service that a peer URL was encountered.
    ///
    /// If the URL is new, the worker will schedule pings for it.
    /// If the URL previously had failed pings, it is re-activated.
    pub(crate) fn touch(&self, url: Url, space: DynSpace) {
        let _ = self.touch_tx.send(TouchRequest { url, space });
    }

    /// Returns a shared handle to the latency data for read access
    /// (e.g., weighted selection).
    pub(crate) fn store(&self) -> Arc<Mutex<LatencyData>> {
        Arc::clone(&self.store)
    }
}

impl Drop for PeerLatencyService {
    fn drop(&mut self) {
        self.abort_handle.abort();
    }
}

/// Computes how long the worker should sleep before the next peer is due
/// for a ping.
fn compute_next_wake_duration(
    peer_state: &HashMap<Url, PeerWorkState>,
    store: &Mutex<LatencyData>,
) -> Duration {
    if peer_state.is_empty() {
        return WORKER_TICK_INTERVAL;
    }

    let data = store.lock().expect("latency data lock poisoned");
    let now = Instant::now();
    let mut min_wait = WORKER_TICK_INTERVAL;

    for url in peer_state.keys() {
        if data.has_failed_pings(url) {
            continue;
        }
        match data.estimates.get(url) {
            None => return Duration::ZERO,
            Some(est) => {
                let refresh_at = est.recorded_at + EXPIRY_DURATION.saturating_sub(REFRESH_BUFFER);
                if now >= refresh_at {
                    return Duration::ZERO;
                }
                let wait = refresh_at - now;
                if wait < min_wait {
                    min_wait = wait;
                }
            }
        }
    }

    min_wait
}

/// Prunes worker-side state for URLs whose estimate has been evicted and
/// that haven't been touched within [`EVICTION_DURATION`].
///
/// Also drops orphan `consecutive_failures` entries for pruned URLs to keep
/// failure bookkeeping bounded under peer churn.
fn prune_peer_state_at(
    peer_state: &mut HashMap<Url, PeerWorkState>,
    data: &mut LatencyData,
    now: Instant,
) {
    peer_state.retain(|url, state| {
        data.estimates.contains_key(url)
            || now.duration_since(state.last_touched_at) < EVICTION_DURATION
    });
    data.consecutive_failures
        .retain(|url, _| peer_state.contains_key(url));
}

/// Background worker loop that schedules and executes pings.
async fn run_worker(
    mut touch_rx: mpsc::UnboundedReceiver<TouchRequest>,
    store: Arc<Mutex<LatencyData>>,
    ping_fn: PingFn,
) {
    let mut peer_state: HashMap<Url, PeerWorkState> = HashMap::new();

    loop {
        let next_wait = compute_next_wake_duration(&peer_state, &store);

        tokio::select! {
            biased;

            msg = touch_rx.recv() => {
                match msg {
                    Some(touch) => {
                        store
                            .lock()
                            .expect("latency data lock poisoned")
                            .clear_failures(&touch.url);
                        let now = Instant::now();
                        peer_state
                            .entry(touch.url)
                            .and_modify(|state| {
                                state.space = touch.space.clone();
                                state.last_touched_at = now;
                            })
                            .or_insert_with(|| PeerWorkState {
                                space: touch.space,
                                last_touched_at: now,
                            });
                    }
                    None => break,
                }
            }

            _ = tokio::time::sleep(next_wait) => {
                ping_due_peers(&peer_state, &store, &ping_fn).await;
                let mut data = store.lock().expect("latency data lock poisoned");
                data.evict_stale();
                prune_peer_state_at(&mut peer_state, &mut data, Instant::now());
            }
        }
    }
}

/// Finds peers that are due for pinging and pings them with bounded concurrency.
async fn ping_due_peers(
    peer_state: &HashMap<Url, PeerWorkState>,
    store: &Arc<Mutex<LatencyData>>,
    ping_fn: &PingFn,
) {
    let urls_to_ping: Vec<(Url, DynSpace)> = {
        let data = store.lock().expect("latency data lock poisoned");
        peer_state
            .iter()
            .filter(|(url, _)| !data.has_failed_pings(url) && data.needs_ping(url))
            .map(|(url, state)| (url.clone(), state.space.clone()))
            .collect()
    };

    if urls_to_ping.is_empty() {
        return;
    }

    let mut futures = FuturesUnordered::new();

    for (url, space) in urls_to_ping {
        let ping_fn = Arc::clone(ping_fn);
        let task_store = Arc::clone(store);
        futures.push(async move {
            let mut success_count = 0u32;
            for _ in 0..PING_SAMPLE_COUNT {
                match ping_fn(space.clone(), url.clone()).await {
                    Some(rtt) => {
                        task_store
                            .lock()
                            .expect("latency data lock poisoned")
                            .record_sample(url.clone(), rtt);
                        success_count += 1;
                    }
                    None => {
                        // Bail early: a failed sample usually means
                        // send_notify error or timeout, so spending
                        // the full PING_TIMEOUT budget on the remaining
                        // samples would just delay the failure round.
                        break;
                    }
                }
            }
            (url, success_count)
        });

        // Drain completed futures when we hit the concurrency limit.
        while futures.len() >= MAX_CONCURRENT_PINGS {
            if let Some((url, success_count)) = futures.next().await {
                handle_ping_result(store, &url, success_count);
            }
        }
    }

    // Drain remaining futures.
    while let Some((url, success_count)) = futures.next().await {
        handle_ping_result(store, &url, success_count);
    }
}

/// Records ping outcome: logs the result and tracks consecutive failures.
fn handle_ping_result(store: &Arc<Mutex<LatencyData>>, url: &Url, success_count: u32) {
    if success_count == 0 {
        tracing::debug!(%url, "All ping samples failed for peer");
        store
            .lock()
            .expect("latency data lock poisoned")
            .record_failure(url);
    } else {
        tracing::debug!(%url, success_count, "Ping sampling complete for peer");
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_url(name: &str) -> Url {
        Url::from_str(format!("ws://test-{name}:1234")).unwrap()
    }

    /// Returns a synthetic future `Instant` that is `offset` ahead of now.
    ///
    /// Tests use this instead of trying to create an `Instant` in the past,
    /// which fails on platforms where `Instant` cannot represent times before
    /// process start (e.g., short-lived Windows processes).
    fn future_instant(offset: Duration) -> Instant {
        Instant::now() + offset
    }

    // -- LatencyEstimate tests --

    #[test]
    fn weight_inverse_of_latency_ms() {
        let est = LatencyEstimate::new(Duration::from_millis(100));
        let w = est.weight();
        // 1 / 100 = 0.01
        assert!((w - 0.01).abs() < 1e-9, "weight was {w}");
    }

    #[test]
    fn weight_clamps_zero_latency_to_one_ms() {
        let est = LatencyEstimate::new(Duration::ZERO);
        let w = est.weight();
        // 1 / 1 = 1.0
        assert!((w - 1.0).abs() < 1e-9, "weight was {w}");
    }

    #[test]
    fn is_due_for_refresh_before_threshold() {
        let est = LatencyEstimate::new(Duration::from_millis(50));
        // 40 min: not yet at 50 min threshold
        let future = future_instant(Duration::from_secs(40 * 60));
        assert!(!est.is_due_for_refresh(future));
    }

    #[test]
    fn is_due_for_refresh_at_threshold() {
        let est = LatencyEstimate::new(Duration::from_millis(50));
        // 50 min: at the refresh threshold (EXPIRY - REFRESH_BUFFER = 50 min)
        let future = future_instant(Duration::from_secs(50 * 60));
        assert!(est.is_due_for_refresh(future));
    }

    #[test]
    fn is_due_for_refresh_after_expiry() {
        let est = LatencyEstimate::new(Duration::from_millis(50));
        // 65 min: past expiry
        let future = future_instant(Duration::from_secs(65 * 60));
        assert!(est.is_due_for_refresh(future));
    }

    // -- LatencyData tests --

    #[test]
    fn get_latency_returns_none_for_unknown_peer() {
        let data = LatencyData::default();
        assert!(data.get_latency(&test_url("a")).is_none());
    }

    #[test]
    fn record_sample_and_get_latency() {
        let mut data = LatencyData::default();
        let url = test_url("a");
        data.record_sample(url.clone(), Duration::from_millis(100));
        data.record_sample(url.clone(), Duration::from_millis(200));

        assert_eq!(data.get_latency(&url), Some(Duration::from_millis(150)));
    }

    #[test]
    fn rolling_window_evicts_oldest_at_max_samples() {
        let mut data = LatencyData::default();
        let url = test_url("a");

        for _ in 0..MAX_SAMPLES {
            data.record_sample(url.clone(), Duration::from_millis(100));
        }

        assert_eq!(data.get_latency(&url), Some(Duration::from_millis(100)));

        data.record_sample(url.clone(), Duration::from_millis(200));

        assert_eq!(data.get_latency(&url), Some(Duration::from_millis(110)));
    }

    #[test]
    fn expired_entry_returns_none_and_needs_ping() {
        let mut data = LatencyData::default();
        let url = test_url("a");
        data.record_sample(url.clone(), Duration::from_millis(50));

        let future = future_instant(Duration::from_secs(2 * 60 * 60));

        assert!(data.get_latency_at(&url, future).is_none());
        assert!(data.needs_ping_at(&url, future));
    }

    #[test]
    fn get_latency_including_stale_returns_expired_average() {
        let mut data = LatencyData::default();
        let url = test_url("a");
        data.record_sample(url.clone(), Duration::from_millis(50));

        let future = future_instant(Duration::from_secs(2 * 60 * 60));

        assert!(data.get_latency_at(&url, future).is_none());
        assert_eq!(
            data.get_latency_including_stale(&url),
            Some(Duration::from_millis(50))
        );
    }

    #[test]
    fn get_weight_returns_estimate_weight() {
        let mut data = LatencyData::default();
        let url = test_url("a");
        data.record_sample(url.clone(), Duration::from_millis(100));

        let w = data.get_weight(&url).unwrap();
        assert!((w - 0.01).abs() < 1e-9, "weight was {w}");
    }

    #[test]
    fn get_weight_returns_none_for_unknown() {
        let data = LatencyData::default();
        assert!(data.get_weight(&test_url("a")).is_none());
    }

    #[test]
    fn evict_stale_removes_entries_older_than_eviction_duration() {
        let mut data = LatencyData::default();
        let fresh_url = test_url("fresh");
        let stale_url = test_url("stale");

        data.record_sample(fresh_url.clone(), Duration::from_millis(50));
        data.record_sample(stale_url.clone(), Duration::from_millis(100));

        let future = future_instant(Duration::from_secs(3 * 60 * 60));
        data.estimates.get_mut(&fresh_url).unwrap().recorded_at =
            future - Duration::from_secs(30 * 60);

        data.evict_stale_at(future);

        assert!(data.get_latency_at(&fresh_url, future).is_some());
        assert!(data.get_latency_including_stale(&stale_url).is_none());
    }

    #[test]
    fn evict_stale_keeps_entries_within_eviction_window() {
        let mut data = LatencyData::default();
        let url = test_url("a");

        data.record_sample(url.clone(), Duration::from_millis(50));

        // 1.5 hours: past EXPIRY (1h) but within EVICTION (2h).
        let future = future_instant(Duration::from_secs(90 * 60));

        data.evict_stale_at(future);

        assert!(data.get_latency_at(&url, future).is_none());
        assert!(data.get_latency_including_stale(&url).is_some());
    }

    #[test]
    fn needs_ping_returns_true_for_unknown_peer() {
        let data = LatencyData::default();
        assert!(data.needs_ping(&test_url("a")));
    }

    #[test]
    fn needs_ping_returns_false_for_fresh_peer() {
        let mut data = LatencyData::default();
        let url = test_url("a");
        data.record_sample(url.clone(), Duration::from_millis(50));
        assert!(!data.needs_ping(&url));
    }

    #[test]
    fn needs_ping_returns_true_near_expiry() {
        let mut data = LatencyData::default();
        let url = test_url("a");
        data.record_sample(url.clone(), Duration::from_millis(50));

        // 55 min: past the refresh threshold (50 min)
        let future = future_instant(Duration::from_secs(55 * 60));
        assert!(data.needs_ping_at(&url, future));
    }

    // -- Ping-failure detection tests --

    #[test]
    fn has_failed_pings_after_consecutive_failures() {
        let mut data = LatencyData::default();
        let url = test_url("a");

        for _ in 0..CONSECUTIVE_FAILURE_THRESHOLD {
            assert!(!data.has_failed_pings(&url));
            data.record_failure(&url);
        }
        assert!(data.has_failed_pings(&url));
    }

    #[test]
    fn record_sample_clears_failures() {
        let mut data = LatencyData::default();
        let url = test_url("a");

        for _ in 0..CONSECUTIVE_FAILURE_THRESHOLD - 1 {
            data.record_failure(&url);
        }
        assert!(!data.has_failed_pings(&url));

        data.record_sample(url.clone(), Duration::from_millis(50));
        assert!(!data.has_failed_pings(&url));
        assert_eq!(data.consecutive_failures.get(&url), None);
    }

    #[test]
    fn clear_failures_reactivates_peer_with_failed_pings() {
        let mut data = LatencyData::default();
        let url = test_url("a");

        for _ in 0..CONSECUTIVE_FAILURE_THRESHOLD {
            data.record_failure(&url);
        }
        assert!(data.has_failed_pings(&url));

        data.clear_failures(&url);
        assert!(!data.has_failed_pings(&url));
    }

    // -- PeerLatencyService integration tests --

    /// Minimal mock of [`kitsune2_api::Space`] for testing.
    /// The ping function in tests ignores the space, so all methods
    /// are stubs that panic if called.
    mod mock_space {
        use kitsune2_api::*;
        use std::sync::Arc;

        #[derive(Debug)]
        pub(super) struct StubSpace;

        impl Space for StubSpace {
            fn peer_store(&self) -> &DynPeerStore {
                unimplemented!("stub")
            }
            fn local_agent_store(&self) -> &DynLocalAgentStore {
                unimplemented!("stub")
            }
            fn op_store(&self) -> &DynOpStore {
                unimplemented!("stub")
            }
            fn fetch(&self) -> &DynFetch {
                unimplemented!("stub")
            }
            fn publish(&self) -> &DynPublish {
                unimplemented!("stub")
            }
            fn gossip(&self) -> &DynGossip {
                unimplemented!("stub")
            }
            fn peer_meta_store(&self) -> &DynPeerMetaStore {
                unimplemented!("stub")
            }
            fn blocks(&self) -> &DynBlocks {
                unimplemented!("stub")
            }
            fn known_peers(&self) -> &DynKnownPeers {
                unimplemented!("stub")
            }
            fn current_url(&self) -> Option<Url> {
                None
            }
            fn local_agent_join(&self, _local_agent: DynLocalAgent) -> BoxFut<'_, K2Result<()>> {
                unimplemented!("stub")
            }
            fn local_agent_leave(&self, _local_agent: AgentId) -> BoxFut<'_, ()> {
                unimplemented!("stub")
            }
            fn send_notify(&self, _to_peer: Url, _data: bytes::Bytes) -> BoxFut<'_, K2Result<()>> {
                unimplemented!("stub")
            }
            fn inform_ops_stored(&self, _ops: Vec<StoredOp>) -> BoxFut<'_, K2Result<()>> {
                unimplemented!("stub")
            }
        }

        pub(super) fn stub_space() -> DynSpace {
            Arc::new(StubSpace)
        }
    }

    #[tokio::test]
    async fn service_touch_triggers_ping_and_records_sample() {
        let ping_fn: PingFn =
            Arc::new(|_space, _url| Box::pin(async { Some(Duration::from_millis(42)) }));

        let service = PeerLatencyService::new(ping_fn);
        let space = mock_space::stub_space();
        let url = test_url("ping-me");

        service.touch(url.clone(), space);

        // Give the worker time to process the touch and run pings.
        tokio::time::sleep(Duration::from_millis(500)).await;

        let store = service.store();
        let data = store.lock().expect("latency data lock poisoned");
        assert!(
            data.get_latency(&url).is_some(),
            "expected latency sample to be recorded"
        );
    }

    #[tokio::test]
    async fn service_ping_failing_peer_stops_pinging() {
        let call_count = Arc::new(std::sync::atomic::AtomicU32::new(0));
        let call_count_fn = Arc::clone(&call_count);

        let ping_fn: PingFn = Arc::new(move |_space, _url| {
            call_count_fn.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            Box::pin(async { None }) // Always fail
        });

        let service = PeerLatencyService::new(ping_fn);
        let space = mock_space::stub_space();
        let url = test_url("ping-failing");

        service.touch(url.clone(), space);

        // Wait for initial round of pings + a few worker ticks.
        tokio::time::sleep(Duration::from_millis(500)).await;

        let initial_calls = call_count.load(std::sync::atomic::Ordering::Relaxed);

        // Wait more — if ping-failure detection works, no more pings.
        tokio::time::sleep(Duration::from_millis(500)).await;

        let final_calls = call_count.load(std::sync::atomic::Ordering::Relaxed);

        // Each round bails on the first failing sample, so an always-failing
        // peer should stop being pinged after CONSECUTIVE_FAILURE_THRESHOLD
        // rounds. Stay lenient in case timing lets one extra round slip in.
        assert!(
            final_calls <= initial_calls + (PING_SAMPLE_COUNT as u32),
            "expected pinging to stop, but calls went from {initial_calls} to {final_calls}"
        );
    }

    #[tokio::test]
    async fn ping_round_bails_early_on_first_failure() {
        let call_count = Arc::new(std::sync::atomic::AtomicU32::new(0));
        let call_count_fn = Arc::clone(&call_count);

        let ping_fn: PingFn = Arc::new(move |_space, _url| {
            call_count_fn.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            Box::pin(async { None }) // Always fail on first sample
        });

        let service = PeerLatencyService::new(ping_fn);
        let space = mock_space::stub_space();
        service.touch(test_url("dead"), space);

        // Worker loops without delay until the peer is marked as having failed pings,
        // so 300ms is comfortably enough to exhaust CONSECUTIVE_FAILURE_THRESHOLD.
        tokio::time::sleep(Duration::from_millis(300)).await;

        let count = call_count.load(std::sync::atomic::Ordering::Relaxed);

        // With the early-exit, each round calls ping_fn exactly once before
        // bailing. Without it, each round would have called it
        // PING_SAMPLE_COUNT times.
        assert!(count >= 1, "expected at least one ping attempt");
        assert!(
            count < PING_SAMPLE_COUNT as u32,
            "expected short-circuit: got {count} calls, should be < {PING_SAMPLE_COUNT} per round"
        );
    }

    #[tokio::test]
    async fn ping_round_records_every_successful_sample() {
        let call_count = Arc::new(std::sync::atomic::AtomicU32::new(0));
        let call_count_fn = Arc::clone(&call_count);

        let ping_fn: PingFn = Arc::new(move |_space, _url| {
            call_count_fn.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            Box::pin(async { Some(Duration::from_millis(5)) })
        });

        let service = PeerLatencyService::new(ping_fn);
        let space = mock_space::stub_space();
        let url = test_url("alive");
        service.touch(url.clone(), space);

        tokio::time::sleep(Duration::from_millis(300)).await;

        // On a healthy peer, every sample in the round should fire — no
        // early exit — so the first round alone accounts for
        // PING_SAMPLE_COUNT calls.
        let count = call_count.load(std::sync::atomic::Ordering::Relaxed);
        assert!(
            count >= PING_SAMPLE_COUNT as u32,
            "expected at least {PING_SAMPLE_COUNT} pings for healthy peer, got {count}"
        );
    }

    // -- prune_peer_state_at tests --

    #[test]
    fn prune_keeps_entries_with_live_estimate() {
        let mut peer_state: HashMap<Url, PeerWorkState> = HashMap::new();
        let mut data = LatencyData::default();
        let url = test_url("a");

        // Insert with a last_touched_at far in the past relative to `now`
        // below, so the only reason to keep the entry is the live estimate.
        let touched_at = Instant::now();
        peer_state.insert(
            url.clone(),
            PeerWorkState {
                space: mock_space::stub_space(),
                last_touched_at: touched_at,
            },
        );
        data.record_sample(url.clone(), Duration::from_millis(50));

        let now = touched_at + EVICTION_DURATION + Duration::from_secs(60);
        prune_peer_state_at(&mut peer_state, &mut data, now);
        assert!(peer_state.contains_key(&url));
    }

    #[test]
    fn prune_keeps_recently_touched_entries_without_estimate() {
        let mut peer_state: HashMap<Url, PeerWorkState> = HashMap::new();
        let mut data = LatencyData::default();
        let url = test_url("pending");
        let now = Instant::now();

        // Newly touched peer: no estimate yet (initial ping in flight).
        peer_state.insert(
            url.clone(),
            PeerWorkState {
                space: mock_space::stub_space(),
                last_touched_at: now,
            },
        );

        prune_peer_state_at(&mut peer_state, &mut data, now);
        assert!(peer_state.contains_key(&url));
    }

    #[test]
    fn prune_drops_abandoned_ping_failed_entries() {
        let mut peer_state: HashMap<Url, PeerWorkState> = HashMap::new();
        let mut data = LatencyData::default();
        let url = test_url("dead");
        let now = Instant::now();

        peer_state.insert(
            url.clone(),
            PeerWorkState {
                space: mock_space::stub_space(),
                last_touched_at: now,
            },
        );
        for _ in 0..CONSECUTIVE_FAILURE_THRESHOLD {
            data.record_failure(&url);
        }
        assert!(data.has_failed_pings(&url));

        let future = future_instant(EVICTION_DURATION + Duration::from_secs(60));
        prune_peer_state_at(&mut peer_state, &mut data, future);

        assert!(!peer_state.contains_key(&url));
        // Failure entry dropped alongside peer_state entry.
        assert!(!data.consecutive_failures.contains_key(&url));
        assert!(!data.has_failed_pings(&url));
    }

    #[test]
    fn prune_drops_entries_with_evicted_estimate_and_stale_touch() {
        let mut peer_state: HashMap<Url, PeerWorkState> = HashMap::new();
        let mut data = LatencyData::default();
        let url = test_url("abandoned");
        let now = Instant::now();

        data.record_sample(url.clone(), Duration::from_millis(50));
        peer_state.insert(
            url.clone(),
            PeerWorkState {
                space: mock_space::stub_space(),
                last_touched_at: now,
            },
        );

        let future = future_instant(EVICTION_DURATION + Duration::from_secs(60));
        data.evict_stale_at(future);
        prune_peer_state_at(&mut peer_state, &mut data, future);

        assert!(!peer_state.contains_key(&url));
    }
}