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