#[derive(Clone, Debug)]
pub struct RPCRetryConfig {
pub max_retries: usize,
pub initial_backoff_ms: u64,
pub max_backoff_ms: u64,
}
impl RPCRetryConfig {
pub fn new(max_retries: usize, initial_backoff_ms: u64, max_backoff_ms: u64) -> Self {
Self { max_retries, initial_backoff_ms, max_backoff_ms }
}
}
impl Default for RPCRetryConfig {
fn default() -> Self {
Self { max_retries: 3, initial_backoff_ms: 100, max_backoff_ms: 5000 }
}
}
#[derive(Clone, Debug, Default)]
pub enum RPCBatchingConfig {
#[default]
Disabled,
Enabled {
max_batch_size: usize,
storage_slot_max_batch_size_override: Option<usize>,
},
}
impl RPCBatchingConfig {
pub fn enabled_with_defaults() -> Self {
Self::Enabled { max_batch_size: 50, storage_slot_max_batch_size_override: Some(1000) }
}
pub fn max_batch_size(&self) -> Option<usize> {
match self {
Self::Enabled { max_batch_size, .. } => Some(*max_batch_size),
Self::Disabled => None,
}
}
pub fn storage_slot_max_batch_size(&self) -> Option<usize> {
match self {
Self::Enabled { max_batch_size, storage_slot_max_batch_size_override } => {
Some(storage_slot_max_batch_size_override.unwrap_or(*max_batch_size))
}
Self::Disabled => None,
}
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[test]
fn test_enabled_batching_config_batch_size() {
let config = RPCBatchingConfig::Enabled {
max_batch_size: 50,
storage_slot_max_batch_size_override: None,
};
assert_eq!(config.max_batch_size(), Some(50));
assert_eq!(config.storage_slot_max_batch_size(), Some(50));
}
#[test]
fn test_disabled_batching_config() {
assert_eq!(RPCBatchingConfig::Disabled.max_batch_size(), None);
assert_eq!(RPCBatchingConfig::Disabled.storage_slot_max_batch_size(), None);
}
#[rstest]
#[case::override_higher_than_max(50, 1000)]
#[case::override_lower_than_max(1000, 50)]
fn test_storage_slot_max_batch_size_uses_override(
#[case] max_batch_size: usize,
#[case] override_size: usize,
) {
let config = RPCBatchingConfig::Enabled {
max_batch_size,
storage_slot_max_batch_size_override: Some(override_size),
};
assert_eq!(config.storage_slot_max_batch_size(), Some(override_size));
}
}