ig_client/utils/
tools.rs

1use crate::constants::{ERROR_COOLDOWN_SECONDS, MAX_CONSECUTIVE_ERRORS};
2use std::time::Duration;
3use tokio::time;
4use tracing::warn;
5
6/// Apply exponential backoff when too many errors occur
7pub async fn apply_backoff(consecutive_errors: &mut u32) {
8    let backoff_time =
9        ERROR_COOLDOWN_SECONDS * (2_u64.pow(*consecutive_errors - MAX_CONSECUTIVE_ERRORS));
10    let capped_backoff = backoff_time.min(3600); // Cap at 1 hour max
11
12    warn!(
13        "Hit maximum consecutive errors ({}). Entering cooldown period of {} seconds",
14        MAX_CONSECUTIVE_ERRORS, capped_backoff
15    );
16
17    // Pause for cooldown period
18    time::sleep(Duration::from_secs(capped_backoff)).await;
19    *consecutive_errors = 0; // Reset after cooldown
20}