turmoil/
config.rs
1use rand_distr::Exp;
2use std::{
3 ops::RangeInclusive,
4 time::{Duration, SystemTime},
5};
6
7#[derive(Clone)]
22pub(crate) struct Config {
23 pub(crate) duration: Duration,
25
26 pub(crate) tick: Duration,
28
29 pub(crate) epoch: SystemTime,
31
32 pub(crate) ephemeral_ports: RangeInclusive<u16>,
33
34 pub(crate) tcp_capacity: usize,
36
37 pub(crate) udp_capacity: usize,
39
40 pub(crate) enable_tokio_io: bool,
42
43 pub(crate) random_node_order: bool,
46}
47
48#[derive(Clone, Default)]
50pub(crate) struct Link {
51 pub(crate) latency: Option<Latency>,
53
54 pub(crate) message_loss: Option<MessageLoss>,
56}
57
58#[derive(Clone)]
65pub(crate) struct Latency {
66 pub(crate) min_message_latency: Duration,
68
69 pub(crate) max_message_latency: Duration,
71
72 pub(crate) latency_distribution: Exp<f64>,
74}
75
76#[derive(Clone)]
80pub(crate) struct MessageLoss {
81 pub(crate) fail_rate: f64,
83
84 pub(crate) repair_rate: f64,
86}
87
88impl Default for Config {
89 fn default() -> Config {
90 Config {
91 duration: Duration::from_secs(10),
92 tick: Duration::from_millis(1),
93 epoch: SystemTime::now(),
94 ephemeral_ports: 49152..=65535,
95 tcp_capacity: 64,
96 udp_capacity: 64,
97 enable_tokio_io: false,
98 random_node_order: false,
99 }
100 }
101}
102
103impl Link {
104 pub(crate) fn latency(&self) -> &Latency {
105 self.latency.as_ref().expect("`Latency` missing")
106 }
107
108 pub(crate) fn latency_mut(&mut self) -> &mut Latency {
109 self.latency.as_mut().expect("`Latency` missing")
110 }
111
112 pub(crate) fn message_loss(&self) -> &MessageLoss {
113 self.message_loss.as_ref().expect("`MessageLoss` missing")
114 }
115
116 pub(crate) fn message_loss_mut(&mut self) -> &mut MessageLoss {
117 self.message_loss.as_mut().expect("`MessageLoss` missing")
118 }
119}
120
121impl Default for Latency {
122 fn default() -> Latency {
123 Latency {
124 min_message_latency: Duration::from_millis(0),
125 max_message_latency: Duration::from_millis(100),
126 latency_distribution: Exp::new(5.0).unwrap(),
127 }
128 }
129}
130
131impl Default for MessageLoss {
132 fn default() -> MessageLoss {
133 MessageLoss {
134 fail_rate: 0.0,
135 repair_rate: 1.0,
136 }
137 }
138}