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