Skip to content

Commit

Permalink
refactor: split config into multiple modules
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh1s <[email protected]>
  • Loading branch information
Harsh1s authored and Phoenix500526 committed May 16, 2024
1 parent b835b24 commit ef75856
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 1,617 deletions.
1,526 changes: 0 additions & 1,526 deletions crates/utils/src/config.rs

This file was deleted.

21 changes: 20 additions & 1 deletion crates/utils/src/config/client_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ pub struct ClientConfig {
#[getset(get = "pub")]
#[serde(default = "default_fixed_backoff")]
fixed_backoff: bool,

/// Curp client keep client id alive interval
#[getset(get = "pub")]
#[serde(
with = "duration_format",
default = "default_client_id_keep_alive_interval"
)]
keep_alive_interval: Duration,
}

impl ClientConfig {
Expand All @@ -56,6 +64,7 @@ impl ClientConfig {
max_retry_timeout: Duration,
retry_count: usize,
fixed_backoff: bool,
keep_alive_interval: Duration,
) -> Self {
assert!(
initial_retry_timeout <= max_retry_timeout,
Expand All @@ -68,6 +77,7 @@ impl ClientConfig {
max_retry_timeout,
retry_count,
fixed_backoff,
keep_alive_interval,
}
}
}
Expand All @@ -82,6 +92,7 @@ impl Default for ClientConfig {
max_retry_timeout: default_max_retry_timeout(),
retry_count: default_retry_count(),
fixed_backoff: default_fixed_backoff(),
keep_alive_interval: default_client_id_keep_alive_interval(),
}
}
}
Expand All @@ -100,6 +111,14 @@ pub const fn default_propose_timeout() -> Duration {
Duration::from_secs(1)
}


/// default client id keep alive interval
#[must_use]
#[inline]
pub const fn default_client_id_keep_alive_interval() -> Duration {
Duration::from_secs(1)
}

/// default initial retry timeout
#[must_use]
#[inline]
Expand Down Expand Up @@ -141,7 +160,7 @@ pub const fn default_fixed_backoff() -> bool {
pub mod duration_format {
use std::time::Duration;

use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use crate::parse_duration;

Expand Down
2 changes: 1 addition & 1 deletion crates/utils/src/config/cluster_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub enum InitialClusterState {

/// `InitialClusterState` deserialization formatter
pub mod state_format {
use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use super::InitialClusterState;
use crate::parse_state;
Expand Down
2 changes: 1 addition & 1 deletion crates/utils/src/config/compact_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub const fn default_compact_sleep_interval() -> Duration {
pub mod duration_format {
use std::time::Duration;

use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use crate::parse_duration;

Expand Down
6 changes: 3 additions & 3 deletions crates/utils/src/config/curp_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub const fn default_retry_count() -> usize {
#[must_use]
#[inline]
pub const fn default_rpc_timeout() -> Duration {
Duration::from_millis(50)
Duration::from_millis(150)
}

/// default batch timeout
Expand Down Expand Up @@ -190,7 +190,7 @@ pub const fn default_log_entries_cap() -> usize {
pub mod duration_format {
use std::time::Duration;

use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use crate::parse_duration;

Expand All @@ -207,7 +207,7 @@ pub mod duration_format {

/// batch size deserialization formatter
pub mod bytes_format {
use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use crate::parse_batch_bytes;

Expand Down
4 changes: 2 additions & 2 deletions crates/utils/src/config/log_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Default for LogConfig {

/// `LevelConfig` deserialization formatter
pub mod level_format {
use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use super::LevelConfig;
use crate::parse_log_level;
Expand Down Expand Up @@ -102,7 +102,7 @@ impl std::fmt::Display for RotationConfig {

/// `RotationConfig` deserialization formatter
pub mod rotation_format {
use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use super::RotationConfig;
use crate::parse_rotation;
Expand Down
125 changes: 62 additions & 63 deletions crates/utils/src/config/metrics_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,109 +5,109 @@ use serde::Deserialize;
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Getters)]
pub struct MetricsConfig {
/// Enable or not
#[getset(get = "pub")]
#[serde(default = "default_metrics_enable")]
pub enable: bool,
/// The http port to expose
#[getset(get = "pub")]
#[serde(default = "default_metrics_port")]
pub port: u16,
/// The http path to expose
#[getset(get = "pub")]
#[serde(default = "default_metrics_path")]
pub path: String,
/// Enable push or not
#[getset(get = "pub")]
#[serde(default = "default_metrics_push")]
pub push: bool,
/// Push endpoint
#[getset(get = "pub")]
#[serde(default = "default_metrics_push_endpoint")]
pub push_endpoint: String,
/// Push protocol
#[getset(get = "pub")]
#[serde(with = "protocol_format", default = "default_metrics_push_protocol")]
pub push_protocol: MetricsPushProtocol,
/// Enable or not
#[getset(get = "pub")]
#[serde(default = "default_metrics_enable")]
enable: bool,
/// The http port to expose
#[getset(get = "pub")]
#[serde(default = "default_metrics_port")]
port: u16,
/// The http path to expose
#[getset(get = "pub")]
#[serde(default = "default_metrics_path")]
path: String,
/// Enable push or not
#[getset(get = "pub")]
#[serde(default = "default_metrics_push")]
push: bool,
/// Push endpoint
#[getset(get = "pub")]
#[serde(default = "default_metrics_push_endpoint")]
push_endpoint: String,
/// Push protocol
#[getset(get = "pub")]
#[serde(with = "protocol_format", default = "default_metrics_push_protocol")]
push_protocol: MetricsPushProtocol,
}

impl MetricsConfig {
/// Create a new `MetricsConfig`
#[must_use]
#[inline]
pub fn new(
enable: bool,
port: u16,
path: String,
push: bool,
push_endpoint: String,
push_protocol: MetricsPushProtocol,
) -> Self {
Self {
enable,
port,
path,
push,
push_endpoint,
push_protocol,
}
}
/// Create a new `MetricsConfig`
#[must_use]
#[inline]
pub fn new(
enable: bool,
port: u16,
path: String,
push: bool,
push_endpoint: String,
push_protocol: MetricsPushProtocol,
) -> Self {
Self {
enable,
port,
path,
push,
push_endpoint,
push_protocol,
}
}
}

impl Default for MetricsConfig {
#[inline]
fn default() -> Self {
Self {
enable: default_metrics_enable(),
port: default_metrics_port(),
path: default_metrics_path(),
push: default_metrics_push(),
push_endpoint: default_metrics_push_endpoint(),
push_protocol: default_metrics_push_protocol(),
}
}
#[inline]
fn default() -> Self {
Self {
enable: default_metrics_enable(),
port: default_metrics_port(),
path: default_metrics_path(),
push: default_metrics_push(),
push_endpoint: default_metrics_push_endpoint(),
push_protocol: default_metrics_push_protocol(),
}
}
}

/// Default metrics enable
#[must_use]
#[inline]
pub const fn default_metrics_enable() -> bool {
true
true
}

/// Default metrics port
#[must_use]
#[inline]
pub const fn default_metrics_port() -> u16 {
9100
9100
}

/// Default metrics path
#[must_use]
#[inline]
pub fn default_metrics_path() -> String {
"/metrics".to_owned()
"/metrics".to_owned()
}

/// Default metrics push option
#[must_use]
#[inline]
pub fn default_metrics_push() -> bool {
false
false
}

/// Default metrics push protocol
#[must_use]
#[inline]
pub fn default_metrics_push_protocol() -> MetricsPushProtocol {
MetricsPushProtocol::GRPC
MetricsPushProtocol::GRPC
}

/// Default metrics push endpoint
#[must_use]
#[inline]
pub fn default_metrics_push_endpoint() -> String {
"http://127.0.0.1:4318".to_owned()
"http://127.0.0.1:4318".to_owned()
}

/// Xline metrics push protocol
Expand All @@ -134,8 +134,7 @@ impl std::fmt::Display for MetricsPushProtocol {

/// Metrics push protocol format
pub mod protocol_format {
use serde::{self, Deserialize, Deserializer};

use serde::{Deserialize, Deserializer};
use super::MetricsPushProtocol;
use crate::parse_metrics_push_protocol;

Expand Down
9 changes: 5 additions & 4 deletions crates/utils/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ mod tests {
Duration::from_secs(50),
default_retry_count(),
default_fixed_backoff(),
default_client_id_keep_alive_interval(),
);

let server_timeout = ServerTimeout::new(
Expand Down Expand Up @@ -305,7 +306,7 @@ mod tests {
#[test]
fn test_xline_server_default_config_should_be_loaded() {
let config: XlineServerConfig = toml::from_str(
r#"[cluster]
"[cluster]
name = 'node1'
is_leader = true
peer_listen_urls = ['127.0.0.1:2380']
Expand Down Expand Up @@ -337,7 +338,7 @@ mod tests {
[auth]
[tls]
"#,
",
)
.unwrap();

Expand Down Expand Up @@ -394,7 +395,7 @@ mod tests {
#[test]
fn test_auto_revision_compactor_config_should_be_loaded() {
let config: XlineServerConfig = toml::from_str(
r#"[cluster]
"[cluster]
name = 'node1'
is_leader = true
peer_listen_urls = ['127.0.0.1:2380']
Expand Down Expand Up @@ -430,7 +431,7 @@ mod tests {
[auth]
[tls]
"#,
",
)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion crates/utils/src/config/server_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub const fn default_watch_progress_notify_interval() -> Duration {
pub mod duration_format {
use std::time::Duration;

use serde::{self, Deserialize, Deserializer};
use serde::{Deserialize, Deserializer};

use crate::parse_duration;

Expand Down
Loading

0 comments on commit ef75856

Please sign in to comment.