zentinel-proxy 0.6.11

A security-first reverse proxy built on Pingora with sleepable ops at the edge
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
//! Inference-specific health check for LLM backends.
//!
//! This module provides a Pingora-compatible health check that verifies:
//! - The inference server is responding (HTTP 200)
//! - Expected models are available in the `/v1/models` response
//!
//! # Example Configuration
//!
//! ```kdl
//! upstream "llm-pool" {
//!     targets {
//!         target { address "gpu-1:8080" }
//!     }
//!     health-check {
//!         type "inference" {
//!             endpoint "/v1/models"
//!             expected-models "gpt-4" "llama-3"
//!         }
//!         interval-secs 30
//!     }
//! }
//! ```

use async_trait::async_trait;
use pingora_core::{Error, ErrorType::CustomCode, Result};
use pingora_load_balancing::health_check::HealthCheck as PingoraHealthCheck;
use pingora_load_balancing::Backend;
use serde::Deserialize;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tracing::{debug, trace, warn};

/// Inference health check for LLM/AI backends.
///
/// Implements Pingora's HealthCheck trait to integrate with the load balancing
/// infrastructure. Verifies both server availability and model availability.
pub struct InferenceHealthCheck {
    /// Endpoint to probe (default: /v1/models)
    endpoint: String,
    /// Models that must be present for the backend to be healthy
    expected_models: Vec<String>,
    /// Connection/response timeout
    timeout: Duration,
    /// Consecutive successes needed to mark healthy
    pub consecutive_success: usize,
    /// Consecutive failures needed to mark unhealthy
    pub consecutive_failure: usize,
}

/// OpenAI-compatible models list response.
#[derive(Debug, Deserialize)]
struct ModelsResponse {
    data: Vec<ModelInfo>,
}

/// Individual model info in the response.
#[derive(Debug, Deserialize)]
struct ModelInfo {
    id: String,
    #[serde(default)]
    object: String,
}

impl InferenceHealthCheck {
    /// Create a new inference health check.
    ///
    /// # Arguments
    ///
    /// * `endpoint` - The models endpoint path (e.g., "/v1/models")
    /// * `expected_models` - List of model IDs that must be available
    /// * `timeout` - Connection and response timeout
    pub fn new(endpoint: String, expected_models: Vec<String>, timeout: Duration) -> Self {
        Self {
            endpoint,
            expected_models,
            timeout,
            consecutive_success: 1,
            consecutive_failure: 1,
        }
    }

    /// Perform the actual health check against a target.
    async fn check_backend(&self, addr: &str) -> Result<(), String> {
        // Parse address
        let socket_addr: std::net::SocketAddr = addr
            .parse()
            .map_err(|e| format!("Invalid address '{}': {}", addr, e))?;

        // Connect with timeout
        let stream = tokio::time::timeout(self.timeout, TcpStream::connect(socket_addr))
            .await
            .map_err(|_| format!("Connection timeout after {:?}", self.timeout))?
            .map_err(|e| format!("Connection failed: {}", e))?;

        // Build HTTP request
        let request = format!(
            "GET {} HTTP/1.1\r\n\
             Host: {}\r\n\
             User-Agent: Zentinel-HealthCheck/1.0\r\n\
             Accept: application/json\r\n\
             Connection: close\r\n\r\n",
            self.endpoint, addr
        );

        // Send request
        let mut stream = stream;
        stream
            .write_all(request.as_bytes())
            .await
            .map_err(|e| format!("Failed to send request: {}", e))?;

        // Read response with timeout
        let mut response = vec![0u8; 65536]; // 64KB buffer for models list
        let n = tokio::time::timeout(self.timeout, stream.read(&mut response))
            .await
            .map_err(|_| "Response timeout".to_string())?
            .map_err(|e| format!("Failed to read response: {}", e))?;

        if n == 0 {
            return Err("Empty response".to_string());
        }

        let response_str = String::from_utf8_lossy(&response[..n]);

        // Parse HTTP status
        let status_code = self.parse_status_code(&response_str)?;
        if status_code != 200 {
            return Err(format!("HTTP {} (expected 200)", status_code));
        }

        // If no expected models specified, just check HTTP 200
        if self.expected_models.is_empty() {
            trace!(
                addr = %addr,
                endpoint = %self.endpoint,
                "Inference health check passed (no model verification)"
            );
            return Ok(());
        }

        // Extract JSON body
        let body = self.extract_body(&response_str)?;

        // Parse models response
        let models = self.parse_models_response(body)?;

        // Verify expected models are present
        self.verify_models(&models)?;

        trace!(
            addr = %addr,
            endpoint = %self.endpoint,
            model_count = models.len(),
            expected_models = ?self.expected_models,
            "Inference health check passed"
        );

        Ok(())
    }

    /// Parse HTTP status code from response.
    fn parse_status_code(&self, response: &str) -> Result<u16, String> {
        response
            .lines()
            .next()
            .and_then(|line| line.split_whitespace().nth(1))
            .and_then(|code| code.parse().ok())
            .ok_or_else(|| "Failed to parse HTTP status".to_string())
    }

    /// Extract body from HTTP response.
    fn extract_body<'a>(&self, response: &'a str) -> Result<&'a str, String> {
        response
            .find("\r\n\r\n")
            .map(|pos| &response[pos + 4..])
            .ok_or_else(|| "Could not find response body".to_string())
    }

    /// Parse the models list from JSON response.
    fn parse_models_response(&self, body: &str) -> Result<Vec<String>, String> {
        // Handle chunked encoding - find the actual JSON
        let json_body = if body.starts_with(|c: char| c.is_ascii_hexdigit()) {
            // Chunked encoding: skip the chunk size line
            body.lines()
                .skip(1)
                .take_while(|line| !line.is_empty() && *line != "0")
                .collect::<Vec<_>>()
                .join("\n")
        } else {
            body.to_string()
        };

        // Try parsing as OpenAI-compatible format
        if let Ok(response) = serde_json::from_str::<ModelsResponse>(&json_body) {
            return Ok(response.data.into_iter().map(|m| m.id).collect());
        }

        // Fallback: try parsing as simple array of model objects
        if let Ok(models) = serde_json::from_str::<Vec<ModelInfo>>(&json_body) {
            return Ok(models.into_iter().map(|m| m.id).collect());
        }

        // Last fallback: extract model IDs from any JSON with "id" fields
        if let Ok(json) = serde_json::from_str::<serde_json::Value>(&json_body) {
            if let Some(data) = json.get("data").and_then(|d| d.as_array()) {
                let models: Vec<String> = data
                    .iter()
                    .filter_map(|m| m.get("id").and_then(|id| id.as_str()))
                    .map(String::from)
                    .collect();
                if !models.is_empty() {
                    return Ok(models);
                }
            }

            // Check for models array directly
            if let Some(models_arr) = json.get("models").and_then(|m| m.as_array()) {
                let models: Vec<String> = models_arr
                    .iter()
                    .filter_map(|m| {
                        m.get("id")
                            .or_else(|| m.get("name"))
                            .and_then(|id| id.as_str())
                    })
                    .map(String::from)
                    .collect();
                if !models.is_empty() {
                    return Ok(models);
                }
            }
        }

        Err(format!(
            "Failed to parse models response. Body preview: {}",
            &json_body[..json_body.len().min(200)]
        ))
    }

    /// Verify that all expected models are present.
    fn verify_models(&self, available_models: &[String]) -> Result<(), String> {
        let mut missing = Vec::new();

        for expected in &self.expected_models {
            // Check for exact match or prefix match (for versioned models)
            let found = available_models
                .iter()
                .any(|m| m == expected || m.starts_with(expected) || expected.starts_with(m));

            if !found {
                missing.push(expected.as_str());
            }
        }

        if missing.is_empty() {
            Ok(())
        } else {
            Err(format!(
                "Missing models: {}. Available: {:?}",
                missing.join(", "),
                available_models
            ))
        }
    }
}

#[async_trait]
impl PingoraHealthCheck for InferenceHealthCheck {
    /// Check if the backend is healthy.
    ///
    /// Returns Ok(()) if healthy, Err with message if not.
    async fn check(&self, backend: &Backend) -> Result<()> {
        let addr = backend.addr.to_string();

        match self.check_backend(&addr).await {
            Ok(()) => {
                trace!(
                    addr = %addr,
                    endpoint = %self.endpoint,
                    expected_models = ?self.expected_models,
                    "Inference health check passed"
                );
                Ok(())
            }
            Err(error) => {
                debug!(
                    addr = %addr,
                    endpoint = %self.endpoint,
                    error = %error,
                    "Inference health check failed"
                );
                Err(Error::explain(
                    CustomCode("inference health check", 1),
                    error,
                ))
            }
        }
    }

    /// Return the health threshold for flipping health status.
    ///
    /// * `success: true` - returns consecutive_success (unhealthy -> healthy)
    /// * `success: false` - returns consecutive_failure (healthy -> unhealthy)
    fn health_threshold(&self, success: bool) -> usize {
        if success {
            self.consecutive_success
        } else {
            self.consecutive_failure
        }
    }
}

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

    #[test]
    fn test_parse_openai_models_response() {
        let check = InferenceHealthCheck::new(
            "/v1/models".to_string(),
            vec!["gpt-4".to_string()],
            Duration::from_secs(5),
        );

        let body = r#"{"object":"list","data":[{"id":"gpt-4","object":"model"},{"id":"gpt-3.5-turbo","object":"model"}]}"#;
        let models = check.parse_models_response(body).unwrap();

        assert_eq!(models.len(), 2);
        assert!(models.contains(&"gpt-4".to_string()));
        assert!(models.contains(&"gpt-3.5-turbo".to_string()));
    }

    #[test]
    fn test_parse_ollama_models_response() {
        let check = InferenceHealthCheck::new(
            "/api/tags".to_string(),
            vec!["llama3".to_string()],
            Duration::from_secs(5),
        );

        // Ollama uses "models" array with "name" field
        let body = r#"{"models":[{"name":"llama3:latest"},{"name":"codellama:7b"}]}"#;
        let models = check.parse_models_response(body).unwrap();

        assert_eq!(models.len(), 2);
        assert!(models.contains(&"llama3:latest".to_string()));
    }

    #[test]
    fn test_verify_models_exact_match() {
        let check = InferenceHealthCheck::new(
            "/v1/models".to_string(),
            vec!["gpt-4".to_string(), "gpt-3.5-turbo".to_string()],
            Duration::from_secs(5),
        );

        let available = vec!["gpt-4".to_string(), "gpt-3.5-turbo".to_string()];
        assert!(check.verify_models(&available).is_ok());
    }

    #[test]
    fn test_verify_models_prefix_match() {
        let check = InferenceHealthCheck::new(
            "/v1/models".to_string(),
            vec!["gpt-4".to_string()],
            Duration::from_secs(5),
        );

        // Should match "gpt-4-turbo" when looking for "gpt-4"
        let available = vec!["gpt-4-turbo".to_string(), "gpt-3.5-turbo".to_string()];
        assert!(check.verify_models(&available).is_ok());
    }

    #[test]
    fn test_verify_models_missing() {
        let check = InferenceHealthCheck::new(
            "/v1/models".to_string(),
            vec!["gpt-4".to_string(), "claude-3".to_string()],
            Duration::from_secs(5),
        );

        let available = vec!["gpt-4".to_string(), "gpt-3.5-turbo".to_string()];
        let result = check.verify_models(&available);

        assert!(result.is_err());
        assert!(result.unwrap_err().contains("claude-3"));
    }

    #[test]
    fn test_parse_status_code() {
        let check =
            InferenceHealthCheck::new("/v1/models".to_string(), vec![], Duration::from_secs(5));

        assert_eq!(check.parse_status_code("HTTP/1.1 200 OK\r\n"), Ok(200));
        assert_eq!(
            check.parse_status_code("HTTP/1.1 404 Not Found\r\n"),
            Ok(404)
        );
    }

    #[test]
    fn test_extract_body() {
        let check =
            InferenceHealthCheck::new("/v1/models".to_string(), vec![], Duration::from_secs(5));

        let response = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"data\":[]}";
        let body = check.extract_body(response).unwrap();
        assert_eq!(body, "{\"data\":[]}");
    }
}