#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RetryReason {
Transient,
FeatureToggle,
AntiBot,
}
#[derive(Debug, Clone)]
pub struct RetryTracker {
total: u32,
feature_toggle: u32,
anti_bot: u32,
max_total: u32,
max_feature_toggle: u32,
max_anti_bot: u32,
}
impl Default for RetryTracker {
fn default() -> Self {
Self::new_default()
}
}
impl RetryTracker {
#[must_use]
pub fn new_default() -> Self {
Self {
total: 0,
feature_toggle: 0,
anti_bot: 0,
max_total: 5,
max_feature_toggle: 3,
max_anti_bot: 2,
}
}
#[must_use]
pub fn new(max_total: u32, max_feature_toggle: u32, max_anti_bot: u32) -> Self {
Self {
total: 0,
feature_toggle: 0,
anti_bot: 0,
max_total,
max_feature_toggle,
max_anti_bot,
}
}
pub fn record(&mut self, r: RetryReason) {
self.total = self.total.saturating_add(1);
match r {
RetryReason::Transient => {}
RetryReason::FeatureToggle => {
self.feature_toggle = self.feature_toggle.saturating_add(1);
}
RetryReason::AntiBot => {
self.anti_bot = self.anti_bot.saturating_add(1);
}
}
}
pub fn should_retry(&self, r: RetryReason) -> bool {
if self.total >= self.max_total {
return false;
}
match r {
RetryReason::Transient => true,
RetryReason::FeatureToggle => self.feature_toggle < self.max_feature_toggle,
RetryReason::AntiBot => self.anti_bot < self.max_anti_bot,
}
}
#[must_use]
pub fn total(&self) -> u32 {
self.total
}
#[must_use]
pub fn feature_toggle(&self) -> u32 {
self.feature_toggle
}
#[must_use]
pub fn anti_bot(&self) -> u32 {
self.anti_bot
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn retry_reason_equality() {
assert_eq!(RetryReason::Transient, RetryReason::Transient);
assert_eq!(RetryReason::FeatureToggle, RetryReason::FeatureToggle);
assert_eq!(RetryReason::AntiBot, RetryReason::AntiBot);
assert_ne!(RetryReason::Transient, RetryReason::AntiBot);
assert_ne!(RetryReason::FeatureToggle, RetryReason::Transient);
assert_ne!(RetryReason::AntiBot, RetryReason::FeatureToggle);
}
#[test]
fn retry_reason_copy_clone() {
let reason = RetryReason::AntiBot;
let cloned = reason;
assert_eq!(reason, cloned);
}
#[test]
fn retry_reason_debug_format() {
assert_eq!(format!("{:?}", RetryReason::Transient), "Transient");
assert_eq!(format!("{:?}", RetryReason::FeatureToggle), "FeatureToggle");
assert_eq!(format!("{:?}", RetryReason::AntiBot), "AntiBot");
}
#[test]
fn retry_reason_hash_consistency() {
use std::collections::HashSet;
let mut set = HashSet::new();
set.insert(RetryReason::Transient);
set.insert(RetryReason::Transient);
assert_eq!(set.len(), 1);
set.insert(RetryReason::AntiBot);
assert_eq!(set.len(), 2);
}
#[test]
fn tracker_default_initial_counts_zero() {
let t = RetryTracker::new_default();
assert_eq!(t.total(), 0);
assert_eq!(t.feature_toggle(), 0);
assert_eq!(t.anti_bot(), 0);
}
#[test]
fn tracker_default_allows_initial_retry() {
let t = RetryTracker::new_default();
assert!(t.should_retry(RetryReason::Transient));
assert!(t.should_retry(RetryReason::FeatureToggle));
assert!(t.should_retry(RetryReason::AntiBot));
}
#[test]
fn tracker_antibot_cap_stops_only_antibot() {
let mut t = RetryTracker::new_default();
t.record(RetryReason::AntiBot);
assert!(
t.should_retry(RetryReason::AntiBot),
"after 1 antibot: still allowed"
);
assert!(t.should_retry(RetryReason::Transient), "transient still ok");
t.record(RetryReason::AntiBot);
assert!(
!t.should_retry(RetryReason::AntiBot),
"after 2 antibot records: should be blocked"
);
assert!(
t.should_retry(RetryReason::Transient),
"transient must still retry when antibot cap reached"
);
}
#[test]
fn tracker_feature_toggle_cap_stops_only_ft() {
let mut t = RetryTracker::new_default();
t.record(RetryReason::FeatureToggle);
t.record(RetryReason::FeatureToggle);
t.record(RetryReason::FeatureToggle);
assert!(
!t.should_retry(RetryReason::FeatureToggle),
"after 3 feature_toggle: blocked"
);
assert!(t.should_retry(RetryReason::Transient), "transient still ok");
assert!(t.should_retry(RetryReason::AntiBot), "antibot still ok");
}
#[test]
fn tracker_max_total_blocks_all() {
let mut t = RetryTracker::new_default();
for _ in 0..5 {
t.record(RetryReason::Transient);
}
assert_eq!(t.total(), 5);
assert!(!t.should_retry(RetryReason::Transient));
assert!(!t.should_retry(RetryReason::FeatureToggle));
assert!(!t.should_retry(RetryReason::AntiBot));
}
#[test]
fn tracker_record_increments_correct_counter() {
let mut t = RetryTracker::new_default();
t.record(RetryReason::Transient);
assert_eq!(t.total(), 1);
assert_eq!(t.feature_toggle(), 0);
assert_eq!(t.anti_bot(), 0);
t.record(RetryReason::FeatureToggle);
assert_eq!(t.total(), 2);
assert_eq!(t.feature_toggle(), 1);
assert_eq!(t.anti_bot(), 0);
t.record(RetryReason::AntiBot);
assert_eq!(t.total(), 3);
assert_eq!(t.feature_toggle(), 1);
assert_eq!(t.anti_bot(), 1);
}
#[test]
fn tracker_custom_limits() {
let mut t = RetryTracker::new(10, 1, 1);
t.record(RetryReason::AntiBot);
assert!(
!t.should_retry(RetryReason::AntiBot),
"custom max_anti_bot=1"
);
assert!(t.should_retry(RetryReason::Transient), "total=1 < 10");
assert!(t.should_retry(RetryReason::FeatureToggle), "ft=0 < 1");
}
#[test]
fn tracker_mixed_reasons_partial_block() {
let mut t = RetryTracker::new_default();
t.record(RetryReason::AntiBot);
t.record(RetryReason::AntiBot); t.record(RetryReason::FeatureToggle);
t.record(RetryReason::Transient);
assert!(!t.should_retry(RetryReason::AntiBot));
assert!(t.should_retry(RetryReason::FeatureToggle)); assert!(t.should_retry(RetryReason::Transient)); t.record(RetryReason::Transient);
assert_eq!(t.total(), 5);
assert!(!t.should_retry(RetryReason::Transient));
}
#[test]
fn tracker_saturating_record_no_overflow() {
let mut t = RetryTracker::new(3, 1, 1);
for _ in 0..1000 {
t.record(RetryReason::Transient);
}
assert_eq!(t.total(), 1000);
assert!(!t.should_retry(RetryReason::Transient));
assert!(!t.should_retry(RetryReason::FeatureToggle));
assert!(!t.should_retry(RetryReason::AntiBot));
}
}