Skip to content

simplify the interaction between ServerConfiguration and Client #444

Closed

Description

@Geal

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:

sozu/lib/src/network/proxy.rs

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:

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
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

  1. Geal commented on Apr 17, 2018

    @Geal
    MemberAuthor

    hmmm, apparently, the *mut pointer will not be correct if we take() the Option<ServerConfiguration>: https://play.rust-lang.org/?gist=ba6513eeb7cf4e0439a111a5d05169ca&version=stable

    Alternative solution: have the ServerConfiguration in a Rc<RefCell<>> and have the Client hold this reference. The client already have a reference to the memory pool, this would be the same approach:

    pool: Weak<RefCell<Pool<BufferQueue>>>,

    and we could pretty much guarantee that it is safe.

    That would mean that the event loop would hold Option<Rc<RefCell<ServerConfiguration>>> instead of Option<ServerConfiguration>, so this would have the following effect:

    • one more level of indirection whenever we receive a configuration message (not a big problem)
    • one more level of indirection whenever we call accept or accept_flush
    • cloning the Rc container every time we're about to call accept
    • calls to connect_to_backend and close_backend would be done from inside the Client
  2. added this to the 0.14 milestone on Jul 29, 2021
  3. modified the milestones: v0.14.0, v0.18.0 on Feb 18, 2022
  4. FlorentinDUBOIS commented on Sep 29, 2026

    @FlorentinDUBOIS
    Collaborator

    ServerConfiguration no 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 the L7Proxy / ProxyConfiguration traits in lib/src/lib.rs — the alternative proposed in this thread. Closing as completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions