Skip to content

Commit

Permalink
Fix empty interest on expect proxy proto upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: Eloi DEMOLIS <eloi.demolis@clever-cloud.com>
  • Loading branch information
Wonshtrum committed Jul 17, 2023
1 parent c6446e1 commit 211db27
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 58 deletions.
83 changes: 40 additions & 43 deletions lib/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl HttpSession {
};

let metrics = SessionMetrics::new(Some(wait_time));
let mut session = HttpSession {
Ok(HttpSession {
answers,
configured_backend_timeout,
configured_connect_timeout,
Expand All @@ -157,10 +157,7 @@ impl HttpSession {
proxy,
state,
sticky_name,
};

session.state.front_readiness().interest = Ready::READABLE | Ready::HUP | Ready::ERROR;
Ok(session)
})
}

pub fn upgrade(&mut self) -> SessionIsToBeClosed {
Expand All @@ -182,6 +179,44 @@ impl HttpSession {
}
}

fn upgrade_expect(
&mut self,
expect: ExpectProxyProtocol<TcpStream>,
) -> Option<HttpStateMachine> {
debug!("switching to HTTP");
match expect
.addresses
.as_ref()
.map(|add| (add.destination(), add.source()))
{
Some((Some(public_address), Some(session_address))) => {
let mut http = Http::new(
self.answers.clone(),
self.configured_backend_timeout,
self.configured_connect_timeout,
self.configured_frontend_timeout,
expect.container_frontend_timeout,
expect.frontend,
expect.frontend_token,
self.listener.clone(),
self.pool.clone(),
Protocol::HTTP,
public_address,
expect.request_id,
Some(session_address),
self.sticky_name.clone(),
)
.ok()?;
http.frontend_readiness.event = expect.frontend_readiness.event;

gauge_add!("protocol.proxy.expect", -1);
gauge_add!("protocol.http", 1);
Some(HttpStateMachine::Http(http))
}
_ => None,
}
}

fn upgrade_http(&mut self, http: Http<TcpStream, HttpListener>) -> Option<HttpStateMachine> {
debug!("http switching to ws");
let front_token = self.frontend_token;
Expand Down Expand Up @@ -222,44 +257,6 @@ impl HttpSession {
Some(HttpStateMachine::WebSocket(pipe))
}

fn upgrade_expect(
&mut self,
expect: ExpectProxyProtocol<TcpStream>,
) -> Option<HttpStateMachine> {
debug!("switching to HTTP");
match expect
.addresses
.as_ref()
.map(|add| (add.destination(), add.source()))
{
Some((Some(public_address), Some(session_address))) => {
let mut http = Http::new(
self.answers.clone(),
self.configured_backend_timeout,
self.configured_connect_timeout,
self.configured_frontend_timeout,
expect.container_frontend_timeout,
expect.frontend,
expect.frontend_token,
self.listener.clone(),
self.pool.clone(),
Protocol::HTTP,
public_address,
expect.request_id,
Some(session_address),
self.sticky_name.clone(),
)
.ok()?;
http.frontend_readiness.event = expect.frontend_readiness.event;

gauge_add!("protocol.proxy.expect", -1);
gauge_add!("protocol.http", 1);
Some(HttpStateMachine::Http(http))
}
_ => None,
}
}

fn upgrade_websocket(&self, ws: Pipe<TcpStream, HttpListener>) -> Option<HttpStateMachine> {
// what do we do here?
error!("Upgrade called on WS, this should not happen");
Expand Down
17 changes: 6 additions & 11 deletions lib/src/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl HttpsSession {
};

let metrics = SessionMetrics::new(Some(wait_time));
let mut session = HttpsSession {
HttpsSession {
answers,
configured_backend_timeout,
configured_connect_timeout,
Expand All @@ -185,10 +185,7 @@ impl HttpsSession {
public_address,
state,
sticky_name,
};

session.state.front_readiness().interest = Ready::READABLE | Ready::HUP | Ready::ERROR;
session
}
}

pub fn upgrade(&mut self) -> SessionIsToBeClosed {
Expand Down Expand Up @@ -240,6 +237,8 @@ impl HttpsSession {
request_id,
);
handshake.frontend_readiness.event = readiness.event;
// Can we remove this? If not why?
// Add e2e test for proto-proxy upgrades
handshake.frontend_readiness.event.insert(Ready::READABLE);

gauge_add!("protocol.proxy.expect", -1);
Expand Down Expand Up @@ -291,8 +290,6 @@ impl HttpsSession {
session: handshake.session,
};

let readiness = handshake.frontend_readiness.clone();

gauge_add!("protocol.tls.handshake", -1);
match alpn {
AlpnProtocols::Http11 => {
Expand Down Expand Up @@ -331,8 +328,7 @@ impl HttpsSession {
}
}

http.frontend_readiness = readiness;
http.frontend_readiness.interest = Ready::READABLE | Ready::HUP | Ready::ERROR;
http.frontend_readiness.event = handshake.frontend_readiness.event;

gauge_add!("protocol.https", 1);
Some(HttpsStateMachine::Http(http))
Expand All @@ -347,8 +343,7 @@ impl HttpsSession {
self.sticky_name.clone(),
);

http.frontend.readiness = readiness;
http.frontend.readiness.interest = Ready::READABLE | Ready::HUP | Ready::ERROR;
http.frontend.readiness.event = handshake.frontend_readiness.event;

gauge_add!("protocol.http2", 1);
Some(HttpsStateMachine::Http2(http))
Expand Down
11 changes: 10 additions & 1 deletion lib/src/protocol/kawa_h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ pub struct Http<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> {
}

impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L> {
/// Instantiate a new HTTP SessionState with:
/// - frontend_interest: READABLE | HUP | ERROR
/// - frontend_event: EMPTY
/// - backend_interest: EMPTY
/// - backend_event: EMPTY
/// Remember to set the events from the previous State!
#[allow(clippy::too_many_arguments)]
pub fn new(
answers: Rc<RefCell<answers::HttpAnswers>>,
Expand Down Expand Up @@ -167,7 +173,10 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
connection_attempts: 0,
container_backend_timeout: TimeoutContainer::new_empty(configured_connect_timeout),
container_frontend_timeout,
frontend_readiness: Readiness::new(),
frontend_readiness: Readiness {
interest: Ready::READABLE | Ready::HUP | Ready::ERROR,
event: Ready::EMPTY,
},
frontend_socket,
frontend_token,
keepalive_count: 0,
Expand Down
10 changes: 8 additions & 2 deletions lib/src/protocol/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ pub struct Pipe<Front: SocketHandler, L: ListenerHandler> {
}

impl<Front: SocketHandler, L: ListenerHandler> Pipe<Front, L> {
/// Instantiate a new Pipe SessionState with:
/// - frontend_interest: READABLE | WRITABLE | HUP | ERROR
/// - frontend_event: EMPTY
/// - backend_interest: READABLE | WRITABLE | HUP | ERROR
/// - backend_event: EMPTY
/// Remember to set the events from the previous State!
#[allow(clippy::too_many_arguments)]
pub fn new(
backend_buffer: Checkout,
Expand Down Expand Up @@ -82,7 +88,7 @@ impl<Front: SocketHandler, L: ListenerHandler> Pipe<Front, L> {
backend_buffer,
backend_id,
backend_readiness: Readiness {
interest: Ready::ALL,
interest: Ready::READABLE | Ready::WRITABLE | Ready::HUP | Ready::ERROR,
event: Ready::EMPTY,
},
backend_socket,
Expand All @@ -94,7 +100,7 @@ impl<Front: SocketHandler, L: ListenerHandler> Pipe<Front, L> {
container_frontend_timeout,
frontend_buffer,
frontend_readiness: Readiness {
interest: Ready::ALL,
interest: Ready::READABLE | Ready::WRITABLE | Ready::HUP | Ready::ERROR,
event: Ready::EMPTY,
},
frontend_status,
Expand Down
5 changes: 4 additions & 1 deletion lib/src/protocol/proxy_protocol/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub struct ExpectProxyProtocol<Front: SocketHandler> {
}

impl<Front: SocketHandler> ExpectProxyProtocol<Front> {
/// Instantiate a new ExpectProxyProtocol SessionState with:
/// - frontend_interest: READABLE | HUP | ERROR
/// - frontend_event: EMPTY
pub fn new(
container_frontend_timeout: TimeoutContainer,
frontend: Front,
Expand All @@ -49,7 +52,7 @@ impl<Front: SocketHandler> ExpectProxyProtocol<Front> {
container_frontend_timeout,
frontend_buffer: [0; 232],
frontend_readiness: Readiness {
interest: Ready::READABLE,
interest: Ready::READABLE | Ready::HUP | Ready::ERROR,
event: Ready::EMPTY,
},
frontend_token,
Expand Down
3 changes: 3 additions & 0 deletions lib/src/protocol/proxy_protocol/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub struct RelayProxyProtocol<Front: SocketHandler> {
}

impl<Front: SocketHandler> RelayProxyProtocol<Front> {
/// Instantiate a new RelayProxyProtocol SessionState with:
/// - frontend_interest: READABLE | HUP | ERROR
/// - frontend_event: EMPTY
pub fn new(
frontend: Front,
frontend_token: Token,
Expand Down
5 changes: 5 additions & 0 deletions lib/src/protocol/proxy_protocol/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ pub struct SendProxyProtocol<Front: SocketHandler> {
}

impl<Front: SocketHandler> SendProxyProtocol<Front> {
/// Instantiate a new SendProxyProtocol SessionState with:
/// - frontend_interest: HUP | ERROR
/// - frontend_event: EMPTY
/// - backend_interest: HUP | ERROR
/// - backend_event: EMPTY
pub fn new(
frontend: Front,
frontend_token: Token,
Expand Down
4 changes: 4 additions & 0 deletions lib/src/protocol/rustls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub struct TlsHandshake {
}

impl TlsHandshake {
/// Instantiate a new TlsHandshake SessionState with:
/// - frontend_interest: READABLE | HUP | ERROR
/// - frontend_event: EMPTY
/// Remember to set the events from the previous State!
pub fn new(
container_frontend_timeout: TimeoutContainer,
session: ServerConnection,
Expand Down

0 comments on commit 211db27

Please sign in to comment.