1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 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()
}
}