rustfs_targets/runtime/tls/
state.rs1use super::fingerprint::{TargetTlsFingerprint, TargetTlsGeneration};
18use ::arc_swap::ArcSwap;
19use serde::Serialize;
20use std::sync::Arc;
21use std::sync::atomic::{AtomicU64, Ordering};
22
23#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct TargetTlsInputSet {
26 pub ca_path: String,
27 pub client_cert_path: String,
28 pub client_key_path: String,
29 pub target_label: String,
31}
32
33impl TargetTlsInputSet {
34 pub fn is_empty(&self) -> bool {
36 self.ca_path.is_empty() && self.client_cert_path.is_empty() && self.client_key_path.is_empty()
37 }
38}
39
40pub struct TargetTlsPublishedState<M> {
42 pub generation: TargetTlsGeneration,
43 pub fingerprint: TargetTlsFingerprint,
44 pub material: Arc<M>,
45 pub loaded_at_unix_ms: u64,
46}
47
48pub struct TargetTlsRuntimeState<M> {
51 pub current: ArcSwap<TargetTlsPublishedState<M>>,
53 pub last_good: ArcSwap<TargetTlsPublishedState<M>>,
55 pub last_attempt_unix_ms: AtomicU64,
57 pub last_success_unix_ms: AtomicU64,
59 pub last_error: parking_lot::RwLock<Option<String>>,
61 pub inputs: TargetTlsInputSet,
63 pub reload_lock: tokio::sync::Mutex<()>,
67}
68
69impl<M> TargetTlsRuntimeState<M> {
70 pub fn new(initial: Arc<TargetTlsPublishedState<M>>, inputs: TargetTlsInputSet) -> Self {
72 Self {
73 current: ArcSwap::from(initial.clone()),
74 last_good: ArcSwap::from(initial),
75 last_attempt_unix_ms: AtomicU64::new(0),
76 last_success_unix_ms: AtomicU64::new(0),
77 last_error: parking_lot::RwLock::new(None),
78 inputs,
79 reload_lock: tokio::sync::Mutex::new(()),
80 }
81 }
82
83 pub fn current_generation(&self) -> TargetTlsGeneration {
85 self.current.load().generation
86 }
87
88 pub fn bump_generation(&self) -> TargetTlsGeneration {
90 let current = self.current.load();
93 TargetTlsGeneration(current.generation.0.saturating_add(1))
94 }
95
96 pub fn mark_attempt(&self, unix_ms: u64) {
98 self.last_attempt_unix_ms.store(unix_ms, Ordering::Release);
99 }
100
101 pub fn mark_success(&self, unix_ms: u64) {
103 self.last_success_unix_ms.store(unix_ms, Ordering::Release);
104 }
105
106 pub fn last_attempt_unix_ms(&self) -> u64 {
108 self.last_attempt_unix_ms.load(Ordering::Acquire)
109 }
110
111 pub fn last_success_unix_ms(&self) -> u64 {
113 self.last_success_unix_ms.load(Ordering::Acquire)
114 }
115}
116
117#[derive(Debug, Clone, Serialize)]
119pub struct TargetTlsStatusSnapshot {
120 pub target_label: String,
121 pub generation: u64,
122 pub reload_enabled: bool,
123 pub detect_mode: &'static str,
124 pub apply_mode: &'static str,
125 pub last_attempt_time: Option<u64>,
126 pub last_success_time: Option<u64>,
127 pub last_error: Option<String>,
128 pub ca_path: String,
130 pub client_cert_path: String,
131 pub client_key_path: String,
132}