Skip to content

Commit

Permalink
simplify backend_from_request
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal authored and FlorentinDUBOIS committed Jul 13, 2022
1 parent 8f1b0cc commit e2f40a5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 94 deletions.
61 changes: 31 additions & 30 deletions lib/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,32 @@ impl Session {

Ok((&host, &rl.uri, &rl.method))
}

fn set_backend(
&mut self,
backend: Rc<RefCell<Backend>>,
front_should_stick: bool,
sticky_name: &str,
) {
if front_should_stick {
self.http_mut().map(|http| {
http.sticky_session = Some(StickySession::new(
backend
.borrow()
.sticky_id
.clone()
.unwrap_or_else(|| backend.borrow().backend_id.clone()),
));
http.sticky_name = sticky_name.to_string();
});
}
self.metrics.backend_id = Some(backend.borrow().backend_id.clone());
self.metrics.backend_start();
self.http_mut().map(|http| {
http.set_backend_id(backend.borrow().backend_id.clone());
});
self.backend = Some(backend);
}
}

impl ProxySession for Session {
Expand Down Expand Up @@ -1230,10 +1256,6 @@ impl Proxy {
cluster_id: &str,
front_should_stick: bool,
) -> Result<TcpStream, ConnectionError> {
session
.http_mut()
.map(|h| h.set_cluster_id(String::from(cluster_id)));

let sticky_session = session
.http()
.and_then(|http| http.request.as_ref())
Expand Down Expand Up @@ -1263,28 +1285,8 @@ impl Proxy {
Err(e)
}
Ok((backend, conn)) => {
if front_should_stick {
let sticky_name = self.listeners[&session.listen_token]
.config
.sticky_name
.clone();
session.http_mut().map(|http| {
http.sticky_session = Some(StickySession::new(
backend
.borrow()
.sticky_id
.clone()
.unwrap_or_else(|| backend.borrow().backend_id.clone()),
));
http.sticky_name = sticky_name;
});
}
session.metrics.backend_id = Some(backend.borrow().backend_id.clone());
session.metrics.backend_start();
session.http_mut().map(|http| {
http.set_backend_id(backend.borrow().backend_id.clone());
});
session.backend = Some(backend);
let sticky_name = &self.listeners[&session.listen_token].config.sticky_name;
session.set_backend(backend, front_should_stick, &sticky_name);

Ok(conn)
}
Expand Down Expand Up @@ -1509,6 +1511,9 @@ impl ProxyConfiguration<Session> for Proxy {
}

session.cluster_id = Some(cluster_id.clone());
session.http_mut().map(|http| {
http.cluster_id = Some(cluster_id.clone());
});

let front_should_stick = self
.clusters
Expand All @@ -1517,10 +1522,6 @@ impl ProxyConfiguration<Session> for Proxy {
.unwrap_or(false);
let mut socket = self.backend_from_request(session, &cluster_id, front_should_stick)?;

session.http_mut().map(|http| {
http.cluster_id = Some(cluster_id.clone());
});

if let Err(e) = socket.set_nodelay(true) {
error!(
"error setting nodelay on back socket({:?}): {:?}",
Expand Down
64 changes: 34 additions & 30 deletions lib/src/https_openssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,32 @@ impl Session {

Ok((&host, &rl.uri, &rl.method))
}

fn set_backend(
&mut self,
backend: Rc<RefCell<Backend>>,
front_should_stick: bool,
sticky_name: &str,
) {
if front_should_stick {
self.http_mut().map(|http| {
http.sticky_session = Some(StickySession::new(
backend
.borrow()
.sticky_id
.clone()
.unwrap_or(backend.borrow().backend_id.clone()),
));
http.sticky_name = sticky_name.to_string();
});
}
self.metrics.backend_id = Some(backend.borrow().backend_id.clone());
self.metrics.backend_start();
self.http_mut().map(|http| {
http.set_backend_id(backend.borrow().backend_id.clone());
});
self.backend = Some(backend);
}
}

impl ProxySession for Session {
Expand Down Expand Up @@ -1902,10 +1928,6 @@ impl Proxy {
cluster_id: &str,
front_should_stick: bool,
) -> Result<TcpStream, ConnectionError> {
session
.http_mut()
.map(|h| h.set_cluster_id(String::from(cluster_id)));

let sticky_session = session
.http()
.and_then(|http| http.request.as_ref())
Expand Down Expand Up @@ -1935,28 +1957,11 @@ impl Proxy {
Err(e)
}
Ok((backend, conn)) => {
if front_should_stick {
let sticky_name = self.listeners[&session.listen_token]
.config
.sticky_name
.clone();
session.http_mut().map(|http| {
http.sticky_session = Some(StickySession::new(
backend
.borrow()
.sticky_id
.clone()
.unwrap_or(backend.borrow().backend_id.clone()),
));
http.sticky_name = sticky_name;
});
}
session.metrics.backend_id = Some(backend.borrow().backend_id.clone());
session.metrics.backend_start();
session.http_mut().map(|http| {
http.set_backend_id(backend.borrow().backend_id.clone());
});
session.backend = Some(backend);
let sticky_name = &self.listeners[&session.listen_token]
.config
.sticky_name
.clone();
session.set_backend(backend, front_should_stick, &sticky_name);

Ok(conn)
}
Expand Down Expand Up @@ -2120,6 +2125,9 @@ impl ProxyConfiguration<Session> for Proxy {
}

session.cluster_id = Some(cluster_id.clone());
session.http_mut().map(|http| {
http.cluster_id = Some(cluster_id.clone());
});

let front_should_stick = self
.clusters
Expand All @@ -2128,10 +2136,6 @@ impl ProxyConfiguration<Session> for Proxy {
.unwrap_or(false);
let mut socket = self.backend_from_request(session, &cluster_id, front_should_stick)?;

session.http_mut().map(|http| {
http.cluster_id = Some(cluster_id.clone());
});

if let Err(e) = socket.set_nodelay(true) {
error!(
"error setting nodelay on back socket({:?}): {:?}",
Expand Down
38 changes: 5 additions & 33 deletions lib/src/https_rustls/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use crate::protocol::http::{
answers::HttpAnswers,
parser::{hostname_and_port, Method},
};
use crate::protocol::StickySession;
use crate::router::Router;
use crate::server::{
ListenSession, ListenToken, ProxyChannel, ProxySessionCast, Server, SessionManager,
Expand Down Expand Up @@ -339,10 +338,6 @@ impl Proxy {
cluster_id: &str,
front_should_stick: bool,
) -> Result<TcpStream, ConnectionError> {
session
.http_mut()
.map(|h| h.set_cluster_id(String::from(cluster_id)));

let sticky_session = session
.http()
.and_then(|http| http.request.as_ref())
Expand Down Expand Up @@ -372,30 +367,8 @@ impl Proxy {
Err(e)
}
Ok((backend, conn)) => {
if front_should_stick {
let sticky_name = self.listeners[&session.listen_token]
.config
.sticky_name
.clone();
session.http_mut().map(|http| {
http.sticky_session = Some(StickySession::new(
backend
.borrow()
.sticky_id
.clone()
.unwrap_or(backend.borrow().backend_id.clone()),
));
http.sticky_name = sticky_name;
});
}
session.metrics.backend_id = Some(backend.borrow().backend_id.clone());
session.metrics.backend_start();

session.http_mut().map(|http| {
http.set_backend_id(backend.borrow().backend_id.clone());
});

session.backend = Some(backend);
let sticky_name = &self.listeners[&session.listen_token].config.sticky_name;
session.set_backend(backend, front_should_stick, &sticky_name);

Ok(conn)
}
Expand Down Expand Up @@ -554,6 +527,9 @@ impl ProxyConfiguration<Session> for Proxy {
}

session.cluster_id = Some(cluster_id.clone());
session.http_mut().map(|http| {
http.cluster_id = Some(cluster_id.clone());
});

let front_should_stick = self
.clusters
Expand All @@ -562,10 +538,6 @@ impl ProxyConfiguration<Session> for Proxy {
.unwrap_or(false);
let mut socket = self.backend_from_request(session, &cluster_id, front_should_stick)?;

session.http_mut().map(|http| {
http.cluster_id = Some(cluster_id.clone());
});

// we still want to use the new socket
if let Err(e) = socket.set_nodelay(true) {
error!("error setting nodelay on back socket: {:?}", e);
Expand Down
30 changes: 29 additions & 1 deletion lib/src/https_rustls/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::protocol::http::parser::{hostname_and_port, Method, RRequestLine, Req
use crate::protocol::http::{answers::HttpAnswers, DefaultAnswerStatus};
use crate::protocol::proxy_protocol::expect::ExpectProxyProtocol;
use crate::protocol::rustls::TlsHandshake;
use crate::protocol::{Http, Pipe, ProtocolResult};
use crate::protocol::{Http, Pipe, ProtocolResult, StickySession};
use crate::retry::RetryPolicy;
use crate::server::{push_event, CONN_RETRIES};
use crate::socket::FrontRustls;
Expand Down Expand Up @@ -980,6 +980,34 @@ impl Session {

Ok((&host, &rl.uri, &rl.method))
}

pub fn set_backend(
&mut self,
backend: Rc<RefCell<Backend>>,
front_should_stick: bool,
sticky_name: &str,
) {
if front_should_stick {
self.http_mut().map(|http| {
http.sticky_session = Some(StickySession::new(
backend
.borrow()
.sticky_id
.clone()
.unwrap_or(backend.borrow().backend_id.clone()),
));
http.sticky_name = sticky_name.to_string();
});
}
self.metrics.backend_id = Some(backend.borrow().backend_id.clone());
self.metrics.backend_start();

self.http_mut().map(|http| {
http.set_backend_id(backend.borrow().backend_id.clone());
});

self.backend = Some(backend);
}
}

impl ProxySession for Session {
Expand Down

0 comments on commit e2f40a5

Please sign in to comment.