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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/// Macro used to wrap client-only modules
#[macro_export]
macro_rules! cfg_client {
($($item:item)*) => {
$(
#[cfg(feature = "client")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "client")))]
$item
)*
}
}
/// Macro used to wrap exclusively non-client modules
#[macro_export]
macro_rules! cfg_not_client {
($($item:item)*) => {
$(
#[cfg(not(feature = "client"))]
#[cfg_attr(doc_cfg, doc(cfg(not(feature = "client"))))]
$item
)*
}
}
/// Macro used to wrap client-only modules
#[macro_export]
macro_rules! cfg_ipfs{
($($item:item)*) => {
$(
#[cfg(feature = "ipfs")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "ipfs")))]
$item
)*
}
}
// /// Macro used to wrap solana exclusive modules
// #[macro_export]
// macro_rules! cfg_solana {
// ($($item:item)*) => {
// $(
// #[cfg(feature = "solana")]
// #[cfg_attr(doc_cfg, doc(cfg(feature = "solana")))]
// $item
// )*
// }
// }
/// Retry a given expression a specified number of times with a delay between each attempt.
///
/// # Arguments
///
/// * `attempts` - The number of attempts to make.
/// * `delay_ms` - The delay in milliseconds between each attempt.
/// * `expr` - The expression to be retried.
///
/// # Returns
///
/// Returns a `Result` containing the value of the expression if it succeeds within the specified number of attempts,
/// or an error if it fails on all attempts.
///
/// # Examples
/// ```
/// use switchboard_common::retry;
///
/// // Retry a blockhash fetch 3 times with a delay of 500ms in between each failure
/// let blockhash = retry!(3, 500, rpc.get_latest_blockhash().await)
/// .await
/// .map_err(|_| SbError::SolanaBlockhashError)?;
/// ```
#[cfg(not(target_os = "solana"))]
#[cfg_attr(doc_cfg, doc(cfg(not(target_os = "solana"))))]
#[macro_export]
macro_rules! retry {
($attempts:expr, $delay_ms:expr, $expr:expr) => {{
async {
let mut attempts = $attempts;
loop {
match $expr {
Ok(val) => break Ok(val),
Err(_) if attempts > 1 => {
attempts -= 1;
tokio::time::sleep(tokio::time::Duration::from_millis($delay_ms)).await;
}
Err(e) => break Err(e),
}
}
}
}};
}
/// Retry a given expression a specified number of times with a delay between each attempt.
/// This will block the current thread until a value is resolved or the maximum number of attempts is reached.
///
/// # Arguments
///
/// * `attempts` - The number of attempts to make.
/// * `delay_ms` - The delay in milliseconds between each attempt.
/// * `expr` - The expression to be retried.
///
/// # Returns
///
/// Returns a `Result` containing the value of the expression if it succeeds within the specified number of attempts,
/// or an error if it fails on all attempts.
///
/// # Examples
/// ```
/// use switchboard_common::blocking_retry;
///
/// // Retry a blockhash fetch 3 times with a delay of 500ms in between each failure
/// let blockhash = blocking_retry!(3, 500, rpc.get_latest_blockhash())
/// .map_err(|_| SbError::SolanaBlockhashError)?;
/// ```
#[cfg(not(target_os = "solana"))]
#[cfg_attr(doc_cfg, doc(cfg(not(target_os = "solana"))))]
#[macro_export]
macro_rules! blocking_retry {
($attempts:expr, $delay_ms:expr, $expr:expr) => {{
let mut attempts = $attempts;
loop {
match $expr {
Ok(val) => break Ok(val),
Err(_) if attempts > 1 => {
attempts -= 1;
std::thread::sleep(tokio::time::Duration::from_millis($delay_ms));
}
Err(e) => break Err(e),
}
}
}};
}