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