proxima-primitives 0.1.0

The pipe algebra itself: the Pipe trait — an async In -> Result<Out, Err> that becomes a transform, source, sink or observe by choice of types — plus its three tier siblings (SendPipe, UnpinPipe, UnpinSendPipe), Handler, SourcePipe, the payload types Carry, ResponseStream and RequestStream (buffered bodies are plain Bytes), HeaderList, the sans-IO byte-stream traits, runtime-agnostic concurrency primitives, and the Replay/GenericStream transport seam. Everything else in proxima composes what is defined here.
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
use bytes::Bytes;
use std::future::Future;
use std::panic::AssertUnwindSafe;
use std::pin::Pin;
use std::time::Duration;

use bon::Builder;
use conflaguration::Settings;
use futures::FutureExt;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::pipe::ProximaError;
use crate::pipe::SendPipe;
use crate::pipe::handler::{Handler, PipeHandle, ThreadLocalPipeHandle, into_handle};
use crate::pipe::pipe_factory::PipeFactory;
use crate::pipe::primitives::Pipe;
use crate::pipe::request::{Request, Response};
use crate::pipe::telemetry_surface::Labels;

const METRIC_TIMEOUT: &str = "proxima.isolate.timeout_total";
const METRIC_PANIC: &str = "proxima.isolate.panic_total";

/// blast-radius barrier around an inner Pipe. enforces:
/// - **time budget** — `proxima_core::time::timeout` drops the inner future and
///   returns 503 when exceeded; the rest of the chain stays healthy.
/// - **panic barrier** — `catch_unwind` converts a panic in the inner
///   future into a 500 response instead of unwinding through the chain.
///
/// either guard is independently optional; with both off this is a
/// pass-through.
///
/// generic over the inner handle so the same wrapper composes a `Send`
/// inner (`PipeHandle`, the default — yields `impl Handler`) or a
/// per-thread `!Send`-tolerant inner (`ThreadLocalPipeHandle` — yields
/// `impl ThreadLocalHandler`). The Send variant participates in
/// cross-thread bootstrap; the per-thread variant lets DPDK / per-core
/// runtimes hold `Rc`/`RefCell`/`!Send` impl state.
pub struct Isolate<Inner = PipeHandle> {
    pub inner: Inner,
    pub timeout: Option<Duration>,
    pub panic_barrier: bool,
}

impl<Inner> Isolate<Inner> {
    #[must_use]
    pub fn new(inner: Inner) -> Self {
        Self {
            inner,
            timeout: None,
            panic_barrier: false,
        }
    }

    #[must_use]
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    #[must_use]
    pub fn with_panic_barrier(mut self, enabled: bool) -> Self {
        self.panic_barrier = enabled;
        self
    }
}

impl Isolate<PipeHandle> {
    pub fn from_spec(inner: PipeHandle, value: &Value) -> Result<Self, ProximaError> {
        let config: IsolateConfig = serde_json::from_value(value.clone())
            .map_err(|err| ProximaError::Config(format!("isolate config: {err}")))?;
        Ok(config.into_isolate(inner))
    }
}

/// Typed config surface for the `isolate` middleware — a blast-radius barrier
/// with an optional time budget and panic barrier around an inner pipe.
#[derive(Debug, Clone, Default, Builder, Deserialize, Serialize, Settings)]
#[settings(prefix = "PROXIMA_ISOLATE")]
#[builder(derive(Clone, Debug))]
pub struct IsolateConfig {
    /// Time budget in ms; the inner future is dropped and a 503 returned when
    /// exceeded. `None` disables the timeout guard.
    #[setting(default)]
    #[serde(default)]
    pub timeout_ms: Option<u64>,

    /// Convert an inner panic into a 500 instead of unwinding the chain.
    #[setting(default)]
    #[serde(default)]
    #[builder(default)]
    pub panic_barrier: bool,
}

impl IsolateConfig {
    /// Materialise the isolate middleware around `inner`.
    #[must_use]
    pub fn into_isolate(self, inner: PipeHandle) -> Isolate<PipeHandle> {
        Isolate {
            inner,
            timeout: self.timeout_ms.map(Duration::from_millis),
            panic_barrier: self.panic_barrier,
        }
    }
}

impl<Inner> SendPipe for Isolate<Inner>
where
    Inner: Handler + Clone,
{
    type In = Request<Bytes>;
    type Out = Response<Bytes>;
    type Err = ProximaError;

    fn call(
        &self,
        request: Request<Bytes>,
    ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> + Send {
        let inner = self.inner.clone();
        let timeout = self.timeout;
        let panic_barrier = self.panic_barrier;
        let telemetry = request.context.telemetry.clone();
        let labels = request.context.metric_labels(&[]);
        async move {
            let guarded = run_with_panic_barrier(inner, request, panic_barrier);
            let outcome = match timeout {
                Some(budget) => match proxima_core::time::timeout(budget, guarded).await {
                    Ok(outcome) => outcome.unwrap_or_else(|reason| panic_response(&reason)),
                    Err(_elapsed) => {
                        telemetry.counter_inc(METRIC_TIMEOUT, &labels, 1);
                        Ok(timeout_response(budget))
                    }
                },
                None => guarded
                    .await
                    .unwrap_or_else(|reason| panic_response(&reason)),
            };
            outcome.map(|response| record_panic_metric(response, &telemetry, &labels))
        }
    }
}

impl Pipe for Isolate<ThreadLocalPipeHandle> {
    type In = Request<Bytes>;
    type Out = Response<Bytes>;
    type Err = ProximaError;

    fn call(
        &self,
        request: Request<Bytes>,
    ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> {
        let inner = self.inner.clone();
        let timeout = self.timeout;
        let panic_barrier = self.panic_barrier;
        let telemetry = request.context.telemetry.clone();
        let labels = request.context.metric_labels(&[]);
        async move {
            let guarded = run_with_panic_barrier_tls(inner, request, panic_barrier);
            let outcome = match timeout {
                Some(budget) => match proxima_core::time::timeout(budget, guarded).await {
                    Ok(outcome) => outcome.unwrap_or_else(|reason| panic_response(&reason)),
                    Err(_elapsed) => {
                        telemetry.counter_inc(METRIC_TIMEOUT, &labels, 1);
                        Ok(timeout_response(budget))
                    }
                },
                None => guarded
                    .await
                    .unwrap_or_else(|reason| panic_response(&reason)),
            };
            outcome.map(|response| record_panic_metric(response, &telemetry, &labels))
        }
    }
}

/// run inner.call with optional catch_unwind. returns Ok(outcome) when no
/// panic was caught, Err(reason) when the inner future panicked. the outer
/// caller then decides whether the panic gets a timeout-style mapping or
/// the direct panic_response.
async fn run_with_panic_barrier<Inner: Handler + Clone>(
    inner: Inner,
    request: Request<Bytes>,
    enabled: bool,
) -> Result<Result<Response<Bytes>, ProximaError>, String> {
    if !enabled {
        return Ok(SendPipe::call(&inner, request).await);
    }
    let future = AssertUnwindSafe(SendPipe::call(&inner, request));
    match future.catch_unwind().await {
        Ok(outcome) => Ok(outcome),
        Err(panic) => Err(describe_panic(panic.as_ref())),
    }
}

async fn run_with_panic_barrier_tls(
    inner: ThreadLocalPipeHandle,
    request: Request<Bytes>,
    enabled: bool,
) -> Result<Result<Response<Bytes>, ProximaError>, String> {
    if !enabled {
        return Ok(Pipe::call(&inner, request).await);
    }
    let future = AssertUnwindSafe(Pipe::call(&inner, request));
    match future.catch_unwind().await {
        Ok(outcome) => Ok(outcome),
        Err(panic) => Err(describe_panic(panic.as_ref())),
    }
}

fn describe_panic(payload: &(dyn std::any::Any + Send)) -> String {
    if let Some(message) = payload.downcast_ref::<&'static str>() {
        (*message).to_string()
    } else if let Some(message) = payload.downcast_ref::<String>() {
        message.clone()
    } else {
        "unknown panic payload".to_string()
    }
}

fn panic_response(reason: &str) -> Result<Response<Bytes>, ProximaError> {
    Ok(Response::new(500)
        .with_header("x-proxima-isolated", "panic")
        .with_body(bytes::Bytes::from(format!(
            "isolated pipe panicked: {reason}"
        ))))
}

fn timeout_response(budget: Duration) -> Response<Bytes> {
    Response::new(503)
        .with_header("x-proxima-isolated", "timeout")
        .with_body(bytes::Bytes::from(format!(
            "isolated pipe exceeded {}ms budget",
            budget.as_millis()
        )))
}

/// look at the outgoing response for our own panic header so we can bump
/// the metric exactly once, regardless of whether the panic was caught
/// directly or by way of the timeout wrapper.
fn record_panic_metric(
    response: Response<Bytes>,
    telemetry: &crate::pipe::telemetry_surface::TelemetryHandle,
    labels: &Labels,
) -> Response<Bytes> {
    if response.metadata.iter().any(|(name, value)| {
        name.eq_ignore_ascii_case(b"x-proxima-isolated") && value.as_ref() == b"panic"
    }) {
        telemetry.counter_inc(METRIC_PANIC, labels, 1);
    }
    response
}

pub struct IsolateFactory;

impl PipeFactory for IsolateFactory {
    fn name(&self) -> &str {
        "isolate"
    }

    fn build(
        &self,
        spec: &Value,
        inner: Option<PipeHandle>,
    ) -> Pin<Box<dyn Future<Output = Result<PipeHandle, ProximaError>> + Send + '_>> {
        let spec = spec.clone();
        Box::pin(async move {
            let inner = inner
                .ok_or_else(|| ProximaError::Config("isolate requires an inner pipe".into()))?;
            let isolate = Isolate::from_spec(inner, &spec)?;
            Ok(into_handle(isolate))
        })
    }
}

// `#[proxima::test]` and inline `tokio::task::{LocalSet, spawn_local}` pull
// in the `proxima` / `tokio` dev-dependencies, which the loom build keeps
// out of the graph (see `[target.'cfg(not(loom))'.dev-dependencies]` in
// Cargo.toml); these tests are unrelated to the Notify/watch loom protocol.
#[cfg(all(test, not(loom)))]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::pipe::telemetry_surface::{Telemetry, TelemetryHandle};
    use std::collections::HashMap;
    use std::sync::Arc;
    use std::sync::Mutex;
    use std::sync::atomic::{AtomicBool, Ordering};

    #[derive(Default)]
    struct CounterRecorder {
        counters: Mutex<HashMap<String, u64>>,
    }

    impl Telemetry for CounterRecorder {
        fn counter_inc(&self, metric: &str, _labels: &Labels, by: u64) {
            *self
                .counters
                .lock()
                .unwrap()
                .entry(metric.to_string())
                .or_insert(0) += by;
        }
        fn gauge_set(&self, _: &str, _: &Labels, _: i64) {}
        fn histogram_record(&self, _: &str, _: &Labels, _: f64) {}
    }

    impl CounterRecorder {
        fn counter(&self, metric: &str) -> Option<u64> {
            self.counters.lock().unwrap().get(metric).copied()
        }
    }

    struct SleepingPipe {
        millis: u64,
    }

    impl SendPipe for SleepingPipe {
        type In = Request<Bytes>;
        type Out = Response<Bytes>;
        type Err = ProximaError;

        fn call(
            &self,
            _request: Request<Bytes>,
        ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> + Send {
            let millis = self.millis;
            async move {
                proxima_core::time::sleep(Duration::from_millis(millis)).await;
                Ok(Response::ok("done"))
            }
        }
    }

    struct PanickingPipe;

    impl SendPipe for PanickingPipe {
        type In = Request<Bytes>;
        type Out = Response<Bytes>;
        type Err = ProximaError;

        fn call(
            &self,
            _request: Request<Bytes>,
        ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> + Send {
            async { panic!("intentional test panic") }
        }
    }

    fn request_with_metrics() -> (Request<Bytes>, Arc<CounterRecorder>) {
        let metrics: Arc<CounterRecorder> = Arc::new(CounterRecorder::default());
        let telemetry: TelemetryHandle = metrics.clone();
        let request = Request::builder()
            .method("GET")
            .path("/x")
            .body("")
            .telemetry(telemetry)
            .build()
            .expect("builder");
        (request, metrics)
    }

    // principle-4 parity: the fluent builder and the config value must lower to
    // identical Isolate state (timeout + panic barrier).
    #[test]
    fn parity_fluent_builder_and_config_value_match() {
        let from_value: IsolateConfig =
            serde_json::from_value(serde_json::json!({"timeout_ms": 250, "panic_barrier": true}))
                .expect("from_value");
        let from_value = from_value.into_isolate(into_handle(SleepingPipe { millis: 0 }));

        let from_builder = IsolateConfig::builder()
            .timeout_ms(250)
            .panic_barrier(true)
            .build()
            .into_isolate(into_handle(SleepingPipe { millis: 0 }));

        assert_eq!(from_value.timeout, from_builder.timeout);
        assert_eq!(from_value.timeout, Some(Duration::from_millis(250)));
        assert_eq!(from_value.panic_barrier, from_builder.panic_barrier);
    }

    #[proxima::test]
    async fn passthrough_when_no_guards_enabled() {
        let inner = into_handle(SleepingPipe { millis: 0 });
        let isolate = Isolate::new(inner);
        let (request, _metrics) = request_with_metrics();
        let response = SendPipe::call(&isolate, request).await.expect("call");
        assert_eq!(response.status, 200);
    }

    #[proxima::test]
    async fn timeout_returns_503_and_increments_metric() {
        // real-time test — 10ms budget vs 1s sleep is plenty of headroom
        // without bringing in tokio's `test-util` feature for pause/advance.
        let inner = into_handle(SleepingPipe { millis: 1_000 });
        let isolate = Isolate::new(inner).with_timeout(Duration::from_millis(10));
        let (request, metrics) = request_with_metrics();
        let response = SendPipe::call(&isolate, request).await.expect("call");
        assert_eq!(response.status, 503);
        let isolated = response
            .metadata
            .iter()
            .find(|(name, _)| name.eq_ignore_ascii_case(b"x-proxima-isolated"))
            .expect("isolated header");
        assert_eq!(isolated.1.as_ref(), b"timeout");
        let count = metrics.counter(METRIC_TIMEOUT).expect("timeout counter");
        assert_eq!(count, 1);
    }

    #[proxima::test]
    async fn panic_barrier_converts_panic_to_500_and_increments_metric() {
        let inner = into_handle(PanickingPipe);
        let isolate = Isolate::new(inner).with_panic_barrier(true);
        let (request, metrics) = request_with_metrics();
        let response = SendPipe::call(&isolate, request).await.expect("call");
        assert_eq!(response.status, 500);
        let isolated = response
            .metadata
            .iter()
            .find(|(name, _)| name.eq_ignore_ascii_case(b"x-proxima-isolated"))
            .expect("isolated header");
        assert_eq!(isolated.1.as_ref(), b"panic");
        let count = metrics.counter(METRIC_PANIC).expect("panic counter");
        assert_eq!(count, 1);
    }

    #[proxima::test(runtime = "tokio")]
    async fn panic_without_barrier_propagates() {
        let inner = into_handle(PanickingPipe);
        let isolate = Isolate::new(inner); // barrier disabled
        let (request, _metrics) = request_with_metrics();
        // panic propagates through .await — catch via spawn_local's JoinError.
        // Handler::call returns ?Send (per-core only); must spawn on LocalSet.
        let outcome = tokio::task::LocalSet::new()
            .run_until(async move {
                tokio::task::spawn_local(async move { SendPipe::call(&isolate, request).await })
                    .await
            })
            .await;
        assert!(outcome.is_err(), "panic must propagate without barrier");
    }

    #[proxima::test]
    async fn timeout_does_not_fire_for_fast_inner() {
        let inner = into_handle(SleepingPipe { millis: 0 });
        let isolate = Isolate::new(inner).with_timeout(Duration::from_millis(100));
        let (request, _metrics) = request_with_metrics();
        let response = SendPipe::call(&isolate, request).await.expect("call");
        assert_eq!(response.status, 200);
    }

    #[proxima::test]
    async fn neighbor_request_unaffected_after_inner_panic() {
        // proves the "rest of chain unaffected" acceptance: build a chain
        // where one call panics (caught by barrier), then a second call
        // through the same Isolate succeeds.
        let toggled = Arc::new(AtomicBool::new(false));
        struct OnceFail {
            toggled: Arc<AtomicBool>,
        }

        impl SendPipe for OnceFail {
            type In = Request<Bytes>;
            type Out = Response<Bytes>;
            type Err = ProximaError;

            fn call(
                &self,
                _request: Request<Bytes>,
            ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> + Send {
                let toggled = self.toggled.clone();
                async move {
                    if toggled.swap(true, Ordering::SeqCst) {
                        Ok(Response::ok("recovered"))
                    } else {
                        panic!("first-call panic")
                    }
                }
            }
        }

        let inner = into_handle(OnceFail {
            toggled: toggled.clone(),
        });
        let isolate = Isolate::new(inner).with_panic_barrier(true);
        let (request_one, _m1) = request_with_metrics();
        let first = SendPipe::call(&isolate, request_one).await.expect("first");
        assert_eq!(first.status, 500);
        let (request_two, _m2) = request_with_metrics();
        let second = SendPipe::call(&isolate, request_two).await.expect("second");
        assert_eq!(second.status, 200);
    }
}