Skip to main content

praxis_protocol/
lib.rs

1// SPDX-License-Identifier: MIT
2// Copyright (c) 2024 Praxis Contributors
3
4#![deny(unreachable_pub)]
5
6//! Protocol adapters for Praxis.
7
8use praxis_core::{PingoraServerRuntime, ProxyError, config::Config};
9use tokio::sync::watch;
10
11mod pipelines;
12pub use pipelines::ListenerPipelines;
13
14/// Process-wide connection limit.
15pub mod connections;
16/// HTTP protocol implementations.
17pub mod http;
18/// Raw TCP/L4 forwarding protocol.
19pub mod tcp;
20
21/// Shared TLS settings builder for HTTP and TCP listeners.
22pub(crate) mod tls_setup;
23
24// -----------------------------------------------------------------------------
25// CertWatcherShutdowns
26// -----------------------------------------------------------------------------
27
28/// Collected TLS certificate watcher shutdown senders.
29///
30/// Keeps [`watch::Sender`]s alive so that background [`CertWatcher`]
31/// tasks run until the process exits. Dropping these senders signals
32/// the watchers to stop.
33///
34/// [`watch::Sender`]: tokio::sync::watch::Sender
35/// [`CertWatcher`]: praxis_tls::watcher::CertWatcher
36pub struct CertWatcherShutdowns {
37    /// Shutdown senders kept alive for the server lifetime.
38    _senders: Vec<watch::Sender<bool>>,
39}
40
41impl CertWatcherShutdowns {
42    /// Wrap collected shutdown senders.
43    pub fn new(senders: Vec<watch::Sender<bool>>) -> Self {
44        Self { _senders: senders }
45    }
46}
47
48// -----------------------------------------------------------------------------
49// Protocol
50// -----------------------------------------------------------------------------
51
52/// A protocol implementation that registers services onto a shared server runtime.
53pub trait Protocol: Send {
54    /// Register this protocol's services. Does not block.
55    ///
56    /// Returns any TLS certificate watcher shutdown senders. The
57    /// caller must keep these alive until server shutdown; dropping
58    /// them signals the watcher tasks to stop.
59    ///
60    /// # Errors
61    ///
62    /// Returns [`ProxyError`] if listener binding or setup fails.
63    ///
64    /// [`ProxyError`]: praxis_core::ProxyError
65    fn register(
66        self: Box<Self>,
67        server: &mut PingoraServerRuntime,
68        config: &Config,
69        pipelines: &ListenerPipelines,
70    ) -> Result<Vec<watch::Sender<bool>>, ProxyError>;
71}