Skip to content

Retry Logic with Exponential Backoff #182

@mftee

Description

@mftee

Add retry logic with exponential backoff to all Stellar Horizon API calls inside StellarClient. Network failures and transient Horizon errors should be retried automatically rather than immediately failing.

Requirements:

  • Create a RetryConfig struct:
    pub struct RetryConfig {
        pub max_attempts: u32,
        pub initial_delay_ms: u64,
        pub max_delay_ms: u64,
        pub backoff_multiplier: f64,
    }
  • Implement a generic retry_async<F, T>(config: &RetryConfig, operation: F) -> Result<T> utility function where F is an async closure
  • Apply jitter to the delay: actual_delay = delay * (0.8 + random * 0.4) using rand::random::<f64>()
  • Retry only on:
    • Network errors (connection refused, timeout)
    • Horizon 5xx HTTP responses
    • Do NOT retry on 4xx responses
  • Log each retry attempt at warn! level with attempt number and delay
  • Apply this to verify_hash, submit_hash, and get_hash_history
  • Make max_attempts configurable via STELLAR_MAX_RETRIES env var (default: 3)

Acceptance Criteria:

  • retry_async correctly stops after max_attempts
  • Backoff delay increases between retries with jitter
  • 4xx responses are NOT retried
  • Unit tests for the retry logic using mock operations that fail N times then succeed

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions