Repository navigation
Description
Right now, when a protocol implementation must connect to a backend server, it returns a ConnectBackend message which is transmitted up to the proxy event loop, which will call the ProxyConfiguration::connect_to_backend method on the ServerConfiguration.
The issue is that the ServerConfiguration is expecting a corresponding client (like, a https_openssl::ServerConfiguration is expecting a https_openssl::TlsClient), which leads to this mess:
Lines 1124 to 1213 in eaff546
| #[cfg(not(feature = "use-openssl"))] | |
| /// this trait is used to work around the fact that we need to transform | |
| /// the clients (that are all stored as a ProxyClient in the slab) back | |
| /// to their original type to call the `connect_to_backend` method of | |
| /// their corresponding `ServerConfiguration`. | |
| /// If we find a way to make `connect_to_backend` work wothout transforming | |
| /// back to the type (example: storing a reference to the configuration | |
| /// in the client and making `connect_to_backend` a method of `ProxyClient`?), | |
| /// we'll be able to remove all those ugly casts and panics | |
| pub trait ProxyClientCast: ProxyClient { | |
| fn as_tcp(&mut self) -> &mut tcp::Client; | |
| fn as_http(&mut self) -> &mut http::Client; | |
| fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient; | |
| } | |
| #[cfg(feature = "use-openssl")] | |
| pub trait ProxyClientCast: ProxyClient { | |
| fn as_tcp(&mut self) -> &mut tcp::Client; | |
| fn as_http(&mut self) -> &mut http::Client; | |
| fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient; | |
| fn as_https_openssl(&mut self) -> &mut https_openssl::TlsClient; | |
| } | |
| #[cfg(not(feature = "use-openssl"))] | |
| impl ProxyClientCast for ListenClient { | |
| fn as_http(&mut self) -> &mut http::Client { panic!() } | |
| fn as_tcp(&mut self) -> &mut tcp::Client { panic!() } | |
| fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() } | |
| } | |
| #[cfg(feature = "use-openssl")] | |
| impl ProxyClientCast for ListenClient { | |
| fn as_http(&mut self) -> &mut http::Client { panic!() } | |
| fn as_tcp(&mut self) -> &mut tcp::Client { panic!() } | |
| fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() } | |
| fn as_https_openssl(&mut self) -> &mut https_openssl::TlsClient { panic!() } | |
| } | |
| #[cfg(not(feature = "use-openssl"))] | |
| impl ProxyClientCast for http::Client { | |
| fn as_http(&mut self) -> &mut http::Client { self } | |
| fn as_tcp(&mut self) -> &mut tcp::Client { panic!() } | |
| fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() } | |
| } | |
| #[cfg(feature = "use-openssl")] | |
| impl ProxyClientCast for http::Client { | |
| fn as_http(&mut self) -> &mut http::Client { self } | |
| fn as_tcp(&mut self) -> &mut tcp::Client { panic!() } | |
| fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() } | |
| fn as_https_openssl(&mut self) -> &mut https_openssl::TlsClient { panic!() } | |
| } | |
| #[cfg(not(feature = "use-openssl"))] | |
| impl ProxyClientCast for tcp::Client { | |
| fn as_http(&mut self) -> &mut http::Client { panic!() } | |
| fn as_tcp(&mut self) -> &mut tcp::Client { self } | |
| fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() } | |
| } | |
| #[cfg(feature = "use-openssl")] | |
| impl ProxyClientCast for tcp::Client { | |
| fn as_http(&mut self) -> &mut http::Client { panic!() } | |
| fn as_tcp(&mut self) -> &mut tcp::Client { self } | |
| fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() } | |
| fn as_https_openssl(&mut self) -> &mut https_openssl::TlsClient { panic!() } | |
| } | |
| #[cfg(not(feature = "use-openssl"))] | |
| impl ProxyClientCast for https_rustls::client::TlsClient { | |
| fn as_http(&mut self) -> &mut http::Client { panic!() } | |
| fn as_tcp(&mut self) -> &mut tcp::Client { panic!() } | |
| fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { self } | |
| } | |
| #[cfg(feature = "use-openssl")] | |
| impl ProxyClientCast for https_rustls::client::TlsClient { | |
| fn as_http(&mut self) -> &mut http::Client { panic!() } | |
| fn as_tcp(&mut self) -> &mut tcp::Client { panic!() } | |
| fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { self } | |
| fn as_https_openssl(&mut self) -> &mut https_openssl::TlsClient { panic!() } | |
| } | |
| #[cfg(feature = "use-openssl")] | |
| impl ProxyClientCast for https_openssl::TlsClient { | |
| fn as_http(&mut self) -> &mut http::Client { panic!() } | |
| fn as_tcp(&mut self) -> &mut tcp::Client { panic!() } | |
| fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() } | |
| fn as_https_openssl(&mut self) -> &mut https_openssl::TlsClient { self } | |
| } |
Proposal: since we know the clients will never outlive their corresponding configuration and the configuration won't move (assuming Option::take does not move the value), let's have the client hold a *mut ServerConfiguration and have a connect_to_backend method in the client that would call frontend_from_request, backend_from_app_id, and other methods of the configuration. Downside is that those calls have to be wrapped with unsafe{}, but as I said, if we can enforce the lifetime and avoid moving the configuration object, it works correctly.
That means we could call directly the client's connect_to_backend method here:
Lines 753 to 781 in eaff546
| Protocol::TCP => { | |
| if let Some(mut tcp) = self.tcp.take() { | |
| let mut b = cl2.borrow_mut(); | |
| let client: &mut tcp::Client = b.as_tcp() ; | |
| let r = tcp.connect_to_backend(&mut self.poll, client, back_token); | |
| self.tcp = Some(tcp); | |
| r | |
| } else { | |
| Err(ConnectionError::HostNotFound) | |
| } | |
| }, | |
| Protocol::HTTP => { | |
| if let Some(mut http) = self.http.take() { | |
| let mut b = cl2.borrow_mut(); | |
| let client: &mut http::Client = b.as_http() ; | |
| let r = http.connect_to_backend(&mut self.poll, client, back_token); | |
| self.http = Some(http); | |
| r | |
| } else { | |
| Err(ConnectionError::HostNotFound) | |
| } | |
| }, | |
| Protocol::HTTPS => { | |
| if let Some(mut https) = self.https.take() { | |
| let r = https.connect_to_backend(&mut self.poll, cl2, back_token); | |
| self.https = Some(https); | |
| r | |
| } else { | |
| Err(ConnectionError::HostNotFound) |
and simplify further the
ProxyConfiguration trait Lines 89 to 98 in eaff546
| pub trait ProxyConfiguration<Client> { | |
| fn connect_to_backend(&mut self, event_loop: &mut Poll, client: &mut Client, | |
| back_token: Token) ->Result<BackendConnectAction,ConnectionError>; | |
| fn notify(&mut self, event_loop: &mut Poll, message: OrderMessage) -> OrderMessageAnswer; | |
| fn accept(&mut self, token: ListenToken, event_loop: &mut Poll, client_token: Token) | |
| -> Result<(Rc<RefCell<Client>>, bool), AcceptError>; | |
| fn accept_flush(&mut self) -> usize; | |
| fn close_backend(&mut self, app_id: String, addr: &SocketAddr); | |
| fn listen_port_state(&self, port: &u16) -> ListenPortState; | |
| } |
Activity
Geal commentedon Apr 17, 2018
hmmm, apparently, the *mut pointer will not be correct if we
take()theOption<ServerConfiguration>: https://play.rust-lang.org/?gist=ba6513eeb7cf4e0439a111a5d05169ca&version=stableAlternative solution: have the
ServerConfigurationin aRc<RefCell<>>and have theClienthold this reference. The client already have a reference to the memory pool, this would be the same approach:sozu/lib/src/network/http.rs
Line 63 in eaff546
and we could pretty much guarantee that it is safe.
That would mean that the event loop would hold
Option<Rc<RefCell<ServerConfiguration>>>instead ofOption<ServerConfiguration>, so this would have the following effect:acceptoraccept_flushRccontainer every time we're about to callacceptconnect_to_backendandclose_backendwould be done from inside theClientFlorentinDUBOIS commentedon Sep 29, 2026
ServerConfigurationno longer exists in the codebase; sessions hold the proxy directly (Rc<RefCell<HttpProxy>>in lib/src/http.rs,Rc<RefCell<HttpsProxy>>in lib/src/https.rs), and backend connection goes through theL7Proxy/ProxyConfigurationtraits in lib/src/lib.rs — the alternative proposed in this thread. Closing as completed.