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