|
11 | 11 | // SPDX-License-Identifier: Apache-2.0 |
12 | 12 | // ******************************************************************************* |
13 | 13 |
|
14 | | -use crate::tag::MonitorTag; |
15 | 14 | use core::hash::Hash; |
16 | 15 | use core::time::Duration; |
17 | | -use std::sync::Arc; |
18 | 16 | use std::time::Instant; |
19 | 17 |
|
20 | 18 | /// Range of accepted time. |
@@ -44,88 +42,6 @@ pub(crate) fn duration_to_u32(duration: Duration) -> u32 { |
44 | 42 | u32::try_from(millis).expect("Monitor running for too long") |
45 | 43 | } |
46 | 44 |
|
47 | | -/// Heartbeat monitor error subgroup. |
48 | | -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, crate::log::ScoreDebug)] |
49 | | -pub(crate) enum HeartbeatMonitorEvaluationError { |
50 | | - /// Multiple heartbeats were observed in the same epoch. |
51 | | - MultipleHeartbeats, |
52 | | -} |
53 | | - |
54 | | -/// Errors that can occur during monitor evaluation. |
55 | | -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, crate::log::ScoreDebug)] |
56 | | -pub(crate) enum MonitorEvaluationError { |
57 | | - TooEarly, |
58 | | - TooLate, |
59 | | - HeartbeatSpecific(HeartbeatMonitorEvaluationError), |
60 | | -} |
61 | | - |
62 | | -/// Trait for evaluating monitors and reporting errors to be used by HealthMonitor. |
63 | | -pub(crate) trait MonitorEvaluator { |
64 | | - /// Run monitor evaluation. |
65 | | - /// |
66 | | - /// - `hmon_starting_point` - starting point of all monitors. |
67 | | - /// - `on_error` - error handling, containing tag of a failing monitor and error code. |
68 | | - fn evaluate(&self, hmon_starting_point: Instant, on_error: &mut dyn FnMut(&MonitorTag, MonitorEvaluationError)); |
69 | | -} |
70 | | - |
71 | | -/// Handle to a monitor evaluator, allowing for dynamic dispatch. |
72 | | -pub(crate) struct MonitorEvalHandle { |
73 | | - inner: Arc<dyn MonitorEvaluator + Send + Sync>, |
74 | | -} |
75 | | - |
76 | | -impl MonitorEvalHandle { |
77 | | - pub(crate) fn new<T: MonitorEvaluator + Send + Sync + 'static>(inner: Arc<T>) -> Self { |
78 | | - Self { inner } |
79 | | - } |
80 | | -} |
81 | | - |
82 | | -impl MonitorEvaluator for MonitorEvalHandle { |
83 | | - fn evaluate(&self, hmon_starting_point: Instant, on_error: &mut dyn FnMut(&MonitorTag, MonitorEvaluationError)) { |
84 | | - self.inner.evaluate(hmon_starting_point, on_error) |
85 | | - } |
86 | | -} |
87 | | - |
88 | | -pub(crate) mod ffi { |
89 | | - use core::mem::ManuallyDrop; |
90 | | - use core::ops::{Deref, DerefMut}; |
91 | | - |
92 | | - pub(crate) type FFIHandle = *mut core::ffi::c_void; |
93 | | - |
94 | | - pub(crate) const HM_OK: i32 = 0; |
95 | | - pub(crate) const HM_NOT_FOUND: i32 = HM_OK + 1; |
96 | | - pub(crate) const HM_ALREADY_EXISTS: i32 = HM_OK + 2; |
97 | | - pub(crate) const _HM_INVALID_ARGS: i32 = HM_OK + 3; |
98 | | - pub(crate) const _HM_WRONG_STATE: i32 = HM_OK + 4; |
99 | | - pub(crate) const HM_FAILED: i32 = HM_OK + 5; |
100 | | - |
101 | | - /// A wrapper to represent borrowed data over FFI boundary without taking ownership. |
102 | | - pub(crate) struct FFIBorrowed<T> { |
103 | | - data: ManuallyDrop<T>, |
104 | | - } |
105 | | - |
106 | | - impl<T> FFIBorrowed<T> { |
107 | | - pub(crate) fn new(data: T) -> Self { |
108 | | - Self { |
109 | | - data: ManuallyDrop::new(data), |
110 | | - } |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - impl<T: Deref> Deref for FFIBorrowed<T> { |
115 | | - type Target = T; |
116 | | - |
117 | | - fn deref(&self) -> &Self::Target { |
118 | | - &self.data |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - impl<T: DerefMut> DerefMut for FFIBorrowed<T> { |
123 | | - fn deref_mut(&mut self) -> &mut Self::Target { |
124 | | - &mut self.data |
125 | | - } |
126 | | - } |
127 | | -} |
128 | | - |
129 | 45 | #[cfg(test)] |
130 | 46 | mod tests { |
131 | 47 | use crate::common::{duration_to_u32, hmon_time_offset}; |
|
0 commit comments