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
131
use super::error::Error;
use super::failure_policy::FailurePolicy;
use super::failure_predicate::{self, FailurePredicate};
use super::instrument::Instrument;
use super::state_machine::StateMachine;

/// A circuit breaker's public interface.
pub trait CircuitBreaker {
    /// Requests permission to call.
    ///
    /// It returns `true` if a call is allowed, or `false` if prohibited.
    fn is_call_permitted(&self) -> bool;

    /// Executes a given function within circuit breaker.
    ///
    /// Depending on function result value, the call will be recorded as success or failure.
    #[inline]
    fn call<F, E, R>(&self, f: F) -> Result<R, Error<E>>
    where
        F: FnOnce() -> Result<R, E>,
    {
        self.call_with(failure_predicate::Any, f)
    }

    /// Executes a given function within circuit breaker.
    ///
    /// Depending on function result value, the call will be recorded as success or failure.
    /// It checks error by the provided predicate. If the predicate returns `true` for the
    /// error, the call is recorded as failure otherwise considered this error as a success.
    fn call_with<P, F, E, R>(&self, predicate: P, f: F) -> Result<R, Error<E>>
    where
        P: FailurePredicate<E>,
        F: FnOnce() -> Result<R, E>;
}

impl<POLICY, INSTRUMENT> CircuitBreaker for StateMachine<POLICY, INSTRUMENT>
where
    POLICY: FailurePolicy,
    INSTRUMENT: Instrument,
{
    #[inline]
    fn is_call_permitted(&self) -> bool {
        self.is_call_permitted()
    }

    fn call_with<P, F, E, R>(&self, predicate: P, f: F) -> Result<R, Error<E>>
    where
        P: FailurePredicate<E>,
        F: FnOnce() -> Result<R, E>,
    {
        if !self.is_call_permitted() {
            return Err(Error::Rejected);
        }

        match f() {
            Ok(ok) => {
                self.on_success();
                Ok(ok)
            }
            Err(err) => {
                if predicate.is_err(&err) {
                    self.on_error();
                } else {
                    self.on_success();
                }
                Err(Error::Inner(err))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::super::backoff;
    use super::super::config::Config;
    use super::super::failure_policy::consecutive_failures;
    use super::*;

    #[test]
    fn call_with() {
        let circuit_breaker = new_circuit_breaker();
        let is_err = |err: &bool| !(*err);

        for _ in 0..2 {
            match circuit_breaker.call_with(is_err, || Err::<(), _>(true)) {
                Err(Error::Inner(true)) => {}
                x => unreachable!("{:?}", x),
            }
            assert_eq!(true, circuit_breaker.is_call_permitted());
        }

        match circuit_breaker.call_with(is_err, || Err::<(), _>(false)) {
            Err(Error::Inner(false)) => {}
            x => unreachable!("{:?}", x),
        }
        assert_eq!(false, circuit_breaker.is_call_permitted());
    }

    #[test]
    fn call_ok() {
        let circuit_breaker = new_circuit_breaker();

        circuit_breaker.call(|| Ok::<_, ()>(())).unwrap();
        assert_eq!(true, circuit_breaker.is_call_permitted());
    }

    #[test]
    fn call_err() {
        let circuit_breaker = new_circuit_breaker();

        match circuit_breaker.call(|| Err::<(), _>(())) {
            Err(Error::Inner(())) => {}
            x => unreachable!("{:?}", x),
        }
        assert_eq!(false, circuit_breaker.is_call_permitted());

        match circuit_breaker.call(|| Err::<(), _>(())) {
            Err(Error::Rejected) => {}
            x => unreachable!("{:?}", x),
        }
        assert_eq!(false, circuit_breaker.is_call_permitted());
    }

    fn new_circuit_breaker() -> impl CircuitBreaker {
        let backoff = backoff::constant(Duration::from_secs(5));
        let policy = consecutive_failures(1, backoff);
        Config::new().failure_policy(policy).build()
    }
}