Skip to main content

hopr_transport_mixer/
config.rs

1use std::time::Duration;
2
3pub const HOPR_MIXER_MINIMUM_DEFAULT_DELAY_IN_MS: u64 = 0;
4pub const HOPR_MIXER_DEFAULT_DELAY_RANGE_IN_MS: u64 = 200;
5pub const HOPR_MIXER_DELAY_METRIC_WINDOW: u64 = 100;
6pub const HOPR_MIXER_CAPACITY: usize = 20_000;
7
8/// Mixer configuration.
9#[derive(Debug, Clone, Copy, Eq, PartialEq, smart_default::SmartDefault)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct MixerConfig {
12    /// The minimum delay introduced during mixing.
13    #[default(Duration::from_millis(HOPR_MIXER_MINIMUM_DEFAULT_DELAY_IN_MS))]
14    #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
15    pub min_delay: Duration,
16    /// The range from the minimum delay to the maximum possible delay.
17    #[default(Duration::from_millis(HOPR_MIXER_DEFAULT_DELAY_RANGE_IN_MS))]
18    #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
19    pub delay_range: Duration,
20    /// The capacity of the preallocated mixer buffer.
21    ///
22    /// It is possible to insert more items past the capacity, triggering
23    /// a possible buffer reallocation.
24    #[default(HOPR_MIXER_CAPACITY)]
25    pub capacity: usize,
26    #[default(HOPR_MIXER_DELAY_METRIC_WINDOW)]
27    pub metric_delay_window: u64,
28}
29
30impl MixerConfig {
31    /// Get a random delay duration from the specified minimum and maximum delay available
32    /// inside the configuration.
33    pub fn random_delay(&self) -> Duration {
34        let max_delay = self.min_delay.saturating_add(self.delay_range);
35
36        let random_delay = if max_delay.as_millis() == 0 {
37            max_delay.as_millis() as u64
38        } else {
39            hopr_types::crypto_random::random_integer(
40                self.min_delay.as_millis() as u64,
41                Some(max_delay.as_millis() as u64),
42            )
43        };
44
45        Duration::from_millis(random_delay)
46    }
47}