mecha10_runtime/
config.rs

1//! Runtime configuration types
2
3use serde::{Deserialize, Serialize};
4use std::time::Duration;
5
6/// Runtime configuration
7#[derive(Clone, Debug)]
8pub struct RuntimeConfig {
9    /// Log level (trace, debug, info, warn, error)
10    pub log_level: String,
11
12    /// Log output format
13    pub log_format: LogFormat,
14
15    /// Restart policy for failed nodes
16    pub restart_policy: RestartPolicy,
17
18    /// Health check interval
19    pub health_check_interval: Duration,
20
21    /// Graceful shutdown timeout
22    pub shutdown_timeout: Duration,
23}
24
25impl Default for RuntimeConfig {
26    fn default() -> Self {
27        Self {
28            log_level: "info".to_string(),
29            log_format: LogFormat::Pretty,
30            restart_policy: RestartPolicy::Never,
31            health_check_interval: Duration::from_secs(30),
32            shutdown_timeout: Duration::from_secs(10),
33        }
34    }
35}
36
37/// Log output format
38#[derive(Clone, Debug, Serialize, Deserialize)]
39pub enum LogFormat {
40    /// Human-readable pretty format
41    Pretty,
42    /// Structured JSON format
43    Json,
44    /// Compact minimal format
45    Compact,
46}
47
48/// Restart policy for nodes
49#[derive(Clone, Debug)]
50pub enum RestartPolicy {
51    /// Never restart failed nodes
52    Never,
53
54    /// Restart on failure with exponential backoff
55    OnFailure {
56        /// Maximum number of restart attempts
57        max_retries: usize,
58        /// Initial backoff duration (doubles each retry)
59        backoff: Duration,
60    },
61
62    /// Always restart nodes with constant backoff
63    Always {
64        /// Backoff duration between restarts
65        backoff: Duration,
66    },
67}
68
69impl RestartPolicy {
70    /// Check if should restart based on attempt count
71    pub fn should_restart(&self, attempt: usize) -> bool {
72        match self {
73            RestartPolicy::Never => false,
74            RestartPolicy::OnFailure { max_retries, .. } => attempt < *max_retries,
75            RestartPolicy::Always { .. } => true,
76        }
77    }
78
79    /// Calculate backoff duration for given attempt
80    pub fn backoff_duration(&self, attempt: usize) -> Duration {
81        match self {
82            RestartPolicy::Never => Duration::from_secs(0),
83            RestartPolicy::OnFailure { backoff, .. } => {
84                // Exponential backoff: backoff * 2^attempt
85                *backoff * 2u32.pow(attempt as u32)
86            }
87            RestartPolicy::Always { backoff } => *backoff,
88        }
89    }
90}