1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2015-2019 Benjamin Fry <benjaminfry@me.com>
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use std::cmp::Ordering;

use std::sync::atomic::{self, AtomicUsize};

pub(crate) struct NameServerStats {
    successes: AtomicUsize,
    failures: AtomicUsize,
    // TODO: incorporate latency
}

impl Default for NameServerStats {
    fn default() -> Self {
        Self::new(0, 0)
    }
}

impl NameServerStats {
    pub(crate) fn new(successes: usize, failures: usize) -> Self {
        Self {
            successes: AtomicUsize::new(successes),
            failures: AtomicUsize::new(failures),
        }
    }

    pub(crate) fn next_success(&self) {
        self.successes.fetch_add(1, atomic::Ordering::Release);
    }

    pub(crate) fn next_failure(&self) {
        self.failures.fetch_add(1, atomic::Ordering::Release);
    }

    fn noload_eq(
        self_successes: usize,
        other_successes: usize,
        self_failures: usize,
        other_failures: usize,
    ) -> bool {
        self_successes == other_successes && self_failures == other_failures
    }
}

impl PartialEq for NameServerStats {
    fn eq(&self, other: &Self) -> bool {
        let self_successes = self.successes.load(atomic::Ordering::Acquire);
        let other_successes = other.successes.load(atomic::Ordering::Acquire);

        let self_failures = self.failures.load(atomic::Ordering::Acquire);
        let other_failures = other.failures.load(atomic::Ordering::Acquire);

        // if they are literally equal, just return
        Self::noload_eq(
            self_successes,
            other_successes,
            self_failures,
            other_failures,
        )
    }
}

impl Eq for NameServerStats {}

impl Ord for NameServerStats {
    /// Custom implementation of Ord for NameServer which incorporates the performance of the connection into it's ranking
    fn cmp(&self, other: &Self) -> Ordering {
        let self_successes = self.successes.load(atomic::Ordering::Acquire);
        let other_successes = other.successes.load(atomic::Ordering::Acquire);

        let self_failures = self.failures.load(atomic::Ordering::Acquire);
        let other_failures = other.failures.load(atomic::Ordering::Acquire);

        // if they are literally equal, just return
        if Self::noload_eq(
            self_successes,
            other_successes,
            self_failures,
            other_failures,
        ) {
            return Ordering::Equal;
        }

        // TODO: track latency and use lowest latency connection...

        // invert failure comparison, i.e. the one with the least failures, wins
        if self_failures <= other_failures {
            return Ordering::Greater;
        }

        // at this point we'll go with the lesser of successes to make sure there is balance
        self_successes.cmp(&other_successes)
    }
}

impl PartialOrd for NameServerStats {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn is_send_sync<S: Sync + Send>() -> bool {
        true
    }

    #[test]
    fn stats_are_sync() {
        assert!(is_send_sync::<NameServerStats>());
    }

    #[test]
    fn test_state_cmp() {
        let nil = NameServerStats::new(0, 0);
        let successes = NameServerStats::new(1, 0);
        let failures = NameServerStats::new(0, 1);

        assert_eq!(nil.cmp(&nil), Ordering::Equal);
        assert_eq!(nil.cmp(&successes), Ordering::Greater);
        assert_eq!(successes.cmp(&failures), Ordering::Greater);
    }
}