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