zentinel-agent-protocol 0.6.11

Agent protocol and IPC for Zentinel reverse proxy external processors
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# Client & Server APIs (v2)

This document covers the v2 APIs for building agent integrations with connection pooling, multiple transports, and reverse connections.

## Quick Start

```rust
use zentinel_agent_protocol::v2::{AgentPool, AgentPoolConfig, LoadBalanceStrategy};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a connection pool
    let config = AgentPoolConfig {
        connections_per_agent: 4,
        load_balance_strategy: LoadBalanceStrategy::LeastConnections,
        request_timeout: Duration::from_secs(30),
        ..Default::default()
    };

    let pool = AgentPool::with_config(config);

    // Add agents (transport auto-detected from endpoint)
    pool.add_agent("waf", "localhost:50051").await?;           // gRPC
    pool.add_agent("auth", "/var/run/auth.sock").await?;       // UDS

    // Send requests through the pool
    let response = pool.send_request_headers("waf", &headers).await?;

    Ok(())
}
```

---

## AgentPool

The `AgentPool` is the primary interface for v2 agent communication. It manages connections, load balancing, health tracking, and metrics.

### Creating a Pool

```rust
use zentinel_agent_protocol::v2::{AgentPool, AgentPoolConfig, LoadBalanceStrategy};

// Default configuration
let pool = AgentPool::new();

// Custom configuration
let config = AgentPoolConfig {
    connections_per_agent: 4,
    load_balance_strategy: LoadBalanceStrategy::LeastConnections,
    request_timeout: Duration::from_secs(30),
    connect_timeout: Duration::from_secs(5),
    health_check_interval: Duration::from_secs(10),
    circuit_breaker_threshold: 5,
    circuit_breaker_reset_timeout: Duration::from_secs(30),
};

let pool = AgentPool::with_config(config);
```

### Adding Agents

```rust
// gRPC agent (detected by host:port format)
pool.add_agent("waf", "localhost:50051").await?;
pool.add_agent("remote-waf", "waf.internal:50051").await?;

// UDS agent (detected by path format)
pool.add_agent("auth", "/var/run/zentinel/auth.sock").await?;

// Explicit transport selection
pool.add_grpc_agent("waf", "localhost:50051", tls_config).await?;
pool.add_uds_agent("auth", "/var/run/auth.sock").await?;
```

### Sending Requests

```rust
use zentinel_agent_protocol::v2::RequestHeaders;

let headers = RequestHeaders {
    request_id: 1,
    method: "POST".to_string(),
    uri: "/api/users".to_string(),
    headers: vec![
        ("content-type".to_string(), "application/json".to_string()),
    ],
    has_body: true,
    metadata: request_metadata,
};

// Send to specific agent
let response = pool.send_request_headers("waf", &headers).await?;

// Send body chunks
let chunk = RequestBodyChunk {
    request_id: 1,
    chunk_index: 0,
    data: base64::encode(&body_bytes),
    is_last: true,
};
let response = pool.send_request_body_chunk("waf", &chunk).await?;
```

### Cancelling Requests

```rust
// Cancel specific request
pool.cancel_request("waf", request_id).await?;

// Cancel all requests for an agent
pool.cancel_all("waf").await?;
```

### Removing Agents

```rust
pool.remove_agent("waf").await?;
```

### Pool Methods

| Method | Description |
|--------|-------------|
| `new()` | Create pool with default config |
| `with_config(config)` | Create pool with custom config |
| `add_agent(name, endpoint)` | Add agent with auto-detected transport |
| `add_grpc_agent(name, endpoint, tls)` | Add gRPC agent explicitly |
| `add_uds_agent(name, path)` | Add UDS agent explicitly |
| `add_reverse_connection(name, client, caps)` | Add reverse-connected agent |
| `remove_agent(name)` | Remove agent from pool |
| `send_request_headers(agent, headers)` | Send request headers |
| `send_request_body_chunk(agent, chunk)` | Send request body chunk |
| `send_response_headers(agent, headers)` | Send response headers |
| `send_response_body_chunk(agent, chunk)` | Send response body chunk |
| `cancel_request(agent, request_id)` | Cancel specific request |
| `cancel_all(agent)` | Cancel all requests |
| `get_health(agent)` | Get agent health status |
| `metrics_collector()` | Get metrics collector reference |

---

## AgentClientV2 (gRPC)

Low-level gRPC client for direct use without pooling.

### Creating a Client

```rust
use zentinel_agent_protocol::v2::AgentClientV2;

let client = AgentClientV2::connect(
    "waf-agent",
    "http://localhost:50051",
    Duration::from_secs(30),
).await?;

// With TLS
let client = AgentClientV2::connect_with_tls(
    "waf-agent",
    "https://waf.internal:50051",
    tls_config,
    Duration::from_secs(30),
).await?;
```

### Sending Messages

```rust
// Send request headers
let response = client.send_request_headers(&headers).await?;

// Send body chunk
let response = client.send_request_body_chunk(&chunk).await?;

// Cancel request
client.cancel_request(request_id).await?;
```

### Health Checking

```rust
let is_healthy = client.health_check().await?;
let capabilities = client.get_capabilities().await?;
```

---

## AgentClientV2Uds (Unix Domain Socket)

Low-level UDS client for direct use without pooling.

### Creating a Client

```rust
use zentinel_agent_protocol::v2::AgentClientV2Uds;

let client = AgentClientV2Uds::connect(
    "auth-agent",
    "/var/run/zentinel/auth.sock",
    Duration::from_secs(30),
).await?;
```

### Handshake

The UDS client performs automatic handshake on connection:

```rust
// Handshake is automatic, but you can query capabilities
let capabilities = client.capabilities();

println!("Agent: {}", capabilities.agent_name);
println!("Handles body: {}", capabilities.handles_request_body);
println!("Max concurrent: {:?}", capabilities.max_concurrent_requests);
```

### Sending Messages

```rust
// Send request headers
let response = client.send_request_headers(&headers).await?;

// Send body chunk
let response = client.send_request_body_chunk(&chunk).await?;

// Cancel request
client.cancel_request(request_id).await?;
client.cancel_all().await?;
```

---

## ReverseConnectionListener

Accepts inbound connections from agents.

### Creating a Listener

```rust
use zentinel_agent_protocol::v2::{ReverseConnectionListener, ReverseConnectionConfig};

let config = ReverseConnectionConfig {
    handshake_timeout: Duration::from_secs(10),
    max_connections_per_agent: 4,
    require_auth: false,
    allowed_agents: None,
};

let listener = ReverseConnectionListener::bind_uds(
    "/var/run/zentinel/agents.sock",
    config,
).await?;
```

### Accepting Connections

```rust
// Accept a single connection
let (client, registration) = listener.accept().await?;
println!("Agent connected: {}", registration.agent_id);

// Add to pool
pool.add_reverse_connection(
    &registration.agent_id,
    client,
    registration.capabilities,
).await?;
```

### Accept Loop

```rust
// Run accept loop in background
let pool_clone = pool.clone();
tokio::spawn(async move {
    loop {
        match listener.accept().await {
            Ok((client, registration)) => {
                let _ = pool_clone.add_reverse_connection(
                    &registration.agent_id,
                    client,
                    registration.capabilities,
                ).await;
            }
            Err(e) => {
                tracing::error!("Accept error: {}", e);
            }
        }
    }
});
```

---

## V2Transport

Unified transport abstraction for all v2 transport types.

```rust
use zentinel_agent_protocol::v2::V2Transport;

pub enum V2Transport {
    Grpc(AgentClientV2),
    Uds(AgentClientV2Uds),
    Reverse(ReverseConnectionClient),
}

impl V2Transport {
    pub async fn send_request_headers(&mut self, headers: &RequestHeaders) -> Result<Decision>;
    pub async fn send_request_body_chunk(&mut self, chunk: &RequestBodyChunk) -> Result<Decision>;
    pub async fn send_response_headers(&mut self, headers: &ResponseHeaders) -> Result<Decision>;
    pub async fn send_response_body_chunk(&mut self, chunk: &ResponseBodyChunk) -> Result<Decision>;
    pub async fn cancel_request(&mut self, request_id: u64) -> Result<()>;
    pub async fn cancel_all(&mut self) -> Result<()>;
    pub fn is_healthy(&self) -> bool;
}
```

---

## Configuration Types

### AgentPoolConfig

```rust
pub struct AgentPoolConfig {
    /// Number of connections to maintain per agent
    pub connections_per_agent: usize,  // Default: 4

    /// Load balancing strategy
    pub load_balance_strategy: LoadBalanceStrategy,  // Default: LeastConnections

    /// Timeout for individual requests
    pub request_timeout: Duration,  // Default: 30s

    /// Timeout for establishing connections
    pub connect_timeout: Duration,  // Default: 5s

    /// Interval between health checks
    pub health_check_interval: Duration,  // Default: 10s

    /// Failures before opening circuit breaker
    pub circuit_breaker_threshold: u32,  // Default: 5

    /// Time before circuit breaker resets
    pub circuit_breaker_reset_timeout: Duration,  // Default: 30s
}
```

### LoadBalanceStrategy

```rust
pub enum LoadBalanceStrategy {
    /// Distribute requests evenly across connections
    RoundRobin,

    /// Route to connection with fewest in-flight requests
    LeastConnections,

    /// Prefer healthier connections based on error rates
    HealthBased,

    /// Random selection
    Random,
}
```

### ReverseConnectionConfig

```rust
pub struct ReverseConnectionConfig {
    /// Timeout for registration handshake
    pub handshake_timeout: Duration,  // Default: 10s

    /// Maximum connections per agent ID
    pub max_connections_per_agent: usize,  // Default: 4

    /// Require authentication token
    pub require_auth: bool,  // Default: false

    /// Allowlist of agent IDs (None = allow all)
    pub allowed_agents: Option<Vec<String>>,
}
```

---

## Metrics

### MetricsCollector

```rust
let metrics = pool.metrics_collector();

// Get metrics snapshot
let snapshot = metrics.snapshot();
println!("Total requests: {}", snapshot.total_requests);
println!("Active connections: {}", snapshot.active_connections);

// Export in Prometheus format
let prometheus_output = metrics.export_prometheus();
```

### Available Metrics

| Metric | Type | Description |
|--------|------|-------------|
| `agent_requests_total` | Counter | Total requests by agent and decision |
| `agent_request_duration_seconds` | Histogram | Request latency distribution |
| `agent_connections_active` | Gauge | Current active connections |
| `agent_errors_total` | Counter | Error counts by type |
| `agent_circuit_breaker_state` | Gauge | Circuit breaker state (0=closed, 1=open) |

---

## Error Handling

### V2-Specific Errors

```rust
pub enum AgentProtocolError {
    // ... existing errors ...

    /// Connection was closed unexpectedly (v2)
    #[error("Connection closed")]
    ConnectionClosed,
}
```

### Pool Error Handling

```rust
match pool.send_request_headers("waf", &headers).await {
    Ok(decision) => {
        // Handle decision
    }
    Err(AgentProtocolError::Timeout) => {
        // Request timed out - apply fallback policy
    }
    Err(AgentProtocolError::ConnectionClosed) => {
        // Connection lost - pool will reconnect automatically
    }
    Err(e) => {
        tracing::error!("Agent error: {}", e);
    }
}
```

---

## Header Utilities

The `headers` module provides optimized header types that minimize allocations in the hot path.

### Zero-Allocation Header Names

The `intern_header_name()` function returns static references for common HTTP headers, avoiding string allocation:

```rust
use zentinel_agent_protocol::headers::{intern_header_name, CowHeaderMap, HeaderValues};
use std::borrow::Cow;

// Known headers return static references (no allocation)
let content_type = intern_header_name("Content-Type");
assert!(matches!(content_type, Cow::Borrowed(_)));
assert_eq!(content_type, "content-type"); // Normalized to lowercase

// Unknown headers allocate once
let custom = intern_header_name("X-Custom-Header");
assert!(matches!(custom, Cow::Owned(_)));
```

### Supported Header Names

32 common headers are interned as static strings:

| Category | Headers |
|----------|---------|
| **Request** | `host`, `user-agent`, `accept`, `accept-encoding`, `accept-language`, `authorization`, `cookie`, `origin`, `referer` |
| **Response** | `content-type`, `content-length`, `set-cookie`, `location`, `server`, `date`, `etag`, `last-modified`, `cache-control` |
| **Caching** | `if-match`, `if-none-match`, `if-modified-since`, `vary` |
| **Connection** | `connection`, `transfer-encoding` |
| **Proxy** | `x-forwarded-for`, `x-forwarded-proto`, `x-forwarded-host`, `x-real-ip` |
| **Tracing** | `x-request-id`, `x-correlation-id`, `x-trace-id`, `x-span-id` |

### CowHeaderMap

Use `CowHeaderMap` for zero-allocation header storage:

```rust
use zentinel_agent_protocol::headers::{
    CowHeaderMap, HeaderValues, intern_header_name,
    to_cow_optimized, from_cow_optimized, iter_flat_cow,
};
use std::collections::HashMap;

// Build headers with interned names
let mut headers = CowHeaderMap::new();
headers.insert(
    intern_header_name("content-type"),
    HeaderValues::from_iter(["application/json".to_string()])
);

// Convert from standard HashMap
let std_headers: HashMap<String, Vec<String>> = get_headers_from_somewhere();
let cow_headers = to_cow_optimized(std_headers);

// Convert back to standard format
let std_again = from_cow_optimized(cow_headers);

// Iterate without allocation
for (name, value) in iter_flat_cow(&cow_headers) {
    println!("{}: {}", name, value);
}
```

### SmallVec Header Values

Header values use `SmallVec<[String; 1]>` to avoid heap allocation for single-value headers (the common case):

```rust
use zentinel_agent_protocol::headers::HeaderValues;

// Single value: stored inline (no heap allocation)
let single: HeaderValues = HeaderValues::from_iter(["value".to_string()]);
assert!(!single.spilled()); // Stored inline

// Multiple values: spills to heap
let multi: HeaderValues = HeaderValues::from_iter([
    "value1".to_string(),
    "value2".to_string(),
]);
assert!(multi.spilled()); // On heap
```

### Performance Impact

| Optimization | Improvement |
|--------------|-------------|
| Header name interning | ~95% fewer string allocations |
| SmallVec single values | 40% faster header creation |
| Cow-based map | 17% faster header map building |

---

## Memory-Mapped Buffers (Feature: `mmap-buffers`)

For large request/response bodies, enable the `mmap-buffers` feature for efficient memory-mapped storage.

### Enabling the Feature

```toml
[dependencies]
zentinel-agent-protocol = { version = "0.3", features = ["mmap-buffers"] }
```

### Using LargeBodyBuffer

```rust
use zentinel_agent_protocol::mmap_buffer::{LargeBodyBuffer, LargeBodyBufferConfig};

let config = LargeBodyBufferConfig {
    mmap_threshold: 1024 * 1024,      // 1MB - switch to mmap above this
    max_body_size: 100 * 1024 * 1024, // 100MB max
    temp_dir: None,                    // Use system temp
};

let mut buffer = LargeBodyBuffer::with_config(config);

// Write chunks (automatically uses mmap for large bodies)
buffer.write_chunk(chunk1)?;
buffer.write_chunk(chunk2)?;

// Read back
let data = buffer.as_slice()?;

// Check storage type
if buffer.is_mmap() {
    println!("Using memory-mapped storage");
}

// Convert to Vec (reads mmap into memory)
let vec = buffer.into_vec()?;
```

### Configuration Options

| Option | Default | Description |
|--------|---------|-------------|
| `mmap_threshold` | 1MB | Size above which to use mmap |
| `max_body_size` | 100MB | Maximum allowed body size |
| `temp_dir` | System temp | Directory for temp files |

### When to Use

| Scenario | Use mmap-buffers? |
|----------|------------------|
| File uploads > 1MB | Yes |
| Large API responses | Yes |
| Streaming media | Yes |
| Small JSON payloads | No (overhead not worth it) |
| Memory-constrained environments | Yes |

---

## Best Practices

1. **Use AgentPool for production**: The pool handles connection management, health tracking, and load balancing automatically.

2. **Configure appropriate timeouts**: Set `request_timeout` based on your latency requirements.

3. **Enable circuit breakers**: Prevent cascading failures with appropriate thresholds.

4. **Monitor metrics**: Use the MetricsCollector to track agent health and performance.

5. **Use UDS for co-located agents**: UDS provides significantly lower latency than gRPC for local communication.

6. **Use header interning**: Call `intern_header_name()` when building header maps to minimize allocations.

7. **Enable mmap-buffers for large bodies**: For file uploads or large responses, use the `mmap-buffers` feature.

8. **Implement graceful shutdown**: Cancel pending requests before shutting down.

```rust
// Graceful shutdown
for agent_name in pool.agent_names() {
    pool.cancel_all(&agent_name).await?;
}
```