feat(protocol): RestartPolicy/RestartConfig/StopConfig with defaults
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum RestartPolicy {
|
||||
Always,
|
||||
OnFailure,
|
||||
Never,
|
||||
}
|
||||
|
||||
impl Default for RestartPolicy {
|
||||
fn default() -> Self {
|
||||
RestartPolicy::OnFailure
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct RestartConfig {
|
||||
#[serde(default)]
|
||||
pub policy: RestartPolicy,
|
||||
#[serde(default = "default_backoff_initial", with = "humantime_serde")]
|
||||
pub backoff_initial: Duration,
|
||||
#[serde(default = "default_backoff_max", with = "humantime_serde")]
|
||||
pub backoff_max: Duration,
|
||||
#[serde(default = "default_max_retries_per_minute")]
|
||||
pub max_retries_per_minute: u32,
|
||||
}
|
||||
|
||||
fn default_backoff_initial() -> Duration {
|
||||
Duration::from_secs(1)
|
||||
}
|
||||
|
||||
fn default_backoff_max() -> Duration {
|
||||
Duration::from_secs(30)
|
||||
}
|
||||
|
||||
fn default_max_retries_per_minute() -> u32 {
|
||||
5
|
||||
}
|
||||
|
||||
impl Default for RestartConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
policy: RestartPolicy::default(),
|
||||
backoff_initial: default_backoff_initial(),
|
||||
backoff_max: default_backoff_max(),
|
||||
max_retries_per_minute: default_max_retries_per_minute(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct StopConfig {
|
||||
#[serde(default = "default_grace", with = "humantime_serde")]
|
||||
pub grace: Duration,
|
||||
}
|
||||
|
||||
fn default_grace() -> Duration {
|
||||
Duration::from_secs(10)
|
||||
}
|
||||
|
||||
impl Default for StopConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
grace: default_grace(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn restart_config_defaults() {
|
||||
let c = RestartConfig::default();
|
||||
assert_eq!(c.policy, RestartPolicy::OnFailure);
|
||||
assert_eq!(c.backoff_initial, Duration::from_secs(1));
|
||||
assert_eq!(c.backoff_max, Duration::from_secs(30));
|
||||
assert_eq!(c.max_retries_per_minute, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stop_config_defaults() {
|
||||
assert_eq!(StopConfig::default().grace, Duration::from_secs(10));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
//! Wire types and config schema shared between the xy daemon and CLI.
|
||||
|
||||
pub mod config;
|
||||
pub mod state;
|
||||
|
||||
pub use config::{RestartConfig, RestartPolicy, StopConfig};
|
||||
pub use state::ServerState;
|
||||
|
||||
Reference in New Issue
Block a user