unc_chain_configs/
updateable_config.rs1use serde::{Deserialize, Serialize, Serializer};
2use std::sync::{Arc, Mutex};
3use std::{fmt::Debug, time::Duration};
4use unc_primitives::types::BlockHeight;
5
6use crate::ReshardingConfig;
7
8#[derive(Clone, Debug)]
13pub struct MutableConfigValue<T> {
14 value: Arc<Mutex<T>>,
15 field_name: String,
18 #[cfg(feature = "metrics")]
19 last_update: chrono::DateTime<chrono::Utc>,
22}
23
24impl<T: Serialize> Serialize for MutableConfigValue<T> {
25 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
28 where
29 S: Serializer,
30 {
31 let to_string_result = serde_json::to_string(&self.value);
32 let value_str = to_string_result.unwrap_or("unable to serialize the value".to_string());
33 serializer.serialize_str(&value_str)
34 }
35}
36
37impl<T: Copy + PartialEq + Debug> MutableConfigValue<T> {
38 pub fn new(val: T, field_name: &str) -> Self {
41 let res = Self {
42 value: Arc::new(Mutex::new(val)),
43 field_name: field_name.to_string(),
44 #[cfg(feature = "metrics")]
45 last_update: unc_primitives::static_clock::StaticClock::utc(),
46 };
47 res.set_metric_value(val, 1);
48 res
49 }
50
51 pub fn get(&self) -> T {
52 *self.value.lock().unwrap()
53 }
54
55 pub fn update(&self, val: T) {
56 let mut lock = self.value.lock().unwrap();
57 if *lock != val {
58 tracing::info!(target: "config", "Updated config field '{}' from {:?} to {:?}", self.field_name, *lock, val);
59 self.set_metric_value(*lock, 0);
60 *lock = val;
61 self.set_metric_value(val, 1);
62 } else {
63 tracing::info!(target: "config", "Mutable config field '{}' remains the same: {:?}", self.field_name, val);
64 }
65 }
66
67 #[cfg(feature = "metrics")]
68 fn set_metric_value(&self, value: T, metric_value: i64) {
69 crate::metrics::CONFIG_MUTABLE_FIELD
76 .with_label_values(&[
77 &self.field_name,
78 &self.last_update.timestamp().to_string(),
79 &format!("{:?}", value),
80 ])
81 .set(metric_value);
82 }
83
84 #[cfg(not(feature = "metrics"))]
85 fn set_metric_value(&self, _value: T, _metric_value: i64) {}
86}
87
88#[derive(Default, Clone, Serialize, Deserialize)]
89pub struct UpdateableClientConfig {
91 pub expected_shutdown: Option<BlockHeight>,
93
94 pub resharding_config: ReshardingConfig,
96
97 pub produce_chunk_add_transactions_time_limit: Option<Duration>,
99}