retry_subprocess_with_backoff

Function retry_subprocess_with_backoff 

Source
pub fn retry_subprocess_with_backoff<F>(
    operation: F,
    max_retries: u32,
    initial_delay: Duration,
    operation_name: &str,
) -> Result<()>
where F: FnMut() -> Result<ExitStatus>,
Expand description

Retry a subprocess execution with exponential backoff on connection errors

Executes a subprocess command with automatic retry on connection-related failures. Each retry doubles the delay (exponential backoff) to handle transient connection issues.

Connection errors are detected by checking:

  • Non-zero exit codes
  • Stderr output containing connection-related error patterns:
    • “connection closed”
    • “connection refused”
    • “could not connect”
    • “server closed the connection”
    • “timeout”
    • “Connection timed out”

§Arguments

  • operation - Function that executes a Command and returns the exit status
  • max_retries - Maximum number of retry attempts (0 = no retries, just initial attempt)
  • initial_delay - Delay before first retry (doubles each subsequent retry)
  • operation_name - Name of the operation for logging (e.g., “pg_restore”, “psql”)

§Returns

Returns Ok(()) on success or the last error after all retries exhausted.

§Examples

retry_subprocess_with_backoff(
    || {
        let mut cmd = Command::new("psql");
        cmd.arg("--version");
        cmd.status().map_err(anyhow::Error::from)
    },
    3,  // Try up to 3 times
    Duration::from_secs(1),  // Start with 1s delay
    "psql"
)?;