praxis-proxy-protocol 0.7.3

HTTP, TCP, and protocol adapters for Praxis
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024 Praxis Contributors

//! Hot-swappable pipeline storage for protocol adapters.
//!
//! [`ListenerPipelines`] lives in the protocol crate because it is
//! the interface between protocol adapters (which invoke filter
//! execution) and the filter engine (which owns [`FilterPipeline`]).
//!
//! Each pipeline is wrapped in [`ArcSwap`] for lock-free atomic
//! replacement during hot reloads. In-flight requests hold an
//! [`Arc`] guard to the old pipeline, so they drain safely while
//! new requests pick up the replacement.
//!
//! [`FilterPipeline`]: praxis_filter::FilterPipeline
//! [`ArcSwap`]: arc_swap::ArcSwap

use std::{collections::HashMap, sync::Arc};

use arc_swap::ArcSwap;
use praxis_core::config::ProtocolKind;
use praxis_filter::FilterPipeline;

// -----------------------------------------------------------------------------
// ListenerPipelines
// -----------------------------------------------------------------------------

/// Maps listener names to their resolved [`FilterPipeline`]s.
///
/// Each pipeline is wrapped in [`ArcSwap`] so it can be atomically
/// replaced at runtime without blocking in-flight requests.
///
/// ```
/// use std::{collections::HashMap, sync::Arc};
///
/// use praxis_filter::{FilterPipeline, FilterRegistry};
/// use praxis_protocol::ListenerPipelines;
///
/// let registry = FilterRegistry::with_builtins();
/// let pipeline = Arc::new(FilterPipeline::build(&mut [], &registry).unwrap());
///
/// let mut map = HashMap::new();
/// map.insert("web".to_owned(), pipeline);
/// let pipelines = ListenerPipelines::new(map);
///
/// assert!(pipelines.get("web").is_some());
/// assert!(pipelines.get("missing").is_none());
/// ```
///
/// [`ArcSwap`]: arc_swap::ArcSwap
pub struct ListenerPipelines {
    /// Maps listener names to their swappable filter pipelines.
    pipelines: HashMap<String, Arc<ArcSwap<FilterPipeline>>>,
    /// The protocol each listener's pipelines are resolved for.
    ///
    /// A protocol handler is bound once, for the process lifetime, and
    /// executes only filters of its own protocol. Swaps replace the
    /// pipeline in a slot but never this record, so a reload can tell
    /// whether a rebuilt pipeline still matches the handler it targets.
    protocols: HashMap<String, ProtocolKind>,
}

impl ListenerPipelines {
    /// Create from a map of listener name to pipeline.
    ///
    /// Called by the server during startup after building filter pipelines
    /// from the loaded configuration. Each pipeline is wrapped in [`ArcSwap`]
    /// for atomic replacement during hot reloads.
    pub fn new(pipelines: HashMap<String, Arc<FilterPipeline>>) -> Self {
        Self::with_protocols(pipelines, HashMap::new())
    }

    /// Create from a map of listener name to pipeline, recording the
    /// protocol each listener's pipeline was resolved for.
    ///
    /// ```
    /// use std::{collections::HashMap, sync::Arc};
    ///
    /// use praxis_core::config::ProtocolKind;
    /// use praxis_filter::{FilterPipeline, FilterRegistry};
    /// use praxis_protocol::ListenerPipelines;
    ///
    /// let registry = FilterRegistry::with_builtins();
    /// let pipeline = Arc::new(FilterPipeline::build(&mut [], &registry).unwrap());
    ///
    /// let mut map = HashMap::new();
    /// map.insert("db".to_owned(), pipeline);
    /// let mut protocols = HashMap::new();
    /// protocols.insert("db".to_owned(), ProtocolKind::Tcp);
    /// let pipelines = ListenerPipelines::with_protocols(map, protocols);
    ///
    /// assert_eq!(pipelines.protocol("db"), Some(ProtocolKind::Tcp));
    /// ```
    pub fn with_protocols(
        pipelines: HashMap<String, Arc<FilterPipeline>>,
        protocols: HashMap<String, ProtocolKind>,
    ) -> Self {
        let swappable = pipelines
            .into_iter()
            .map(|(name, p)| (name, Arc::new(ArcSwap::from(p))))
            .collect();
        Self {
            pipelines: swappable,
            protocols,
        }
    }

    /// The protocol a listener's pipelines are resolved for, if recorded.
    ///
    /// Fixed at construction: a swap never changes it.
    pub fn protocol(&self, listener_name: &str) -> Option<ProtocolKind> {
        self.protocols.get(listener_name).copied()
    }

    /// Get the swappable pipeline for a listener by name.
    ///
    /// Called by protocol adapters on every request to access the filter
    /// pipeline. The returned [`ArcSwap`] reference allows protocol adapters
    /// to load the current pipeline without blocking reload operations.
    pub fn get(&self, listener_name: &str) -> Option<&Arc<ArcSwap<FilterPipeline>>> {
        self.pipelines.get(listener_name)
    }

    /// Atomically replace the pipeline for a listener.
    ///
    /// No-op if the listener name is not present.
    ///
    /// ```
    /// use std::{collections::HashMap, sync::Arc};
    ///
    /// use praxis_filter::{FilterPipeline, FilterRegistry};
    /// use praxis_protocol::ListenerPipelines;
    ///
    /// let registry = FilterRegistry::with_builtins();
    /// let old = Arc::new(FilterPipeline::build(&mut [], &registry).unwrap());
    /// let new = Arc::new(FilterPipeline::build(&mut [], &registry).unwrap());
    ///
    /// let mut map = HashMap::new();
    /// map.insert("web".to_owned(), old);
    /// let pipelines = ListenerPipelines::new(map);
    ///
    /// pipelines.swap("web", new);
    /// pipelines.swap(
    ///     "nonexistent",
    ///     Arc::new(FilterPipeline::build(&mut [], &registry).unwrap()),
    /// );
    /// ```
    pub fn swap(&self, listener_name: &str, new_pipeline: Arc<FilterPipeline>) {
        if let Some(slot) = self.pipelines.get(listener_name) {
            slot.store(new_pipeline);
        }
    }

    /// Every filesystem path any filter in any listener's pipeline reads
    /// configuration from, de-duplicated.
    ///
    /// Two listeners can share a filter chain, so the same document would
    /// otherwise appear more than once and be watched and hashed repeatedly.
    pub fn referenced_files(&self) -> Vec<std::path::PathBuf> {
        let mut seen = std::collections::BTreeSet::new();
        for name in self.listener_names() {
            if let Some(slot) = self.get(name) {
                for path in slot.load().referenced_files() {
                    seen.insert(path);
                }
            }
        }
        seen.into_iter().collect()
    }

    /// Returns an iterator over listener names.
    ///
    /// Used during config reload to iterate over all listeners when
    /// swapping pipelines or collecting referenced files for watching.
    pub fn listener_names(&self) -> impl Iterator<Item = &str> {
        self.pipelines.keys().map(String::as_str)
    }
}

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

#[cfg(test)]
#[expect(clippy::allow_attributes, reason = "blanket test suppressions")]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::too_many_lines,
    reason = "tests"
)]
mod tests {
    use praxis_filter::FilterRegistry;

    use super::*;

    #[test]
    fn get_returns_pipeline() {
        let pipelines = make_pipelines(&["web"]);
        assert!(pipelines.get("web").is_some(), "should find 'web' pipeline");
    }

    #[test]
    fn get_returns_none_for_missing() {
        let pipelines = make_pipelines(&["web"]);
        assert!(pipelines.get("missing").is_none(), "should return None for missing");
    }

    #[test]
    fn swap_replaces_pipeline_pointer() {
        let pipelines = make_pipelines(&["web"]);
        let old_ptr = Arc::as_ptr(&pipelines.get("web").unwrap().load());

        let registry = FilterRegistry::with_builtins();
        let new_pipeline = Arc::new(FilterPipeline::build(&mut [], &registry).unwrap());
        pipelines.swap("web", Arc::clone(&new_pipeline));

        let new_ptr = Arc::as_ptr(&pipelines.get("web").unwrap().load());
        assert_ne!(old_ptr, new_ptr, "swap should replace the pipeline pointer");
    }

    #[test]
    fn old_guard_remains_valid_after_swap() {
        let pipelines = make_pipelines(&["web"]);
        let old_guard = pipelines.get("web").unwrap().load();
        let old_ptr = Arc::as_ptr(&old_guard);

        let registry = FilterRegistry::with_builtins();
        let new_pipeline = Arc::new(FilterPipeline::build(&mut [], &registry).unwrap());
        pipelines.swap("web", new_pipeline);

        let still_old_ptr = Arc::as_ptr(&old_guard);
        assert_eq!(
            old_ptr, still_old_ptr,
            "old guard should still point to the original pipeline"
        );
    }

    #[test]
    fn swap_nonexistent_is_noop() {
        let pipelines = make_pipelines(&["web"]);
        let registry = FilterRegistry::with_builtins();
        let new_pipeline = Arc::new(FilterPipeline::build(&mut [], &registry).unwrap());
        pipelines.swap("nonexistent", new_pipeline);
        assert!(pipelines.get("web").is_some(), "existing pipeline should be unaffected");
    }

    #[test]
    fn get_returns_arcswap_reference() {
        let pipelines = make_pipelines(&["web"]);
        let slot: &Arc<ArcSwap<FilterPipeline>> = pipelines.get("web").unwrap();
        let _loaded: arc_swap::Guard<Arc<FilterPipeline>> = slot.load();
    }

    #[test]
    fn protocol_is_recorded_at_construction() {
        let registry = FilterRegistry::with_builtins();
        let mut map = HashMap::new();
        map.insert(
            "web".to_owned(),
            Arc::new(FilterPipeline::build(&mut [], &registry).unwrap()),
        );
        let mut protocols = HashMap::new();
        protocols.insert("web".to_owned(), ProtocolKind::Tcp);

        let pipelines = ListenerPipelines::with_protocols(map, protocols);

        assert_eq!(pipelines.protocol("web"), Some(ProtocolKind::Tcp));
        assert_eq!(
            pipelines.protocol("missing"),
            None,
            "unknown listeners have no protocol"
        );
    }

    #[test]
    fn protocol_survives_swap() {
        let registry = FilterRegistry::with_builtins();
        let mut map = HashMap::new();
        map.insert(
            "web".to_owned(),
            Arc::new(FilterPipeline::build(&mut [], &registry).unwrap()),
        );
        let mut protocols = HashMap::new();
        protocols.insert("web".to_owned(), ProtocolKind::Http);
        let pipelines = ListenerPipelines::with_protocols(map, protocols);

        pipelines.swap("web", Arc::new(FilterPipeline::build(&mut [], &registry).unwrap()));

        assert_eq!(
            pipelines.protocol("web"),
            Some(ProtocolKind::Http),
            "a swap replaces the pipeline, never the protocol the handler was bound for"
        );
    }

    #[test]
    fn new_records_no_protocol() {
        let pipelines = make_pipelines(&["web"]);
        assert_eq!(pipelines.protocol("web"), None);
    }

    #[test]
    fn referenced_files_empty_without_listeners() {
        let pipelines = make_pipelines(&[]);
        assert!(
            pipelines.referenced_files().is_empty(),
            "no listeners means no referenced documents"
        );
    }

    #[test]
    fn referenced_files_empty_when_no_filter_declares_one() {
        let pipelines = make_pipelines(&["web"]);
        assert!(
            pipelines.referenced_files().is_empty(),
            "a pipeline of non-declaring filters contributes nothing"
        );
    }

    #[test]
    fn referenced_files_collects_across_listeners() {
        let pipelines =
            make_pipelines_with_documents(&[("web", "/etc/praxis/web.yaml"), ("api", "/etc/praxis/api.yaml")]);
        assert_eq!(
            pipelines.referenced_files(),
            vec![
                std::path::PathBuf::from("/etc/praxis/api.yaml"),
                std::path::PathBuf::from("/etc/praxis/web.yaml"),
            ],
            "every listener's documents must be collected, sorted by the BTreeSet"
        );
    }

    #[test]
    fn referenced_files_dedupes_a_document_shared_by_two_listeners() {
        let shared = "/etc/praxis/shared.yaml";
        let pipelines = make_pipelines_with_documents(&[("web", shared), ("api", shared)]);
        assert_eq!(
            pipelines.referenced_files(),
            vec![std::path::PathBuf::from(shared)],
            "a shared document must appear once"
        );
    }

    // -------------------------------------------------------------------------
    // Test Utilities
    // -------------------------------------------------------------------------

    /// A filter that declares the document named by its `document:` config key.
    struct DocumentReaderFilter {
        document: std::path::PathBuf,
    }

    #[async_trait::async_trait]
    impl praxis_filter::HttpFilter for DocumentReaderFilter {
        fn name(&self) -> &'static str {
            "document_reader"
        }

        fn referenced_files(&self) -> Vec<std::path::PathBuf> {
            vec![self.document.clone()]
        }

        async fn on_request(
            &self,
            _ctx: &mut praxis_filter::HttpFilterContext<'_>,
        ) -> Result<praxis_filter::FilterAction, praxis_filter::FilterError> {
            Ok(praxis_filter::FilterAction::Continue)
        }
    }

    /// Build [`ListenerPipelines`] where each named listener runs a single filter
    /// declaring the given document.
    fn make_pipelines_with_documents(listeners: &[(&str, &str)]) -> ListenerPipelines {
        let mut registry = FilterRegistry::with_builtins();
        registry
            .register(
                "document_reader",
                praxis_filter::FilterFactory::Http(Arc::new(|cfg: &serde_yaml::Value| {
                    let document = cfg
                        .get("document")
                        .and_then(serde_yaml::Value::as_str)
                        .ok_or_else(|| praxis_filter::FilterError::from("document_reader: missing document"))?;
                    let filter: Box<dyn praxis_filter::HttpFilter> = Box::new(DocumentReaderFilter {
                        document: std::path::PathBuf::from(document),
                    });
                    Ok(filter)
                })),
            )
            .unwrap();

        let mut map = HashMap::new();
        for (listener, document) in listeners {
            let yaml = format!("- filter: document_reader\n  document: {document}\n");
            let mut entries: Vec<praxis_core::config::FilterEntry> = serde_yaml::from_str(&yaml).unwrap();
            let pipeline = Arc::new(FilterPipeline::build(&mut entries, &registry).unwrap());
            map.insert((*listener).to_owned(), pipeline);
        }
        ListenerPipelines::new(map)
    }

    /// Build [`ListenerPipelines`] with empty pipelines for the given names.
    fn make_pipelines(names: &[&str]) -> ListenerPipelines {
        let registry = FilterRegistry::with_builtins();
        let mut map = HashMap::new();
        for name in names {
            let pipeline = Arc::new(FilterPipeline::build(&mut [], &registry).unwrap());
            map.insert((*name).to_owned(), pipeline);
        }
        ListenerPipelines::new(map)
    }
}