Skip to content

Commit

Permalink
refactor: Refactor the new method using the builder pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Tang <[email protected]>
  • Loading branch information
Standing-Man committed Dec 9, 2024
1 parent cd309df commit 175780c
Show file tree
Hide file tree
Showing 17 changed files with 968 additions and 317 deletions.
20 changes: 11 additions & 9 deletions crates/benchmark/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,17 @@ impl CommandRunner {

/// Create clients
async fn create_clients(&self) -> Result<Vec<BenchClient>> {
let client_options = ClientOptions::default().with_client_config(ClientConfig::new(
Duration::from_secs(10),
Duration::from_secs(5),
Duration::from_millis(250),
Duration::from_millis(10_000),
3,
true,
Duration::from_secs(1),
));
let client_options = ClientOptions::default().with_client_config(
ClientConfig::builder()
.wait_synced_timeout(Duration::from_secs(10))
.propose_timeout(Duration::from_secs(5))
.initial_retry_timeout(Duration::from_millis(250))
.max_retry_timeout(Duration::from_millis(10_000))
.retry_count(3)
.fixed_backoff(true)
.keep_alive_interval(Duration::from_secs(1))
.build(),
);
let addrs = self
.args
.endpoints
Expand Down
44 changes: 39 additions & 5 deletions crates/utils/src/config/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,47 @@ pub struct AuthConfig {
}

impl AuthConfig {
/// Generate a new `AuthConfig` object
/// Create a builder for `AuthConfig`
#[inline]
#[must_use]
pub fn builder() -> Builder {
Builder::default()
}
}

/// Builder for `AuthConfig`
#[derive(Default, Debug)]
pub struct Builder {
/// The public key file
auth_public_key: Option<PathBuf>,
/// The private key file
auth_private_key: Option<PathBuf>,
}

impl Builder {
/// Set the public key file
#[inline]
pub fn new(auth_public_key: Option<PathBuf>, auth_private_key: Option<PathBuf>) -> Self {
Self {
auth_public_key,
auth_private_key,
#[must_use]
pub fn auth_public_key(mut self, path: Option<PathBuf>) -> Self {
self.auth_public_key = path;
self
}

/// Set the private key file
#[inline]
#[must_use]
pub fn auth_private_key(mut self, path: Option<PathBuf>) -> Self {
self.auth_private_key = path;
self
}

/// Build the `AuthConfig`
#[inline]
#[must_use]
pub fn build(self) -> AuthConfig {
AuthConfig {
auth_public_key: self.auth_public_key,
auth_private_key: self.auth_private_key,
}
}
}
144 changes: 116 additions & 28 deletions crates/utils/src/config/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,11 @@ pub struct ClientConfig {
}

impl ClientConfig {
/// Create a new client timeout
///
/// # Panics
///
/// Panics if `initial_retry_timeout` is larger than `max_retry_timeout`
#[must_use]
/// Create a builder for `ClientConfig`
#[inline]
pub fn new(
wait_synced_timeout: Duration,
propose_timeout: Duration,
initial_retry_timeout: Duration,
max_retry_timeout: Duration,
retry_count: usize,
fixed_backoff: bool,
keep_alive_interval: Duration,
) -> Self {
assert!(
initial_retry_timeout <= max_retry_timeout,
"`initial_retry_timeout` should less or equal to `max_retry_timeout`"
);
Self {
wait_synced_timeout,
propose_timeout,
initial_retry_timeout,
max_retry_timeout,
retry_count,
fixed_backoff,
keep_alive_interval,
}
#[must_use]
pub fn builder() -> Builder {
Builder::default()
}
}

Expand All @@ -103,3 +79,115 @@ impl Default for ClientConfig {
}
}
}

/// Builder for `ClientConfig`
#[derive(Default, Debug, Clone, Copy)]
pub struct Builder {
/// Curp client wait sync timeout
wait_synced_timeout: Option<Duration>,
/// Curp client propose request timeout
propose_timeout: Option<Duration>,
/// Curp client initial retry interval
initial_retry_timeout: Option<Duration>,
/// Curp client max retry interval
max_retry_timeout: Option<Duration>,
/// Curp client retry interval
retry_count: Option<usize>,
/// Whether to use exponential backoff in retries
fixed_backoff: Option<bool>,
/// Curp client keep client id alive interval
keep_alive_interval: Option<Duration>,
}

impl Builder {
/// Set the wait sync timeout
#[inline]
#[must_use]
pub fn wait_synced_timeout(mut self, timeout: Duration) -> Self {
self.wait_synced_timeout = Some(timeout);
self
}

/// Set the propose timeout
#[inline]
#[must_use]
pub fn propose_timeout(mut self, timeout: Duration) -> Self {
self.propose_timeout = Some(timeout);
self
}

/// Set the initial retry timeout
#[inline]
#[must_use]
pub fn initial_retry_timeout(mut self, timeout: Duration) -> Self {
self.initial_retry_timeout = Some(timeout);
self
}

/// Set the max retry timeout
#[inline]
#[must_use]
pub fn max_retry_timeout(mut self, timeout: Duration) -> Self {
self.max_retry_timeout = Some(timeout);
self
}

/// Set the retry count
#[inline]
#[must_use]
pub fn retry_count(mut self, count: usize) -> Self {
self.retry_count = Some(count);
self
}

/// Set whether to use fixed backoff
#[inline]
#[must_use]
pub fn fixed_backoff(mut self, use_backoff: bool) -> Self {
self.fixed_backoff = Some(use_backoff);
self
}

/// Set the keep alive interval
#[inline]
#[must_use]
pub fn keep_alive_interval(mut self, interval: Duration) -> Self {
self.keep_alive_interval = Some(interval);
self
}

/// # Panics
///
/// Panics if `initial_retry_timeout` is larger than `max_retry_timeout`
/// Build the `ClientConfig` and validate it
#[inline]
#[must_use]
pub fn build(self) -> ClientConfig {
let initial_retry_timeout = self
.initial_retry_timeout
.unwrap_or_else(default_initial_retry_timeout);
let max_retry_timeout = self
.max_retry_timeout
.unwrap_or_else(default_max_retry_timeout);

// Assert that `initial_retry_timeout <= max_retry_timeout`
assert!(
initial_retry_timeout <= max_retry_timeout,
"`initial_retry_timeout` should be less than or equal to `max_retry_timeout`"
);

ClientConfig {
wait_synced_timeout: self
.wait_synced_timeout
.unwrap_or_else(default_client_wait_synced_timeout),
propose_timeout: self.propose_timeout.unwrap_or_else(default_propose_timeout),
initial_retry_timeout,
max_retry_timeout,
retry_count: self.retry_count.unwrap_or_else(default_retry_count),
fixed_backoff: self.fixed_backoff.unwrap_or_else(default_fixed_backoff),
keep_alive_interval: self
.keep_alive_interval
.unwrap_or_else(default_client_id_keep_alive_interval),
}
}
}
4 changes: 3 additions & 1 deletion crates/utils/src/config/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use derive_builder::Builder;
use getset::Getters;
use serde::Deserialize;
use std::collections::HashMap;
Expand All @@ -6,7 +7,8 @@ use super::prelude::{ClientConfig, CurpConfig, XlineServerTimeout};

/// Cluster configuration object, including cluster relevant configuration fields
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Getters)]
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Getters, Builder)]
#[builder(pattern = "owned", default)]
pub struct ClusterConfig {
/// Get xline server name
#[getset(get = "pub")]
Expand Down
60 changes: 50 additions & 10 deletions crates/utils/src/config/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,58 @@ impl Default for CompactConfig {
}

impl CompactConfig {
/// Create a new compact config
/// Create a builder for `CompactConfig`
#[inline]
#[must_use]
pub fn builder() -> Builder {
Builder::default()
}
}

/// Builder for `CompactConfig`
#[derive(Default, Debug, Clone, Copy)]
pub struct Builder {
/// The max number of historical versions processed in a single compact operation
compact_batch_size: Option<usize>,
/// The interval between two compaction batches
compact_sleep_interval: Option<Duration>,
/// The auto compactor config
auto_compact_config: Option<AutoCompactConfig>,
}

impl Builder {
/// Set the compact batch size
#[inline]
pub fn new(
compact_batch_size: usize,
compact_sleep_interval: Duration,
auto_compact_config: Option<AutoCompactConfig>,
) -> Self {
Self {
compact_batch_size,
compact_sleep_interval,
auto_compact_config,
#[must_use]
pub fn compact_batch_size(mut self, size: usize) -> Self {
self.compact_batch_size = Some(size);
self
}

/// Set the compact sleep interval
#[inline]
#[must_use]
pub fn compact_sleep_interval(mut self, interval: Duration) -> Self {
self.compact_sleep_interval = Some(interval);
self
}

/// Set the auto compactor config
#[inline]
#[must_use]
pub fn auto_compact_config(mut self, config: Option<AutoCompactConfig>) -> Self {
self.auto_compact_config = config;
self
}

/// Build the `CompactConfig`
#[inline]
#[must_use]
pub fn build(self) -> CompactConfig {
CompactConfig {
compact_batch_size: self.compact_batch_size.unwrap_or_default(),
compact_sleep_interval: self.compact_sleep_interval.unwrap_or_default(),
auto_compact_config: self.auto_compact_config,
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/utils/src/config/curp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ pub struct CurpConfig {
pub log_entries_cap: usize,
}

impl CurpConfig {
/// Create a new `CurpConfig` with a builder
#[must_use]
#[inline]
pub fn builder() -> CurpConfigBuilder {
CurpConfigBuilder::default()
}
}

/// default heartbeat interval
#[must_use]
#[inline]
Expand Down
Loading

0 comments on commit 175780c

Please sign in to comment.