rialo-types 0.1.10

Rialo Types
Documentation
// Copyright (c) Subzero Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use getset::CopyGetters;
use serde_derive::{Deserialize, Serialize};

/// Configuration for handling failed oracle duties with retry logic.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, CopyGetters)]
#[getset(get_copy = "pub")]
pub struct RetryConfig {
    /// Number of retries, not the number of attempts.
    /// The value of 0 means a single attempt with no retries.
    num_retries: u32,

    /// Delay between retries, in rounds.
    retry_delay: u32,
}

impl RetryConfig {
    /// Default delay between retries, in rounds.
    const DEFAULT_RETRY_DELAY: u32 = 100;

    pub fn new(num_retries: u32, retry_delay: u32) -> Self {
        Self {
            num_retries,
            retry_delay,
        }
    }

    /// Creates a retry configuration for a single attempt with no retries.
    pub fn single_attempt() -> Self {
        Self {
            num_retries: 0,
            retry_delay: Self::DEFAULT_RETRY_DELAY,
        }
    }

    /// Creates a retry configuration with the specified number of retries.
    ///
    /// # Arguments
    ///
    /// * `num_retries` - Number of retry attempts after the initial attempt fails.
    pub fn with_retries(num_retries: u32) -> Self {
        Self {
            num_retries,
            retry_delay: Self::DEFAULT_RETRY_DELAY,
        }
    }
}

impl Default for RetryConfig {
    /// Returns a single attempt with no retries.
    fn default() -> Self {
        Self::single_attempt()
    }
}