Skip to main content

sozu_lib/protocol/
mod.rs

1//! Protocol-state surface area.
2//!
3//! Defines the [`SessionState`] trait that every front-end protocol
4//! implementation (`h2c`, `mux`, `pipe`, `proxy_protocol`, `rustls`, `tcp_preread`)
5//! plugs into the mio worker loop. `kawa_h1` is no longer one of them: it
6//! keeps the H1 vocabulary (`DefaultAnswer`, `HttpContext`, `Method`) that
7//! `mux` builds on, and its own session state machine was removed on
8//! 2026-09-20 (sozu#1346). State implementations decide how to
9//! react to readiness events, manage timeouts, render their internal state
10//! for debugging, and signal whether the session can be torn down.
11
12pub mod h2c;
13pub mod kawa_h1;
14pub mod mux;
15pub mod pipe;
16pub mod proxy_protocol;
17pub mod rustls;
18pub mod tcp_preread;
19pub mod udp;
20
21use std::{cell::RefCell, rc::Rc};
22
23use mio::Token;
24use sozu_command::ready::Ready;
25
26pub use crate::protocol::{
27    kawa_h1 as http, pipe::Pipe, proxy_protocol::send::SendProxyProtocol, rustls::TlsHandshake,
28};
29use crate::{
30    L7Proxy, ProxySession, SessionIsToBeClosed, SessionMetrics, SessionResult, StateResult,
31};
32
33/// All States should satisfy this trait in order to receive and handle Session events
34pub trait SessionState {
35    /// if a session received an event or can still execute, the event loop will
36    /// call this method. Its result indicates if it can still execute or if the
37    /// session can be closed
38    fn ready(
39        &mut self,
40        session: Rc<RefCell<dyn ProxySession>>,
41        proxy: Rc<RefCell<dyn L7Proxy>>,
42        metrics: &mut SessionMetrics,
43    ) -> SessionResult;
44    /// if the event loop got an event for a token associated with the session,
45    /// it will call this method
46    fn update_readiness(&mut self, token: Token, events: Ready);
47    /// close the state
48    fn close(&mut self, _proxy: Rc<RefCell<dyn L7Proxy>>, _metrics: &mut SessionMetrics) {}
49    /// if a timeout associated with the session triggers, the event loop will
50    /// call this method with the timeout's token
51    fn timeout(&mut self, token: Token, metrics: &mut SessionMetrics) -> StateResult;
52    /// cancel frontend timeout (and backend timeout if present)
53    fn cancel_timeouts(&mut self);
54    /// display the session's internal state (for debugging purpose),
55    /// ```plain
56    /// <context> Session(<State name>):
57    ///     Frontend:
58    ///         - Token(...) Readiness(...)
59    ///     Backends:
60    ///         - Token(...) Readiness(...)
61    ///         - Token(...) Readiness(...)
62    /// ```
63    fn print_state(&self, context: &str);
64    /// tell the session it has to shut down if possible
65    ///
66    /// if the session handles HTTP requests, it will not close until the response
67    /// is completely sent back to the client
68    fn shutting_down(&mut self) -> SessionIsToBeClosed {
69        true
70    }
71    /// Whether this state holds a backend connection `target` names; see
72    /// `ProxySession::targets` (`lib/src/lib.rs`).
73    fn targets(&self, _target: &crate::SessionTarget) -> bool {
74        false
75    }
76}