mecha10_runtime/
config.rs1use serde::{Deserialize, Serialize};
4use std::time::Duration;
5
6#[derive(Clone, Debug)]
8pub struct RuntimeConfig {
9 pub log_level: String,
11
12 pub log_format: LogFormat,
14
15 pub restart_policy: RestartPolicy,
17
18 pub health_check_interval: Duration,
20
21 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#[derive(Clone, Debug, Serialize, Deserialize)]
39pub enum LogFormat {
40 Pretty,
42 Json,
44 Compact,
46}
47
48#[derive(Clone, Debug)]
50pub enum RestartPolicy {
51 Never,
53
54 OnFailure {
56 max_retries: usize,
58 backoff: Duration,
60 },
61
62 Always {
64 backoff: Duration,
66 },
67}
68
69impl RestartPolicy {
70 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 pub fn backoff_duration(&self, attempt: usize) -> Duration {
81 match self {
82 RestartPolicy::Never => Duration::from_secs(0),
83 RestartPolicy::OnFailure { backoff, .. } => {
84 *backoff * 2u32.pow(attempt as u32)
86 }
87 RestartPolicy::Always { backoff } => *backoff,
88 }
89 }
90}