Skip to content

Commit

Permalink
draft: add lifetime everywhere in lib to handle &[u8] in TrieNode
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Feb 26, 2024
1 parent 2cd23de commit ebcb27c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 53 deletions.
37 changes: 20 additions & 17 deletions lib/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,23 @@ StateMachineBuilder! {
/// HTTP Session to insert in the SessionManager
///
/// 1 session <=> 1 HTTP connection (client to sozu)
pub struct HttpSession {
pub struct HttpSession<'a> {
answers: Rc<RefCell<HttpAnswers>>,
configured_backend_timeout: Duration,
configured_connect_timeout: Duration,
configured_frontend_timeout: Duration,
frontend_token: Token,
last_event: Instant,
listener: Rc<RefCell<HttpListener>>,
listener: Rc<RefCell<HttpListener<'a>>>,
metrics: SessionMetrics,
pool: Weak<RefCell<Pool>>,
proxy: Rc<RefCell<HttpProxy>>,
proxy: Rc<RefCell<HttpProxy<'a>>>,
state: HttpStateMachine,
sticky_name: String,
has_been_closed: bool,
}

impl HttpSession {
impl HttpSession<'_> {
#[allow(clippy::too_many_arguments)]
pub fn new(
answers: Rc<RefCell<HttpAnswers>>,
Expand Down Expand Up @@ -266,7 +266,7 @@ impl HttpSession {
}
}

impl ProxySession for HttpSession {
impl<'a> ProxySession for HttpSession<'a> {
fn close(&mut self) {
if self.has_been_closed {
return;
Expand Down Expand Up @@ -383,18 +383,18 @@ impl ProxySession for HttpSession {

pub type Hostname = String;

pub struct HttpListener {
pub struct HttpListener<'a> {
active: bool,
address: SocketAddr,
answers: Rc<RefCell<HttpAnswers>>,
config: HttpListenerConfig,
fronts: Router,
fronts: Router<'a>,
listener: Option<TcpListener>,
tags: BTreeMap<String, CachedTags>,
token: Token,
}

impl ListenerHandler for HttpListener {
impl<'a> ListenerHandler for HttpListener<'a> {
fn get_addr(&self) -> &SocketAddr {
&self.address
}
Expand All @@ -411,7 +411,7 @@ impl ListenerHandler for HttpListener {
}
}

impl L7ListenerHandler for HttpListener {
impl<'a> L7ListenerHandler for HttpListener<'a> {
fn get_sticky_name(&self) -> &str {
&self.config.sticky_name
}
Expand Down Expand Up @@ -474,22 +474,22 @@ impl L7ListenerHandler for HttpListener {
}
}

pub struct HttpProxy {
pub struct HttpProxy<'a> {
backends: Rc<RefCell<BackendMap>>,
clusters: HashMap<ClusterId, Cluster>,
listeners: HashMap<Token, Rc<RefCell<HttpListener>>>,
listeners: HashMap<Token, Rc<RefCell<HttpListener<'a>>>>,
pool: Rc<RefCell<Pool>>,
registry: Registry,
sessions: Rc<RefCell<SessionManager>>,
}

impl HttpProxy {
impl<'a> HttpProxy<'a> {
pub fn new(
registry: Registry,
sessions: Rc<RefCell<SessionManager>>,
pool: Rc<RefCell<Pool>>,
backends: Rc<RefCell<BackendMap>>,
) -> HttpProxy {
) -> HttpProxy<'a> {
HttpProxy {
backends,
clusters: HashMap::new(),
Expand Down Expand Up @@ -712,8 +712,11 @@ impl HttpProxy {
}
}

impl HttpListener {
pub fn new(config: HttpListenerConfig, token: Token) -> Result<HttpListener, ListenerError> {
impl<'a> HttpListener<'a> {
pub fn new(
config: HttpListenerConfig,
token: Token,
) -> Result<HttpListener<'a>, ListenerError> {
Ok(HttpListener {
active: false,
address: config.address.clone().into(),
Expand Down Expand Up @@ -789,7 +792,7 @@ impl HttpListener {
}
}

impl ProxyConfiguration for HttpProxy {
impl<'a> ProxyConfiguration for HttpProxy<'a> {
fn notify(&mut self, request: WorkerRequest) -> WorkerResponse {
let request_id = request.id.clone();

Expand Down Expand Up @@ -939,7 +942,7 @@ impl ProxyConfiguration for HttpProxy {
}
}

impl L7Proxy for HttpProxy {
impl<'a> L7Proxy for HttpProxy<'a> {
fn kind(&self) -> ListenerType {
ListenerType::Http
}
Expand Down
36 changes: 18 additions & 18 deletions lib/src/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,25 @@ pub enum AlpnProtocols {
Http11,
}

pub struct HttpsSession {
pub struct HttpsSession<'a> {
answers: Rc<RefCell<HttpAnswers>>,
configured_backend_timeout: Duration,
configured_connect_timeout: Duration,
configured_frontend_timeout: Duration,
frontend_token: Token,
has_been_closed: bool,
last_event: Instant,
listener: Rc<RefCell<HttpsListener>>,
listener: Rc<RefCell<HttpsListener<'a>>>,
metrics: SessionMetrics,
peer_address: Option<StdSocketAddr>,
pool: Weak<RefCell<Pool>>,
proxy: Rc<RefCell<HttpsProxy>>,
proxy: Rc<RefCell<HttpsProxy<'a>>>,
public_address: StdSocketAddr,
state: HttpsStateMachine,
sticky_name: String,
}

impl HttpsSession {
impl<'a> HttpsSession<'a> {
#[allow(clippy::too_many_arguments)]
pub fn new(
answers: Rc<RefCell<HttpAnswers>>,
Expand All @@ -140,7 +140,7 @@ impl HttpsSession {
sticky_name: String,
token: Token,
wait_time: Duration,
) -> HttpsSession {
) -> HttpsSession<'a> {
let peer_address = if expect_proxy {
// Will be defined later once the expect proxy header has been received and parsed
None
Expand Down Expand Up @@ -400,7 +400,7 @@ impl HttpsSession {
}
}

impl ProxySession for HttpsSession {
impl<'a> ProxySession for HttpsSession<'a> {
fn close(&mut self) {
if self.has_been_closed {
return;
Expand Down Expand Up @@ -522,20 +522,20 @@ impl ProxySession for HttpsSession {
pub type HostName = String;
pub type PathBegin = String;

pub struct HttpsListener {
pub struct HttpsListener<'a> {
active: bool,
address: StdSocketAddr,
answers: Rc<RefCell<HttpAnswers>>,
config: HttpsListenerConfig,
fronts: Router,
fronts: Router<'a>,
listener: Option<MioTcpListener>,
resolver: Arc<MutexCertificateResolver>,
rustls_details: Arc<RustlsServerConfig>,
tags: BTreeMap<String, CachedTags>,
token: Token,
}

impl ListenerHandler for HttpsListener {
impl<'a> ListenerHandler for HttpsListener<'a> {
fn get_addr(&self) -> &StdSocketAddr {
&self.address
}
Expand All @@ -552,7 +552,7 @@ impl ListenerHandler for HttpsListener {
}
}

impl L7ListenerHandler for HttpsListener {
impl<'a> L7ListenerHandler for HttpsListener<'a> {
fn get_sticky_name(&self) -> &str {
&self.config.sticky_name
}
Expand Down Expand Up @@ -609,11 +609,11 @@ impl L7ListenerHandler for HttpsListener {
}
}

impl HttpsListener {
impl<'a> HttpsListener<'a> {
pub fn try_new(
config: HttpsListenerConfig,
token: Token,
) -> Result<HttpsListener, ListenerError> {
) -> Result<HttpsListener<'a>, ListenerError> {
let resolver = Arc::new(MutexCertificateResolver::default());

let server_config = Arc::new(Self::create_rustls_context(&config, resolver.to_owned())?);
Expand Down Expand Up @@ -767,22 +767,22 @@ impl HttpsListener {
}
}

pub struct HttpsProxy {
listeners: HashMap<Token, Rc<RefCell<HttpsListener>>>,
pub struct HttpsProxy<'a> {
listeners: HashMap<Token, Rc<RefCell<HttpsListener<'a>>>>,
clusters: HashMap<ClusterId, Cluster>,
backends: Rc<RefCell<BackendMap>>,
pool: Rc<RefCell<Pool>>,
registry: Registry,
sessions: Rc<RefCell<SessionManager>>,
}

impl HttpsProxy {
impl<'a> HttpsProxy<'a> {
pub fn new(
registry: Registry,
sessions: Rc<RefCell<SessionManager>>,
pool: Rc<RefCell<Pool>>,
backends: Rc<RefCell<BackendMap>>,
) -> HttpsProxy {
) -> HttpsProxy<'a> {
HttpsProxy {
listeners: HashMap::new(),
clusters: HashMap::new(),
Expand Down Expand Up @@ -1168,7 +1168,7 @@ impl HttpsProxy {
}
}

impl ProxyConfiguration for HttpsProxy {
impl<'a> ProxyConfiguration for HttpsProxy<'a> {
fn accept(&mut self, token: ListenToken) -> Result<MioTcpStream, AcceptError> {
match self.listeners.get(&Token(token.0)) {
Some(listener) => listener.borrow_mut().accept(),
Expand Down Expand Up @@ -1356,7 +1356,7 @@ impl ProxyConfiguration for HttpsProxy {
}
}
}
impl L7Proxy for HttpsProxy {
impl<'a> L7Proxy for HttpsProxy<'a> {
fn kind(&self) -> ListenerType {
ListenerType::Https
}
Expand Down
10 changes: 5 additions & 5 deletions lib/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ pub enum RouterError {
},
}

pub struct Router {
pub struct Router<'a> {
pre: Vec<(DomainRule, PathRule, MethodRule, Route)>,
pub tree: TrieNode<Vec<(PathRule, MethodRule, Route)>>,
pub tree: TrieNode<'a, Vec<(PathRule, MethodRule, Route)>>,
post: Vec<(DomainRule, PathRule, MethodRule, Route)>,
}

impl Default for Router {
impl Default for Router<'_> {
fn default() -> Self {
Self::new()
}
}

impl Router {
pub fn new() -> Router {
impl<'a> Router<'a> {
pub fn new() -> Router<'a> {
Router {
pre: Vec::new(),
tree: TrieNode::root(),
Expand Down
18 changes: 9 additions & 9 deletions lib/src/router/pattern_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, fmt::Debug, iter, str};

use regex::bytes::Regex;

pub type Key = &[u8];
pub type Key<'a> = &'a [u8];
pub type KeyValue<K, V> = (K, V);

#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -35,14 +35,14 @@ fn find_last_slash(input: &[u8]) -> Option<usize> {
/// Leaves also store a value associated with the complete domain.
/// For Sozu it is a list of (PathRule, MethodRule, ClusterId). See the Router strucure.
#[derive(Debug)]
pub struct TrieNode<V> {
key_value: Option<KeyValue<Key, V>>,
wildcard: Option<KeyValue<Key, V>>,
children: HashMap<Key, TrieNode<V>>,
regexps: Vec<(Regex, TrieNode<V>)>,
pub struct TrieNode<'a, V> {
key_value: Option<KeyValue<Key<'a>, V>>,
wildcard: Option<KeyValue<Key<'a>, V>>,
children: HashMap<Key<'a>, TrieNode<'a, V>>,
regexps: Vec<(Regex, TrieNode<'a, V>)>,
}

impl<V: PartialEq> std::cmp::PartialEq for TrieNode<V> {
impl<'a, V: PartialEq> std::cmp::PartialEq for TrieNode<'a, V> {
fn eq(&self, other: &Self) -> bool {
self.key_value == other.key_value
&& self.wildcard == other.wildcard
Expand All @@ -58,7 +58,7 @@ impl<V: PartialEq> std::cmp::PartialEq for TrieNode<V> {
}
}

impl<V: Debug + Clone> TrieNode<V> {
impl<'a, V: Debug + Clone> TrieNode<'a, V> {
pub fn new(key: Key, value: V) -> TrieNode<V> {
TrieNode {
key_value: Some((key, value)),
Expand All @@ -77,7 +77,7 @@ impl<V: Debug + Clone> TrieNode<V> {
}
}

pub fn root() -> TrieNode<V> {
pub fn root() -> TrieNode<'a, V> {
TrieNode {
key_value: None,
wildcard: None,
Expand Down
8 changes: 4 additions & 4 deletions lib/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub enum ServerError {
///
/// Listeners and sessions are all stored in a slab structure to index them
/// by a [Token], they all have to implement the [ProxySession] trait.
pub struct Server {
pub struct Server<'a> {
accept_queue_timeout: Duration,
accept_queue: VecDeque<(TcpStream, ListenToken, Protocol, Instant)>,
accept_ready: HashSet<ListenToken>,
Expand All @@ -219,8 +219,8 @@ pub struct Server {
channel: ProxyChannel,
config_state: ConfigState,
current_poll_errors: i32,
http: Rc<RefCell<http::HttpProxy>>,
https: Rc<RefCell<https::HttpsProxy>>,
http: Rc<RefCell<http::HttpProxy<'a>>>,
https: Rc<RefCell<https::HttpsProxy<'a>>>,
last_sessions_len: usize,
last_shutting_down_message: Option<Instant>,
last_zombie_check: Instant,
Expand All @@ -238,7 +238,7 @@ pub struct Server {
zombie_check_interval: Duration,
}

impl Server {
impl<'a> Server<'a> {
pub fn try_new_from_config(
worker_to_main_channel: ProxyChannel,
worker_to_main_scm: ScmSocket,
Expand Down

0 comments on commit ebcb27c

Please sign in to comment.