Skip to content

Commit b137a97

Browse files
committed
Duration serialization
1 parent aa57d56 commit b137a97

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

primitives/src/config.rs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use crate::{
55
Address, BigNum, ChainOf, ValidatorId,
66
};
77
use once_cell::sync::Lazy;
8-
use serde::{Deserialize, Deserializer, Serialize};
8+
use serde::{Deserialize, Serialize};
99
use std::{collections::HashMap, num::NonZeroU8, time::Duration};
1010
use thiserror::Error;
11+
use duration::{milliseconds_to_std_duration, std_duration_to_milliseconds};
1112

1213
pub use toml::de::Error as TomlError;
1314

@@ -77,7 +78,7 @@ pub struct Config {
7778
/// finish before running a new tick in the Validator Worker.
7879
///
7980
/// In milliseconds
80-
#[serde(deserialize_with = "milliseconds_to_std_duration")]
81+
#[serde(deserialize_with = "milliseconds_to_std_duration", serialize_with = "std_duration_to_milliseconds")]
8182
pub wait_time: Duration,
8283
/// The maximum allowed limit of [`ValidatorMessage`](crate::sentry::ValidatorMessage)s per page
8384
/// returned by Sentry's GET `/v5/channel/0xXXX.../validator-messages` route.
@@ -108,12 +109,12 @@ pub struct Config {
108109
/// - GET `/v5/analytics/for-admin`
109110
///
110111
/// In milliseconds
111-
#[serde(deserialize_with = "milliseconds_to_std_duration")]
112+
#[serde(deserialize_with = "milliseconds_to_std_duration", serialize_with = "std_duration_to_milliseconds")]
112113
pub analytics_maxtime: Duration,
113114
/// The amount of time that should have passed before sending a new heartbeat.
114115
///
115116
/// In milliseconds
116-
#[serde(deserialize_with = "milliseconds_to_std_duration")]
117+
#[serde(deserialize_with = "milliseconds_to_std_duration", serialize_with = "std_duration_to_milliseconds")]
117118
pub heartbeat_time: Duration,
118119
/// The pro miles below which the [`ApproveState`](crate::validator::ApproveState)
119120
/// becomes **unhealthy** in the [`Channel`](crate::Channel)'s Follower.
@@ -132,7 +133,7 @@ pub struct Config {
132133
/// to a validator.
133134
///
134135
/// In milliseconds
135-
#[serde(deserialize_with = "milliseconds_to_std_duration")]
136+
#[serde(deserialize_with = "milliseconds_to_std_duration", serialize_with = "std_duration_to_milliseconds")]
136137
pub propagation_timeout: Duration,
137138
/// The Client timeout for `SentryApi`.
138139
///
@@ -142,20 +143,20 @@ pub struct Config {
142143
/// [`Config.propagation_timeout`](Config::propagation_timeout).
143144
///
144145
/// In milliseconds
145-
#[serde(deserialize_with = "milliseconds_to_std_duration")]
146+
#[serde(deserialize_with = "milliseconds_to_std_duration", serialize_with = "std_duration_to_milliseconds")]
146147
pub fetch_timeout: Duration,
147148
/// The Client timeout for `SentryApi` when collecting all channels
148149
/// and Validators using the `/campaign/list` route.
149150
///
150151
/// In milliseconds
151-
#[serde(deserialize_with = "milliseconds_to_std_duration")]
152+
#[serde(deserialize_with = "milliseconds_to_std_duration", serialize_with = "std_duration_to_milliseconds")]
152153
pub all_campaigns_timeout: Duration,
153154
/// The timeout for a single tick of a [`Channel`](crate::Channel) in
154155
/// the Validator Worker.
155156
/// This timeout is applied to both the leader and follower ticks.
156157
///
157158
/// In milliseconds
158-
#[serde(deserialize_with = "milliseconds_to_std_duration")]
159+
#[serde(deserialize_with = "milliseconds_to_std_duration", serialize_with = "std_duration_to_milliseconds")]
159160
pub channel_tick_timeout: Duration,
160161
/// The default IP rate limit that will be imposed if
161162
/// [`Campaign.event_submission`](crate::Campaign::event_submission) is [`None`].
@@ -214,7 +215,7 @@ impl Config {
214215
#[derive(Serialize, Deserialize, Debug, Clone)]
215216
pub struct PlatformConfig {
216217
pub url: ApiUrl,
217-
#[serde(deserialize_with = "milliseconds_to_std_duration")]
218+
#[serde(deserialize_with = "milliseconds_to_std_duration", serialize_with = "std_duration_to_milliseconds")]
218219
pub keep_alive_interval: Duration,
219220
}
220221

@@ -252,21 +253,34 @@ pub struct Limits {
252253
pub units_for_slot: limits::UnitsForSlot,
253254
}
254255

255-
fn milliseconds_to_std_duration<'de, D>(deserializer: D) -> Result<Duration, D::Error>
256-
where
257-
D: Deserializer<'de>,
258-
{
259-
use serde::de::Error;
260-
use toml::Value;
261256

262-
let toml_value: Value = Value::deserialize(deserializer)?;
257+
pub mod duration {
258+
use std::time::Duration;
259+
use serde::{Serializer, Deserializer, Deserialize};
263260

264-
let milliseconds = match toml_value {
265-
Value::Integer(mills) => u64::try_from(mills).map_err(Error::custom),
266-
_ => Err(Error::custom("Only integers allowed for this value")),
267-
}?;
261+
pub fn milliseconds_to_std_duration<'de, D>(deserializer: D) -> Result<Duration, D::Error>
262+
where
263+
D: Deserializer<'de>,
264+
{
265+
use serde::de::Error;
266+
use toml::Value;
268267

269-
Ok(Duration::from_millis(milliseconds))
268+
let toml_value: Value = Value::deserialize(deserializer)?;
269+
270+
let milliseconds = match toml_value {
271+
Value::Integer(mills) => u64::try_from(mills).map_err(Error::custom),
272+
_ => Err(Error::custom("Only integers allowed for this value")),
273+
}?;
274+
275+
Ok(Duration::from_millis(milliseconds))
276+
}
277+
278+
pub fn std_duration_to_milliseconds<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
279+
where
280+
S: Serializer,
281+
{
282+
serializer.serialize_str(&duration.as_millis().to_string())
283+
}
270284
}
271285

272286
pub mod limits {

0 commit comments

Comments
 (0)