Skip to content

Support backends listening on UNIX domain sockets #152

Closed

Description

@KellerFuchs

UNIX sockets are pretty commonly used for backend services which run on the same server, especially when security is a concern, as they make it easy not to give the backend network access and control which services/users may connect to the backend.

As for #151, I would happily contribute the code for this, assuming it is OK and assuming I manage to find my way around the codebase. :)

Activity

  1. Geal commented on Apr 16, 2017

    @Geal
    Member

    this is doable, but requires changes in a lot of parts of the system.

    Backend instances are defined here: https://github.com/sozu-proxy/sozu/blob/master/lib/src/messages.rs#L42
    This would probably become an enum and require changes in the other crates as well, to support the new option in configuration.

    The part of the code handling connection to the backend is here:

    sozu/lib/src/network/mod.rs

    Lines 186 to 252 in d7b2dbe

    impl Backend {
    pub fn new(addr: SocketAddr) -> Backend {
    Backend {
    address: addr,
    status: BackendStatus::Normal,
    active_connections: 0,
    failures: 0,
    }
    }
    pub fn set_closing(&mut self) {
    self.status = BackendStatus::Closing;
    }
    pub fn can_open(&self, max_failures: usize) -> bool {
    self.status == BackendStatus::Normal && self.failures < max_failures
    }
    pub fn inc_connections(&mut self) -> Option<usize> {
    if self.status == BackendStatus::Normal {
    self.active_connections += 1;
    Some(self.active_connections)
    } else {
    None
    }
    }
    pub fn dec_connections(&mut self) -> Option<usize> {
    if self.active_connections == 0 {
    self.status = BackendStatus::Closed;
    return None;
    }
    match self.status {
    BackendStatus::Normal => {
    self.active_connections -= 1;
    Some(self.active_connections)
    }
    BackendStatus::Closed => None,
    BackendStatus::Closing => {
    self.active_connections -= 1;
    if self.active_connections == 0 {
    self.status = BackendStatus::Closed;
    None
    } else {
    Some(self.active_connections)
    }
    },
    }
    }
    pub fn try_connect(&mut self, max_failures: usize) -> Result<mio::tcp::TcpStream, ConnectionError> {
    if self.failures >= max_failures || self.status == BackendStatus::Closing || self.status == BackendStatus::Closed {
    return Err(ConnectionError::NoBackendAvailable);
    }
    //FIXME: what happens if the connect() call fails with EINPROGRESS?
    let conn = mio::tcp::TcpStream::connect(&self.address).map_err(|_| ConnectionError::NoBackendAvailable);
    if conn.is_ok() {
    self.inc_connections();
    } else {
    self.failures += 1;
    }
    conn
    }
    }

    You can use mio-uds to add Unix socket support.

    To sum up, it's doable, but you'll spend a lot more time on updating the configuration code than on actual network code.
    I'll ask that you wait a bit for me to finish #144, since I'm changing a lot of parts of the configuration handling code.

  2. KellerFuchs commented on Apr 17, 2017

    @KellerFuchs
    Author

    Sure, thanks.

  3. added this to the milestone on Jul 27, 2017
  4. hongquan commented on Jan 18, 2020

    @hongquan

    Please also support Linux abstract namespace socket (which is like Unix domain socket but doesn't create a file in filesystem).

  5. modified the milestones: , 0.14 on Jul 29, 2021
  6. modified the milestones: v0.14.0, next on Feb 18, 2022
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