praxis-proxy-core 0.1.0

Configuration, error types, and server factory for Praxis
Documentation
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
473
474
// SPDX-License-Identifier: LGPL-3.0-only
// Copyright (c) 2024 Shane Utt

//! Shared health state types for active health checking.

use std::{
    collections::HashMap,
    sync::{
        Arc, Mutex,
        atomic::{AtomicBool, Ordering},
    },
};

// -----------------------------------------------------------------------------
// EndpointHealth
// -----------------------------------------------------------------------------

/// Atomic health state for a single upstream endpoint.
///
/// Uses a [`Mutex`] to ensure counter updates are consistent,
/// with a lock-free [`AtomicBool`] cache for hot-path reads
/// via [`is_healthy`].
///
/// ```
/// use praxis_core::health::EndpointHealth;
///
/// let ep = EndpointHealth::new();
/// assert!(ep.is_healthy(), "endpoints start healthy");
///
/// ep.mark_unhealthy();
/// assert!(!ep.is_healthy());
///
/// ep.mark_healthy();
/// assert!(ep.is_healthy());
/// ```
///
/// [`is_healthy`]: EndpointHealth::is_healthy
#[derive(Debug)]
pub struct EndpointHealth {
    /// Lock-free read cache for the hot path.
    healthy_cache: AtomicBool,

    /// Mutex-protected counters for atomic mutation.
    inner: Mutex<HealthInner>,
}

impl EndpointHealth {
    /// Create a new endpoint health state (starts healthy).
    ///
    /// ```
    /// use praxis_core::health::EndpointHealth;
    ///
    /// let ep = EndpointHealth::new();
    /// assert!(ep.is_healthy());
    /// ```
    pub fn new() -> Self {
        Self {
            healthy_cache: AtomicBool::new(true),
            inner: Mutex::new(HealthInner {
                healthy: true,
                consecutive_successes: 0,
                consecutive_failures: 0,
            }),
        }
    }

    /// Returns `true` if the endpoint is considered healthy.
    ///
    /// Lock-free; reads a cached [`AtomicBool`].
    ///
    /// ```
    /// use praxis_core::health::EndpointHealth;
    ///
    /// let ep = EndpointHealth::new();
    /// assert!(ep.is_healthy());
    /// ```
    #[inline]
    pub fn is_healthy(&self) -> bool {
        self.healthy_cache.load(Ordering::Acquire)
    }

    /// Mark the endpoint as healthy and reset failure counter.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    ///
    /// ```
    /// use praxis_core::health::EndpointHealth;
    ///
    /// let ep = EndpointHealth::new();
    /// ep.mark_unhealthy();
    /// ep.mark_healthy();
    /// assert!(ep.is_healthy());
    /// ```
    #[allow(clippy::expect_used, reason = "poisoned mutex is unrecoverable")]
    pub fn mark_healthy(&self) {
        let mut inner = self.inner.lock().expect("health lock poisoned");
        inner.healthy = true;
        inner.consecutive_failures = 0;
        drop(inner);
        self.healthy_cache.store(true, Ordering::Release);
    }

    /// Mark the endpoint as unhealthy and reset success counter.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    ///
    /// ```
    /// use praxis_core::health::EndpointHealth;
    ///
    /// let ep = EndpointHealth::new();
    /// ep.mark_unhealthy();
    /// assert!(!ep.is_healthy());
    /// ```
    #[allow(clippy::expect_used, reason = "poisoned mutex is unrecoverable")]
    pub fn mark_unhealthy(&self) {
        let mut inner = self.inner.lock().expect("health lock poisoned");
        inner.healthy = false;
        inner.consecutive_successes = 0;
        drop(inner);
        self.healthy_cache.store(false, Ordering::Release);
    }

    /// Record a successful probe and return whether the endpoint
    /// transitioned from unhealthy to healthy.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    ///
    /// ```
    /// use praxis_core::health::EndpointHealth;
    ///
    /// let ep = EndpointHealth::new();
    /// ep.mark_unhealthy();
    /// assert!(!ep.record_success(2), "need 2 successes to recover");
    /// assert!(ep.record_success(2), "second success should transition");
    /// assert!(ep.is_healthy());
    /// ```
    #[allow(clippy::expect_used, reason = "poisoned mutex is unrecoverable")]
    pub fn record_success(&self, healthy_threshold: u32) -> bool {
        let transitioned = {
            let mut inner = self.inner.lock().expect("health lock poisoned");
            inner.consecutive_failures = 0;
            inner.consecutive_successes = inner.consecutive_successes.saturating_add(1);
            let t = inner.consecutive_successes >= healthy_threshold && !inner.healthy;
            if t {
                inner.healthy = true;
            }
            t
        };
        if transitioned {
            self.healthy_cache.store(true, Ordering::Release);
        }
        transitioned
    }

    /// Record a failed probe and return whether the endpoint
    /// transitioned from healthy to unhealthy.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    ///
    /// ```
    /// use praxis_core::health::EndpointHealth;
    ///
    /// let ep = EndpointHealth::new();
    /// assert!(!ep.record_failure(3), "need 3 failures to mark down");
    /// assert!(!ep.record_failure(3), "still need one more");
    /// assert!(ep.record_failure(3), "third failure should transition");
    /// assert!(!ep.is_healthy());
    /// ```
    #[allow(clippy::expect_used, reason = "poisoned mutex is unrecoverable")]
    pub fn record_failure(&self, unhealthy_threshold: u32) -> bool {
        let transitioned = {
            let mut inner = self.inner.lock().expect("health lock poisoned");
            inner.consecutive_successes = 0;
            inner.consecutive_failures = inner.consecutive_failures.saturating_add(1);
            let t = inner.consecutive_failures >= unhealthy_threshold && inner.healthy;
            if t {
                inner.healthy = false;
            }
            t
        };
        if transitioned {
            self.healthy_cache.store(false, Ordering::Release);
        }
        transitioned
    }
}

impl Default for EndpointHealth {
    fn default() -> Self {
        Self::new()
    }
}

// -----------------------------------------------------------------------------
// HealthRegistry
// -----------------------------------------------------------------------------

/// Maps cluster names to their endpoint health state.
///
/// Shared between the background health check runner
/// (writer) and the load balancer (reader).
///
/// ```
/// use std::{collections::HashMap, sync::Arc};
///
/// use praxis_core::health::{EndpointHealth, HealthRegistry};
///
/// let mut map = HashMap::new();
/// map.insert(Arc::from("backend"), Arc::new(vec![EndpointHealth::new()]));
/// let registry: HealthRegistry = Arc::new(map);
/// assert!(registry["backend"][0].is_healthy());
/// ```
pub type HealthRegistry = Arc<HashMap<Arc<str>, ClusterHealthState>>;

/// Build a [`HealthRegistry`] from the cluster config.
///
/// Only clusters with a `health_check` config get entries.
/// Endpoints without health checks are not tracked (always
/// considered healthy by the load balancer).
///
/// ```
/// use praxis_core::{config::Cluster, health::build_health_registry};
///
/// let clusters: Vec<Cluster> = vec![];
/// let registry = build_health_registry(&clusters);
/// assert!(registry.is_empty());
/// ```
pub fn build_health_registry(clusters: &[crate::config::Cluster]) -> HealthRegistry {
    let mut map = HashMap::new();
    for cluster in clusters {
        if cluster.health_check.is_some() {
            let state: Vec<EndpointHealth> = cluster.endpoints.iter().map(|_| EndpointHealth::new()).collect();
            map.insert(Arc::clone(&cluster.name), Arc::new(state));
        }
    }
    Arc::new(map)
}

// -----------------------------------------------------------------------------
// HealthInner
// -----------------------------------------------------------------------------

/// Mutable health counters protected by a [`Mutex`].
#[derive(Debug)]
struct HealthInner {
    /// Whether this endpoint is currently considered healthy.
    healthy: bool,

    /// Consecutive successful probes since last failure.
    consecutive_successes: u32,

    /// Consecutive failed probes since last success.
    consecutive_failures: u32,
}

// -----------------------------------------------------------------------------
// ClusterHealthState
// -----------------------------------------------------------------------------

/// Shared health state for all endpoints in a cluster.
///
/// The `Vec` is indexed in the same order as the cluster's
/// endpoint list.
///
/// ```
/// use std::sync::Arc;
///
/// use praxis_core::health::{ClusterHealthState, EndpointHealth};
///
/// let state: ClusterHealthState = Arc::new(vec![EndpointHealth::new(), EndpointHealth::new()]);
/// assert!(state[0].is_healthy());
/// assert!(state[1].is_healthy());
/// ```
pub type ClusterHealthState = Arc<Vec<EndpointHealth>>;

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

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

    use super::*;

    #[test]
    fn endpoint_starts_healthy() {
        let ep = EndpointHealth::new();
        assert!(ep.is_healthy(), "new endpoint should be healthy");
    }

    #[test]
    fn mark_unhealthy_then_healthy() {
        let ep = EndpointHealth::new();
        ep.mark_unhealthy();
        assert!(!ep.is_healthy(), "should be unhealthy after mark_unhealthy");
        ep.mark_healthy();
        assert!(ep.is_healthy(), "should be healthy after mark_healthy");
    }

    #[test]
    fn record_failure_transitions_at_threshold() {
        let ep = EndpointHealth::new();
        assert!(!ep.record_failure(3), "failure 1/3 should not transition");
        assert!(!ep.record_failure(3), "failure 2/3 should not transition");
        assert!(ep.record_failure(3), "failure 3/3 should transition to unhealthy");
        assert!(!ep.is_healthy(), "should be unhealthy after threshold failures");
    }

    #[test]
    fn record_success_transitions_at_threshold() {
        let ep = EndpointHealth::new();
        ep.mark_unhealthy();
        assert!(!ep.record_success(2), "success 1/2 should not transition");
        assert!(ep.record_success(2), "success 2/2 should transition to healthy");
        assert!(ep.is_healthy(), "should be healthy after threshold successes");
    }

    #[test]
    fn failure_resets_success_counter() {
        let ep = EndpointHealth::new();
        ep.mark_unhealthy();
        ep.record_success(3);
        ep.record_failure(1);
        assert!(!ep.record_success(3), "success counter should have reset");
    }

    #[test]
    fn success_resets_failure_counter() {
        let ep = EndpointHealth::new();
        ep.record_failure(3);
        ep.record_failure(3);
        ep.record_success(1);
        assert!(!ep.record_failure(3), "failure counter should have reset");
    }

    #[test]
    fn already_healthy_success_no_transition() {
        let ep = EndpointHealth::new();
        assert!(!ep.record_success(1), "already-healthy should not report transition");
    }

    #[test]
    fn already_unhealthy_failure_no_transition() {
        let ep = EndpointHealth::new();
        ep.mark_unhealthy();
        assert!(!ep.record_failure(1), "already-unhealthy should not report transition");
    }

    #[test]
    fn default_is_healthy() {
        let ep = EndpointHealth::default();
        assert!(ep.is_healthy(), "default should be healthy");
    }

    #[test]
    fn build_registry_only_includes_health_checked_clusters() {
        let clusters = vec![
            crate::config::Cluster {
                health_check: Some(crate::config::HealthCheckConfig {
                    check_type: crate::config::HealthCheckType::Http,
                    path: "/".to_owned(),
                    expected_status: 200,
                    interval_ms: 5000,
                    timeout_ms: 2000,
                    healthy_threshold: 2,
                    unhealthy_threshold: 3,
                }),
                ..crate::config::Cluster::with_defaults("checked", vec!["10.0.0.1:80".into(), "10.0.0.2:80".into()])
            },
            crate::config::Cluster::with_defaults("unchecked", vec!["10.0.0.3:80".into()]),
        ];

        let registry = build_health_registry(&clusters);
        assert!(
            registry.contains_key("checked"),
            "checked cluster should be in registry"
        );
        assert!(
            !registry.contains_key("unchecked"),
            "unchecked cluster should not be in registry"
        );
        assert_eq!(registry["checked"].len(), 2, "checked cluster should have 2 endpoints");
    }

    #[test]
    fn build_registry_empty_clusters() {
        let registry = build_health_registry(&[]);
        assert!(registry.is_empty(), "empty clusters should produce empty registry");
    }

    #[test]
    fn concurrent_record_failure_transitions_exactly_once() {
        let ep = Arc::new(EndpointHealth::new());
        let threshold = 10;

        let handles: Vec<_> = (0..threshold)
            .map(|_| {
                let ep = Arc::clone(&ep);
                thread::spawn(move || ep.record_failure(threshold))
            })
            .collect();

        let transitions: u32 = handles.into_iter().map(|h| u32::from(h.join().unwrap())).sum();

        assert_eq!(
            transitions, 1,
            "exactly one thread should observe the unhealthy transition"
        );
        assert!(
            !ep.is_healthy(),
            "endpoint should be unhealthy after threshold failures"
        );
    }

    #[test]
    fn concurrent_record_success_transitions_exactly_once() {
        let ep = Arc::new(EndpointHealth::new());
        ep.mark_unhealthy();
        let threshold = 10;

        let handles: Vec<_> = (0..threshold)
            .map(|_| {
                let ep = Arc::clone(&ep);
                thread::spawn(move || ep.record_success(threshold))
            })
            .collect();

        let transitions: u32 = handles.into_iter().map(|h| u32::from(h.join().unwrap())).sum();

        assert_eq!(
            transitions, 1,
            "exactly one thread should observe the healthy transition"
        );
        assert!(ep.is_healthy(), "endpoint should be healthy after threshold successes");
    }

    #[test]
    fn concurrent_mixed_probes_stay_consistent() {
        let ep = Arc::new(EndpointHealth::new());

        let handles: Vec<_> = (0..100)
            .map(|i| {
                let ep = Arc::clone(&ep);
                thread::spawn(move || {
                    if i % 2 == 0 {
                        ep.record_failure(5);
                    } else {
                        ep.record_success(5);
                    }
                })
            })
            .collect();

        for h in handles {
            h.join().unwrap();
        }

        let inner = ep.inner.lock().expect("health lock poisoned");
        assert_eq!(
            inner.healthy,
            ep.is_healthy(),
            "cache must match inner state after concurrent mixed probes"
        );
    }
}