subscription-proxy-pool 0.1.0

Subscription-backed HTTP and SOCKS proxy pools with caching, health checks, and request-count or time-based rotation
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
//! Request-count and elapsed-time policies for selecting the next proxy.

use std::{
    collections::HashMap,
    num::NonZeroU64,
    sync::Arc,
    time::{Duration, Instant},
};

use rand::seq::SliceRandom;

/// How a replacement is chosen from the currently eligible proxies.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SelectionStrategy {
    /// Visit proxies in candidate-list order, wrapping at the end.
    #[default]
    RoundRobin,
    /// Choose uniformly among eligible proxies other than the current one.
    Random,
    /// Visit every eligible proxy once in a randomly shuffled round.
    /// New and recovered proxies join the next round; unavailable proxies are
    /// skipped. Consecutive rounds do not immediately repeat a proxy when there
    /// is an alternative. Request/time limits still apply to each selection.
    ShuffledRoundRobin,
}

/// Limits controlling how long the pool retains its current proxy.
///
/// Each successful acquisition consumes one request allowance, including an
/// acquisition whose eventual network request fails. When either enabled limit
/// is reached, the *next* acquisition selects a replacement. Time limits are
/// checked during acquisition; they do not schedule background work.
///
/// With both limits disabled, the current proxy is retained until it becomes
/// unavailable or a caller requests a manual rotation. A replacement avoids the
/// current proxy whenever another eligible proxy exists.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RotationPolicy {
    /// Maximum acquisitions per proxy selection; `None` disables this limit.
    pub requests_per_proxy: Option<NonZeroU64>,
    /// Maximum elapsed time per selection; `None` disables this limit.
    pub max_age: Option<Duration>,
    /// Method used to choose the next eligible proxy.
    pub strategy: SelectionStrategy,
}

impl Default for RotationPolicy {
    fn default() -> Self {
        Self {
            requests_per_proxy: NonZeroU64::new(1),
            max_age: None,
            strategy: SelectionStrategy::RoundRobin,
        }
    }
}

impl RotationPolicy {
    /// Keep each selection for this many acquisitions before rotating.
    pub fn every(requests: NonZeroU64) -> Self {
        Self {
            requests_per_proxy: Some(requests),
            ..Self::default()
        }
    }

    /// Keep a proxy until it becomes unavailable or rotation is requested.
    pub fn sticky() -> Self {
        Self {
            requests_per_proxy: None,
            ..Self::default()
        }
    }

    /// Keep each selection for a positive duration, regardless of request count.
    /// The time limit is checked on acquisition; no background timer is needed.
    pub fn for_duration(duration: Duration) -> crate::Result<Self> {
        let policy = Self {
            max_age: Some(duration),
            ..Self::sticky()
        };
        policy.validate()?;
        Ok(policy)
    }

    /// Rejects a zero time limit; use a request limit of one to rotate every time.
    pub fn validate(&self) -> crate::Result<()> {
        if self.max_age.is_some_and(|age| age.is_zero()) {
            return Err(crate::Error::Config(
                "rotation max_age must be greater than zero",
            ));
        }
        Ok(())
    }
}

/// The pool serializes access to this state together with its eligible nodes.
#[derive(Debug, Default)]
pub(crate) struct RotationState {
    current: Option<String>,
    requests: u64,
    selected_at: Option<Instant>,
    rotate_next: bool,
    // The pool reuses this allocation while eligibility is unchanged. Keeping
    // its order also preserves the successor when the current node disappears.
    candidates: Option<Arc<[String]>>,
    positions: HashMap<String, usize>,
    shuffled_remaining: Vec<usize>,
    shuffled_started: bool,
    #[cfg(test)]
    snapshot_rebuilds: usize,
}

impl RotationState {
    #[cfg(test)]
    fn select(
        &mut self,
        candidates: &[String],
        policy: &RotationPolicy,
        now: Instant,
    ) -> Option<String> {
        // Unit tests may construct small slices; the production pool passes a
        // shared snapshot so stable acquisitions never copy or scan all IDs.
        let candidates = match &self.candidates {
            Some(previous) if previous.as_ref() == candidates => Arc::clone(previous),
            _ => Arc::from(candidates),
        };
        self.select_shared(&candidates, policy, now)
    }

    pub(crate) fn select_shared(
        &mut self,
        candidates: &Arc<[String]>,
        policy: &RotationPolicy,
        now: Instant,
    ) -> Option<String> {
        if candidates.is_empty() {
            // Preserve the previous position across temporary total outages.
            self.rotate_next = true;
            return None;
        }

        let removed_successor = self.update_candidates(candidates);

        let quota_reached = policy
            .requests_per_proxy
            .is_some_and(|limit| self.requests >= limit.get());
        let age_reached = policy.max_age.is_some_and(|limit| {
            self.selected_at
                .is_some_and(|started| now.saturating_duration_since(started) >= limit)
        });
        let current_index = self
            .current
            .as_ref()
            .and_then(|current| self.positions.get(current))
            .copied();

        if current_index.is_some() && !self.rotate_next && !quota_reached && !age_reached {
            if policy.strategy == SelectionStrategy::ShuffledRoundRobin && !self.shuffled_started {
                // A session can receive its first node from the pool's shared
                // allocator. That node counts as visited in this shuffled round.
                self.start_shuffled_round(candidates.len(), current_index);
            }
            self.requests = self.requests.saturating_add(1);
            return self.current.clone();
        }

        let index = match policy.strategy {
            SelectionStrategy::RoundRobin => current_index
                .map(|index| (index + 1) % candidates.len())
                .or(removed_successor)
                .unwrap_or(0),
            SelectionStrategy::Random => Self::next_random(candidates.len(), current_index),
            SelectionStrategy::ShuffledRoundRobin => {
                self.next_shuffled(candidates.len(), current_index)
            }
        };
        let selected = candidates[index].clone();
        self.current = Some(selected.clone());
        self.requests = 1;
        self.selected_at = Some(now);
        self.rotate_next = false;
        Some(selected)
    }

    pub(crate) fn has_current(&self) -> bool {
        self.current.is_some()
    }

    /// Start a new session at a globally allocated node without consuming quota.
    pub(crate) fn seed(&mut self, id: &str, now: Instant) {
        self.current = Some(id.to_owned());
        self.requests = 0;
        self.selected_at = Some(now);
        self.rotate_next = false;
        self.shuffled_remaining.clear();
        self.shuffled_started = false;
    }

    pub(crate) fn invalidate(&mut self, id: &str) {
        if self.current.as_deref() == Some(id) {
            self.force_rotate();
        }
    }

    pub(crate) fn force_rotate(&mut self) {
        self.rotate_next = true;
    }

    // Returns the removed current node's next surviving successor. All work
    // proportional to pool size happens only when the shared snapshot changes.
    fn update_candidates(&mut self, candidates: &Arc<[String]>) -> Option<usize> {
        if self
            .candidates
            .as_ref()
            .is_some_and(|previous| Arc::ptr_eq(previous, candidates))
        {
            return None;
        }
        let positions: HashMap<_, _> = candidates
            .iter()
            .enumerate()
            .map(|(index, id)| (id.clone(), index))
            .collect();
        let successor = self.current.as_ref().and_then(|current| {
            if positions.contains_key(current) {
                return None;
            }
            let index = *self.positions.get(current)?;
            let previous = self.candidates.as_ref()?;
            previous[index + 1..]
                .iter()
                .chain(&previous[..index])
                .find_map(|id| positions.get(id).copied())
        });
        if let Some(previous) = &self.candidates {
            self.shuffled_remaining = self
                .shuffled_remaining
                .iter()
                .filter_map(|index| positions.get(&previous[*index]).copied())
                .collect();
        }
        self.positions = positions;
        self.candidates = Some(Arc::clone(candidates));
        #[cfg(test)]
        {
            self.snapshot_rebuilds += 1;
        }
        successor
    }

    // Uniformly sample the index range with the current node removed, without
    // allocating or scanning an alternative-node collection.
    fn next_random(len: usize, current: Option<usize>) -> usize {
        if len == 1 {
            return 0;
        }
        if let Some(current) = current {
            let index = rand::random_range(0..len - 1);
            index + usize::from(index >= current)
        } else {
            rand::random_range(0..len)
        }
    }

    fn start_shuffled_round(&mut self, len: usize, already_visited: Option<usize>) {
        self.shuffled_remaining = (0..len)
            .filter(|index| Some(*index) != already_visited)
            .collect();
        self.shuffled_remaining.shuffle(&mut rand::rng());
        self.shuffled_started = true;
    }

    fn next_shuffled(&mut self, len: usize, current: Option<usize>) -> usize {
        if self.shuffled_remaining.is_empty() {
            self.start_shuffled_round(len, None);
            let last = self.shuffled_remaining.len() - 1;
            if len > 1 && self.shuffled_remaining.last().copied() == current {
                self.shuffled_remaining.swap(0, last);
            }
        }
        self.shuffled_remaining
            .pop()
            .expect("a shuffled round is nonempty for a nonempty candidate list")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{
        collections::HashMap,
        sync::{Arc, Mutex},
        thread,
    };

    fn nodes(ids: &[&str]) -> Vec<String> {
        ids.iter().map(|id| (*id).to_owned()).collect()
    }

    fn every(requests: u64) -> RotationPolicy {
        RotationPolicy {
            requests_per_proxy: NonZeroU64::new(requests),
            ..RotationPolicy::default()
        }
    }

    fn select(
        state: &mut RotationState,
        nodes: &[String],
        policy: &RotationPolicy,
        now: Instant,
    ) -> String {
        state.select(nodes, policy, now).expect("eligible nodes")
    }

    #[test]
    fn an_explicit_batch_rotates_on_twenty_first_acquisition() {
        let mut state = RotationState::default();
        let nodes = nodes(&["a", "b", "c"]);
        let now = Instant::now();
        for expected in ["a", "b", "c", "a"] {
            for _ in 0..20 {
                assert_eq!(select(&mut state, &nodes, &every(20), now), expected);
            }
        }
    }

    #[test]
    fn default_visits_every_node_in_order_without_concentrating_a_batch() {
        let mut state = RotationState::default();
        let nodes = nodes(&["a", "b", "c"]);
        let now = Instant::now();
        let actual: Vec<_> = (0..7)
            .map(|_| select(&mut state, &nodes, &RotationPolicy::default(), now))
            .collect();
        assert_eq!(actual, ["a", "b", "c", "a", "b", "c", "a"]);
    }

    #[test]
    fn elapsed_time_rotates_at_exact_boundary_and_resets_the_clock() {
        let mut state = RotationState::default();
        let nodes = nodes(&["a", "b"]);
        let start = Instant::now();
        let policy = RotationPolicy {
            requests_per_proxy: None,
            max_age: Some(Duration::from_secs(10)),
            ..RotationPolicy::default()
        };
        assert_eq!(select(&mut state, &nodes, &policy, start), "a");
        assert_eq!(
            select(
                &mut state,
                &nodes,
                &policy,
                start + Duration::from_millis(9_999)
            ),
            "a"
        );
        assert_eq!(
            select(&mut state, &nodes, &policy, start + Duration::from_secs(10)),
            "b"
        );
        assert_eq!(
            select(&mut state, &nodes, &policy, start + Duration::from_secs(19)),
            "b"
        );
        assert_eq!(
            select(&mut state, &nodes, &policy, start + Duration::from_secs(20)),
            "a"
        );
    }

    #[test]
    fn either_limit_triggers_rotation() {
        let mut state = RotationState::default();
        let nodes = nodes(&["a", "b", "c"]);
        let start = Instant::now();
        let policy = RotationPolicy {
            max_age: Some(Duration::from_secs(10)),
            ..every(2)
        };
        assert_eq!(select(&mut state, &nodes, &policy, start), "a");
        assert_eq!(select(&mut state, &nodes, &policy, start), "a");
        assert_eq!(select(&mut state, &nodes, &policy, start), "b");
        assert_eq!(
            select(&mut state, &nodes, &policy, start + Duration::from_secs(10)),
            "c"
        );
        assert_eq!(
            select(&mut state, &nodes, &policy, start + Duration::from_secs(10)),
            "c"
        );
        assert_eq!(
            select(&mut state, &nodes, &policy, start + Duration::from_secs(10)),
            "a"
        );
    }

    #[test]
    fn random_rotation_never_repeats_when_an_alternative_exists() {
        let mut state = RotationState::default();
        let nodes = nodes(&["a", "b", "c", "d"]);
        let now = Instant::now();
        let policy = RotationPolicy {
            strategy: SelectionStrategy::Random,
            ..every(1)
        };
        let mut previous = None;
        for _ in 0..1_000 {
            let selected = select(&mut state, &nodes, &policy, now);
            assert!(nodes.contains(&selected));
            assert_ne!(previous.as_ref(), Some(&selected));
            previous = Some(selected);
        }
    }

    #[test]
    fn shuffled_rounds_cover_every_node_without_repeating_at_the_boundary() {
        let mut state = RotationState::default();
        let candidates: Arc<[String]> = (0..16).map(|index| index.to_string()).collect();
        let policy = RotationPolicy {
            strategy: SelectionStrategy::ShuffledRoundRobin,
            ..RotationPolicy::default()
        };
        let now = Instant::now();
        let mut previous = None;
        for _ in 0..20 {
            let mut visited = std::collections::HashSet::new();
            for _ in 0..candidates.len() {
                let selected = state.select_shared(&candidates, &policy, now).unwrap();
                assert_ne!(previous.as_ref(), Some(&selected));
                assert!(visited.insert(selected.clone()));
                previous = Some(selected);
            }
            assert_eq!(visited.len(), candidates.len());
        }
    }

    #[test]
    fn shuffled_rounds_apply_the_request_budget_to_each_visited_node() {
        let mut state = RotationState::default();
        let candidates: Arc<[String]> = nodes(&["a", "b", "c", "d"]).into();
        let policy = RotationPolicy {
            strategy: SelectionStrategy::ShuffledRoundRobin,
            ..every(3)
        };
        let now = Instant::now();
        for _ in 0..5 {
            let mut visited = std::collections::HashSet::new();
            for _ in 0..candidates.len() {
                let selected = state.select_shared(&candidates, &policy, now).unwrap();
                assert!(visited.insert(selected.clone()));
                for _ in 0..2 {
                    assert_eq!(
                        state.select_shared(&candidates, &policy, now),
                        Some(selected.clone())
                    );
                }
            }
        }
    }

    #[test]
    fn shuffled_updates_finish_surviving_nodes_before_adding_new_nodes() {
        let mut state = RotationState::default();
        let candidates: Arc<[String]> = nodes(&["a", "b", "c", "d", "e"]).into();
        let policy = RotationPolicy {
            strategy: SelectionStrategy::ShuffledRoundRobin,
            ..RotationPolicy::default()
        };
        let now = Instant::now();
        let first = state.select_shared(&candidates, &policy, now).unwrap();
        let removed = candidates[*state.shuffled_remaining.last().unwrap()].clone();
        let expected: std::collections::HashSet<_> = candidates
            .iter()
            .filter(|id| **id != first && **id != removed)
            .cloned()
            .collect();
        // Also reverse the list to verify the remaining round follows node
        // identities, rather than reusing indices from the old snapshot.
        let updated: Arc<[String]> = candidates
            .iter()
            .rev()
            .filter(|id| **id != removed)
            .cloned()
            .chain(["new".to_owned()])
            .collect();
        let actual: std::collections::HashSet<_> = (0..expected.len())
            .map(|_| state.select_shared(&updated, &policy, now).unwrap())
            .collect();
        assert_eq!(actual, expected);

        let next_round: std::collections::HashSet<_> = (0..updated.len())
            .map(|_| state.select_shared(&updated, &policy, now).unwrap())
            .collect();
        assert_eq!(next_round, updated.iter().cloned().collect());
        assert!(next_round.contains("new"));
        assert!(!next_round.contains(&removed));
    }

    #[test]
    fn seeded_sessions_start_at_the_assigned_node_without_spending_quota() {
        let candidates: Arc<[String]> = nodes(&["a", "b", "c", "d"]).into();
        let now = Instant::now();
        let mut state = RotationState::default();
        assert!(!state.has_current());
        state.seed("c", now);
        assert!(state.has_current());
        for _ in 0..3 {
            assert_eq!(
                state.select_shared(&candidates, &every(3), now).as_deref(),
                Some("c")
            );
        }
        assert_eq!(
            state.select_shared(&candidates, &every(3), now).as_deref(),
            Some("d")
        );

        let policy = RotationPolicy {
            strategy: SelectionStrategy::ShuffledRoundRobin,
            ..RotationPolicy::default()
        };
        state.seed("b", now);
        assert_eq!(
            state.select_shared(&candidates, &policy, now).as_deref(),
            Some("b")
        );
        let remaining: std::collections::HashSet<_> = (0..3)
            .map(|_| state.select_shared(&candidates, &policy, now).unwrap())
            .collect();
        assert_eq!(remaining, nodes(&["a", "c", "d"]).into_iter().collect());
    }

    #[test]
    fn large_shared_snapshots_are_indexed_only_when_the_allocation_changes() {
        let candidates: Arc<[String]> = (0..4_096).map(|index| index.to_string()).collect();
        let now = Instant::now();
        for strategy in [
            SelectionStrategy::RoundRobin,
            SelectionStrategy::Random,
            SelectionStrategy::ShuffledRoundRobin,
        ] {
            let mut state = RotationState::default();
            let policy = RotationPolicy {
                strategy,
                ..every(7)
            };
            for _ in 0..20_000 {
                state.select_shared(&candidates, &policy, now).unwrap();
            }
            assert_eq!(state.snapshot_rebuilds, 1);
            assert!(Arc::ptr_eq(state.candidates.as_ref().unwrap(), &candidates));

            let new_snapshot: Arc<[String]> = candidates.to_vec().into();
            state.select_shared(&new_snapshot, &policy, now).unwrap();
            assert_eq!(state.snapshot_rebuilds, 2);
            assert!(Arc::ptr_eq(
                state.candidates.as_ref().unwrap(),
                &new_snapshot
            ));
        }
    }

    #[test]
    fn manual_rotation_preserves_the_round_robin_position() {
        let mut state = RotationState::default();
        let nodes = nodes(&["a", "b", "c"]);
        let now = Instant::now();
        let policy = every(0); // Both limits disabled.
        for expected in ["a", "b", "c", "a"] {
            for _ in 0..30 {
                assert_eq!(select(&mut state, &nodes, &policy, now), expected);
            }
            state.force_rotate();
        }
    }

    #[test]
    fn invalidation_only_forces_rotation_for_the_current_node() {
        let mut state = RotationState::default();
        let nodes = nodes(&["a", "b", "c"]);
        let now = Instant::now();
        let policy = every(20);
        assert_eq!(select(&mut state, &nodes, &policy, now), "a");
        state.invalidate("b");
        assert_eq!(select(&mut state, &nodes, &policy, now), "a");
        state.invalidate("a");
        assert_eq!(select(&mut state, &nodes, &policy, now), "b");
    }

    #[test]
    fn removal_continues_from_the_removed_nodes_successor_and_recovery_rejoins() {
        let mut state = RotationState::default();
        let all = nodes(&["a", "b", "c", "d"]);
        let now = Instant::now();
        let policy = every(1);
        assert_eq!(select(&mut state, &all, &policy, now), "a");
        assert_eq!(select(&mut state, &all, &policy, now), "b");
        state.invalidate("b");
        assert_eq!(
            select(&mut state, &nodes(&["a", "c", "d"]), &policy, now),
            "c"
        );
        assert_eq!(select(&mut state, &all, &policy, now), "d");
        assert_eq!(select(&mut state, &all, &policy, now), "a");
        assert_eq!(select(&mut state, &all, &policy, now), "b");
    }

    #[test]
    fn removal_skips_missing_successors_and_wraps() {
        let mut state = RotationState::default();
        let all = nodes(&["a", "b", "c", "d"]);
        let now = Instant::now();
        let policy = every(1);
        assert_eq!(select(&mut state, &all, &policy, now), "a");
        assert_eq!(select(&mut state, &all, &policy, now), "b");
        assert_eq!(select(&mut state, &nodes(&["a", "d"]), &policy, now), "d");
        assert_eq!(select(&mut state, &nodes(&["a", "new"]), &policy, now), "a");
        assert_eq!(select(&mut state, &nodes(&["new"]), &policy, now), "new");
    }

    #[test]
    fn list_changes_do_not_reset_a_valid_nodes_request_allowance() {
        let mut state = RotationState::default();
        let now = Instant::now();
        let policy = every(3);
        assert_eq!(select(&mut state, &nodes(&["a", "b"]), &policy, now), "a");
        assert_eq!(
            select(&mut state, &nodes(&["b", "a", "c"]), &policy, now),
            "a"
        );
        assert_eq!(select(&mut state, &nodes(&["a", "c"]), &policy, now), "a");
        assert_eq!(select(&mut state, &nodes(&["a", "c"]), &policy, now), "c");
    }

    #[test]
    fn empty_list_preserves_position_until_nodes_return() {
        let mut state = RotationState::default();
        let all = nodes(&["a", "b", "c"]);
        let now = Instant::now();
        let policy = every(20);
        assert_eq!(state.select(&[], &policy, now), None);
        assert_eq!(select(&mut state, &all, &policy, now), "a");
        state.force_rotate();
        assert_eq!(select(&mut state, &all, &policy, now), "b");
        assert_eq!(state.select(&[], &policy, now), None);
        assert_eq!(select(&mut state, &all, &policy, now), "c");
    }

    #[test]
    fn a_single_node_remains_usable_with_every_strategy() {
        for strategy in [
            SelectionStrategy::RoundRobin,
            SelectionStrategy::Random,
            SelectionStrategy::ShuffledRoundRobin,
        ] {
            let mut state = RotationState::default();
            let nodes = nodes(&["only"]);
            let now = Instant::now();
            let policy = RotationPolicy {
                strategy,
                ..every(1)
            };
            for _ in 0..50 {
                assert_eq!(select(&mut state, &nodes, &policy, now), "only");
            }
            state.invalidate("only");
            assert_eq!(select(&mut state, &nodes, &policy, now), "only");
            state.force_rotate();
            assert_eq!(select(&mut state, &nodes, &policy, now), "only");
        }
    }

    #[test]
    fn count_saturates_without_limits_and_rotates_at_the_maximum_limit() {
        let now = Instant::now();
        let nodes = nodes(&["a", "b"]);
        let mut state = RotationState::default();
        assert_eq!(select(&mut state, &nodes, &every(0), now), "a");
        state.requests = u64::MAX - 1;
        assert_eq!(select(&mut state, &nodes, &every(u64::MAX), now), "a");
        assert_eq!(select(&mut state, &nodes, &every(u64::MAX), now), "b");
        state.requests = u64::MAX;
        assert_eq!(select(&mut state, &nodes, &every(0), now), "b");
        assert_eq!(state.requests, u64::MAX);
    }

    #[test]
    fn configuration_rejects_zero_time_but_allows_manual_only_rotation() {
        assert!(
            RotationPolicy {
                max_age: Some(Duration::ZERO),
                ..every(1)
            }
            .validate()
            .is_err()
        );
        assert!(every(0).validate().is_ok());
        assert!(RotationPolicy::default().validate().is_ok());
    }

    #[test]
    fn constructors_select_independent_count_time_and_sticky_policies() {
        let count = NonZeroU64::new(20).unwrap();
        let counted = RotationPolicy::every(count);
        assert_eq!(counted.requests_per_proxy, Some(count));
        assert_eq!(counted.max_age, None);

        let sticky = RotationPolicy::sticky();
        assert_eq!(sticky.requests_per_proxy, None);
        assert_eq!(sticky.max_age, None);

        let duration = Duration::from_secs(30);
        let timed = RotationPolicy::for_duration(duration).unwrap();
        assert_eq!(timed.requests_per_proxy, None);
        assert_eq!(timed.max_age, Some(duration));
        assert!(RotationPolicy::for_duration(Duration::ZERO).is_err());
    }

    #[test]
    fn concurrent_acquisitions_receive_exact_serialized_batches() {
        let state = Arc::new(Mutex::new((RotationState::default(), Vec::new())));
        let nodes = Arc::new(nodes(&["a", "b", "c", "d"]));
        let mut threads = Vec::new();
        for _ in 0..8 {
            let state = Arc::clone(&state);
            let nodes = Arc::clone(&nodes);
            threads.push(thread::spawn(move || {
                for _ in 0..100 {
                    let mut locked = state.lock().unwrap();
                    let selected = select(&mut locked.0, &nodes, &every(20), Instant::now());
                    locked.1.push(selected);
                }
            }));
        }
        for thread in threads {
            thread.join().unwrap();
        }
        let locked = state.lock().unwrap();
        let counts = locked.1.iter().fold(HashMap::new(), |mut counts, id| {
            *counts.entry(id.as_str()).or_insert(0) += 1;
            counts
        });
        for id in nodes.iter() {
            assert_eq!(counts[id.as_str()], 200);
        }
        for (index, batch) in locked.1.chunks_exact(20).enumerate() {
            assert!(batch.iter().all(|id| id == &nodes[index % nodes.len()]));
        }
    }
}