Line data Source code
1 : use rand::Rng;
2 : use rand::rngs::StdRng;
3 :
4 : /// Describes random delays and failures. Delay will be uniformly distributed in [min, max].
5 : /// Connection failure will occur with the probablity fail_prob.
6 : #[derive(Clone, Debug)]
7 : pub struct Delay {
8 : pub min: u64,
9 : pub max: u64,
10 : pub fail_prob: f64, // [0; 1]
11 : }
12 :
13 : impl Delay {
14 : /// Create a struct with no delay, no failures.
15 0 : pub fn empty() -> Delay {
16 0 : Delay {
17 0 : min: 0,
18 0 : max: 0,
19 0 : fail_prob: 0.0,
20 0 : }
21 0 : }
22 :
23 : /// Create a struct with a fixed delay.
24 0 : pub fn fixed(ms: u64) -> Delay {
25 0 : Delay {
26 0 : min: ms,
27 0 : max: ms,
28 0 : fail_prob: 0.0,
29 0 : }
30 0 : }
31 :
32 : /// Generate a random delay in range [min, max]. Return None if the
33 : /// message should be dropped.
34 103199 : pub fn delay(&self, rng: &mut StdRng) -> Option<u64> {
35 103199 : if rng.gen_bool(self.fail_prob) {
36 14468 : return None;
37 88731 : }
38 88731 : Some(rng.gen_range(self.min..=self.max))
39 103199 : }
40 : }
41 :
42 : /// Describes network settings. All network packets will be subjected to the same delays and failures.
43 : #[derive(Clone, Debug)]
44 : pub struct NetworkOptions {
45 : /// Connection will be automatically closed after this timeout if no data is received.
46 : pub keepalive_timeout: Option<u64>,
47 : /// New connections will be delayed by this amount of time.
48 : pub connect_delay: Delay,
49 : /// Each message will be delayed by this amount of time.
50 : pub send_delay: Delay,
51 : }
|