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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! A library to assist in reporting on the health of a system.

#![warn(
    missing_docs,
    unused_import_braces,
    unused_imports,
    unused_qualifications
)]
#![deny(missing_debug_implementations, trivial_numeric_casts, unused_must_use)]
#![forbid(unsafe_code)]

use async_trait::async_trait;
use parking_lot::Mutex;
use std::{
    borrow::Cow,
    error::Error as StdError,
    ops,
    sync::Arc,
    time::{Duration, Instant},
};

#[cfg(feature = "tokio_0_2")]
use tokio_0_2::time::delay_for as sleep;

#[cfg(all(feature = "tokio_0_3", not(feature = "tokio_0_2")))]
use tokio_0_3::time::sleep;

/// A health status reporter
pub trait Reporter {
    /// The current health status without regard to any reliance criteria
    fn raw_status(&self) -> Status {
        self.last_check().into()
    }

    /// The current health status of the underlying health check or `None` if
    /// the current status should not be relyed upon
    fn status(&self) -> Option<Status> {
        Some(self.raw_status())
    }

    /// The result of the most recent health check
    ///
    /// Because it may take multiple checks to cause the health status to
    /// change, this value may match the current health status.
    fn last_check(&self) -> Check;
}

/// A report of a single health check run
///
/// Default: `Pass`
///
/// ## Operations
///
/// ```
/// use health::Check;
///
/// assert_eq!(Check::Pass, !Check::Failed);
/// assert_eq!(Check::Failed, !Check::Pass);
///
/// assert_eq!(Check::Pass, Check::Pass & Check::Pass);
/// assert_eq!(Check::Failed, Check::Pass & Check::Failed);
/// assert_eq!(Check::Failed, Check::Failed & Check::Pass);
/// assert_eq!(Check::Failed, Check::Failed & Check::Failed);
///
/// assert_eq!(Check::Pass, Check::Pass | Check::Pass);
/// assert_eq!(Check::Pass, Check::Pass | Check::Failed);
/// assert_eq!(Check::Pass, Check::Failed | Check::Pass);
/// assert_eq!(Check::Failed, Check::Failed | Check::Failed);
///
/// assert_eq!(Check::Failed, Check::Pass ^ Check::Pass);
/// assert_eq!(Check::Pass, Check::Pass ^ Check::Failed);
/// assert_eq!(Check::Pass, Check::Failed ^ Check::Pass);
/// assert_eq!(Check::Failed, Check::Failed ^ Check::Failed);
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Check {
    /// The health check passed
    Pass,
    /// The health check failed
    Failed,
}

impl Default for Check {
    #[inline]
    fn default() -> Self {
        Self::Pass
    }
}

impl ops::Not for Check {
    type Output = Self;

    fn not(self) -> Self::Output {
        match self {
            Self::Pass => Self::Failed,
            Self::Failed => Self::Pass,
        }
    }
}

impl ops::BitAnd for Check {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (Self::Pass, Self::Pass) => Self::Pass,
            _ => Self::Failed,
        }
    }
}

impl ops::BitOr for Check {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (Self::Pass, _) | (_, Self::Pass) => Self::Pass,
            _ => Self::Failed,
        }
    }
}

impl ops::BitXor for Check {
    type Output = Self;

    fn bitxor(self, rhs: Self) -> Self::Output {
        if self != rhs {
            Self::Pass
        } else {
            Self::Failed
        }
    }
}

/// The status of a health check, accounting for allowable variance
///
/// Default: `Healthy`
///
/// ## Operations
///
/// ```
/// use health::Status;
///
/// assert_eq!(Status::Healthy, !Status::Unhealthy);
/// assert_eq!(Status::Unhealthy, !Status::Healthy);
///
/// assert_eq!(Status::Healthy, Status::Healthy & Status::Healthy);
/// assert_eq!(Status::Unhealthy, Status::Healthy & Status::Unhealthy);
/// assert_eq!(Status::Unhealthy, Status::Unhealthy & Status::Healthy);
/// assert_eq!(Status::Unhealthy, Status::Unhealthy & Status::Unhealthy);
///
/// assert_eq!(Status::Healthy, Status::Healthy | Status::Healthy);
/// assert_eq!(Status::Healthy, Status::Healthy | Status::Unhealthy);
/// assert_eq!(Status::Healthy, Status::Unhealthy | Status::Healthy);
/// assert_eq!(Status::Unhealthy, Status::Unhealthy | Status::Unhealthy);
///
/// assert_eq!(Status::Unhealthy, Status::Healthy ^ Status::Healthy);
/// assert_eq!(Status::Healthy, Status::Healthy ^ Status::Unhealthy);
/// assert_eq!(Status::Healthy, Status::Unhealthy ^ Status::Healthy);
/// assert_eq!(Status::Unhealthy, Status::Unhealthy ^ Status::Unhealthy);
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Status {
    /// The health check is reporting as healthy
    Healthy,
    /// The health check is reporting as unhealthy
    Unhealthy,
}

impl Default for Status {
    #[inline]
    fn default() -> Self {
        Self::Healthy
    }
}

impl From<Check> for Status {
    fn from(hc: Check) -> Self {
        match hc {
            Check::Pass => Self::Healthy,
            Check::Failed => Self::Unhealthy,
        }
    }
}

impl ops::Not for Status {
    type Output = Self;

    fn not(self) -> Self::Output {
        match self {
            Self::Healthy => Self::Unhealthy,
            Self::Unhealthy => Self::Healthy,
        }
    }
}

impl ops::BitAnd for Status {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (Self::Healthy, Self::Healthy) => Self::Healthy,
            _ => Self::Unhealthy,
        }
    }
}

impl ops::BitOr for Status {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (Self::Healthy, _) | (_, Self::Healthy) => Self::Healthy,
            _ => Self::Unhealthy,
        }
    }
}

impl ops::BitXor for Status {
    type Output = Self;

    fn bitxor(self, rhs: Self) -> Self::Output {
        if self != rhs {
            Self::Healthy
        } else {
            Self::Unhealthy
        }
    }
}

/// Configuration for a periodic health check
///
/// Defaults:
///
/// * Checks every 5 seconds
/// * Becomes unhealthy after 3 consecutive failures
/// * Becomes healthy on first success
/// * Becomes unreliable if no completed checks in 15 seconds
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Config {
    /// Interval over which to periodically run the health check
    pub check_interval: Duration,

    /// Minimum number of consecutive failures to flip an healthy
    /// health check to unhealthy
    pub min_failures: u8,

    /// Minimum number of consecutive successes to flip an unhealthy
    /// health check to healthy
    pub min_successes: u8,

    /// Leeway between updated before the health check status is no longer
    /// considered current
    ///
    /// To deal with variation, this should generally not be less than
    /// twice the `check_interval`.
    pub leeway: Duration,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            check_interval: Duration::from_secs(5),
            min_failures: 3,
            min_successes: 1,
            leeway: Duration::from_secs(15),
        }
    }
}

#[derive(Clone, Copy, Debug, Default)]
struct State {
    status: Status,
    last_update: Duration,
    last_check: Check,
    count: u8,
}

/// A type that exposes a health check
#[async_trait]
pub trait Checkable {
    /// The error reported on a failed health check
    type Error: std::error::Error + Send + Sync + 'static;

    /// The action run to check the current health of the element
    ///
    /// `Ok(())` is interpreted as a passing result. Any `Err(_)`
    /// is interpreted as a failure.
    async fn check(&self) -> Result<(), Self::Error>;

    /// An identifier for the type of the checkable resource
    fn name(&self) -> Cow<str>;
}

/// A background healthcheck for checking the health of the MySQL Pool
#[derive(Clone, Debug)]
pub struct PeriodicChecker<C> {
    inner: Arc<PeriodicCheckerInner<C>>,
}

impl<C: Checkable> Reporter for PeriodicChecker<C> {
    /// The current health status without consideration for when
    /// the health status was last updated
    #[inline]
    fn raw_status(&self) -> Status {
        self.inner.raw_status()
    }

    /// The current health status or `None` if the health check hasn't
    /// updated its internal state within the leeway time
    #[inline]
    fn status(&self) -> Option<Status> {
        self.inner.status()
    }

    /// The result of the most recent health check
    #[inline]
    fn last_check(&self) -> Check {
        self.inner.last_check()
    }
}

impl<C: Checkable> PeriodicChecker<C> {
    /// Creates a new health check for the MySQL pool
    pub fn new(checkable: C, config: Config) -> Self {
        Self {
            inner: Arc::new(PeriodicCheckerInner {
                checkable,
                initialized: Instant::now(),
                state: Mutex::new(State::default()),
                config,
            }),
        }
    }

    /// Begins the health check loop and never returns
    pub async fn run(self) -> ! {
        self.inner.run().await
    }
}

#[derive(Debug)]
struct PeriodicCheckerInner<C> {
    checkable: C,
    initialized: Instant,
    state: Mutex<State>,
    config: Config,
}

impl<C: Checkable> Reporter for PeriodicCheckerInner<C> {
    fn raw_status(&self) -> Status {
        self.state.lock().status
    }

    fn status(&self) -> Option<Status> {
        let state = self.state.lock();
        let now = self.initialized.elapsed();
        if now - state.last_update > self.config.leeway {
            None
        } else {
            Some(state.status)
        }
    }

    fn last_check(&self) -> Check {
        self.state.lock().last_check
    }
}

impl<C: Checkable> PeriodicCheckerInner<C> {
    async fn run(self: Arc<Self>) -> ! {
        let mut delay = sleep(Duration::from_secs(0));
        loop {
            delay.await;
            delay = sleep(self.config.check_interval);
            let result = self.checkable.check().await;
            let mut state = self.state.lock();
            state.last_update = self.initialized.elapsed();
            let prior_status = state.status;

            let error = result.err();

            let this_check = if error.is_none() {
                Check::Pass
            } else {
                Check::Failed
            };

            if state.last_check != this_check {
                state.count = 0;
            }
            state.count = state.count.saturating_add(1);

            let new_status = if this_check == Check::Pass
                && state.status == Status::Unhealthy
                && state.count >= self.config.min_successes
            {
                Status::Healthy
            } else if this_check == Check::Failed
                && state.status == Status::Healthy
                && state.count >= self.config.min_failures
            {
                Status::Unhealthy
            } else {
                state.status
            };

            let count = state.count;

            state.last_check = this_check;
            state.status = new_status;

            drop(state);

            let module = &*self.checkable.name();
            match (new_status, &error) {
                // Report errors while unhealthy and still failing health checks
                (Status::Unhealthy, Some(error)) => {
                    tracing::error!(
                        error = error as &dyn StdError,
                        check = ?this_check,
                        status = ?new_status,
                        count,
                        module,
                        "healthcheck"
                    );
                }
                // Report warnings while healthy but reporting failing health checks
                (Status::Healthy, Some(error)) => {
                    tracing::warn!(
                        error = error as &dyn StdError,
                        check = ?this_check,
                        status = ?new_status,
                        count,
                        module,
                        "healthcheck"
                    );
                }
                // Report info while unhealthy but passing health checks
                (Status::Unhealthy, None) => {
                    tracing::info!(
                        check = ?this_check,
                        status = ?new_status,
                        count,
                        module,
                        "healthcheck"
                    );
                }
                // Report info if just becoming healthy
                (Status::Healthy, None) if prior_status == Status::Unhealthy => {
                    tracing::info!(
                        check = ?this_check,
                        status = ?new_status,
                        count,
                        module,
                        "healthcheck"
                    );
                }
                // Report debug if healthy and passing health checks
                (Status::Healthy, None) => {
                    tracing::debug!(
                        check = ?this_check,
                        status = ?new_status,
                        count,
                        module,
                        "healthcheck"
                    );
                }
            }
        }
    }
}