Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harmonize handling of add_listener #1087

Merged
merged 4 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions lib/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
};

use mio::{
net::{TcpListener, TcpStream},
net::{TcpListener as MioTcpListener, TcpStream},
unix::SourceFd,
Interest, Registry, Token,
};
Expand Down Expand Up @@ -389,7 +389,7 @@
answers: Rc<RefCell<HttpAnswers>>,
config: HttpListenerConfig,
fronts: Router,
listener: Option<TcpListener>,
listener: Option<MioTcpListener>,
tags: BTreeMap<String, CachedTags>,
token: Token,
}
Expand Down Expand Up @@ -535,7 +535,7 @@
pub fn activate_listener(
&self,
addr: &SocketAddr,
tcp_listener: Option<TcpListener>,
tcp_listener: Option<MioTcpListener>,
) -> Result<Token, ProxyError> {
let listener = self
.listeners
Expand All @@ -552,7 +552,7 @@
})
}

pub fn give_back_listeners(&mut self) -> Vec<(SocketAddr, TcpListener)> {
pub fn give_back_listeners(&mut self) -> Vec<(SocketAddr, MioTcpListener)> {
self.listeners
.iter()
.filter_map(|(_, listener)| {
Expand All @@ -566,18 +566,24 @@
.collect()
}

pub fn give_back_listener(&mut self, address: SocketAddr) -> Option<(Token, TcpListener)> {
self.listeners
pub fn give_back_listener(
&mut self,
address: SocketAddr,
) -> Result<(Token, MioTcpListener), ProxyError> {
let listener = self
.listeners
.values()
.find(|listener| listener.borrow().address == address)
.and_then(|listener| {
let mut owned = listener.borrow_mut();
.ok_or(ProxyError::NoListenerFound(address.clone()))?;

owned
.listener
.take()
.map(|listener| (owned.token, listener))
})
let mut owned = listener.borrow_mut();

let taken_listener = owned
.listener
.take()
.ok_or(ProxyError::UnactivatedListener)?;

Ok((owned.token, taken_listener))
}

pub fn add_cluster(&mut self, cluster: Cluster) -> Result<(), ProxyError> {
Expand Down Expand Up @@ -732,7 +738,7 @@
pub fn activate(
&mut self,
registry: &Registry,
tcp_listener: Option<TcpListener>,
tcp_listener: Option<MioTcpListener>,
) -> Result<Token, ListenerError> {
if self.active {
return Ok(self.token);
Expand Down Expand Up @@ -845,7 +851,7 @@
);
self.logging(logging_filter)
}
other_command => {

Check warning on line 854 in lib/src/http.rs

View workflow job for this annotation

GitHub Actions / Build Sozu 🦀

unused variable: `other_command`

Check warning on line 854 in lib/src/http.rs

View workflow job for this annotation

GitHub Actions / Build Sozu 🦀

unused variable: `other_command`
debug!(
"{} unsupported message for HTTP proxy, ignoring: {:?}",
request.id, other_command
Expand Down
38 changes: 22 additions & 16 deletions lib/src/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,15 +793,19 @@ impl HttpsProxy {
}
}

pub fn add_listener(&mut self, config: HttpsListenerConfig, token: Token) -> Option<Token> {
pub fn add_listener(
&mut self,
config: HttpsListenerConfig,
token: Token,
) -> Result<Token, ProxyError> {
match self.listeners.entry(token) {
Entry::Vacant(entry) => {
entry.insert(Rc::new(RefCell::new(
HttpsListener::try_new(config, token).ok()?,
)));
Some(token)
let https_listener =
HttpsListener::try_new(config, token).map_err(ProxyError::AddListener)?;
entry.insert(Rc::new(RefCell::new(https_listener)));
Ok(token)
}
_ => None,
_ => Err(ProxyError::ListenerAlreadyPresent),
}
}

Expand Down Expand Up @@ -981,22 +985,24 @@ impl HttpsProxy {
.collect()
}

// TODO: return <Result, ProxyError>
pub fn give_back_listener(
&mut self,
address: StdSocketAddr,
) -> Option<(Token, MioTcpListener)> {
self.listeners
) -> Result<(Token, MioTcpListener), ProxyError> {
let listener = self
.listeners
.values()
.find(|listener| listener.borrow().address == address)
.and_then(|listener| {
let mut owned = listener.borrow_mut();
.ok_or(ProxyError::NoListenerFound(address.clone()))?;

owned
.listener
.take()
.map(|listener| (owned.token, listener))
})
let mut owned = listener.borrow_mut();

let taken_listener = owned
.listener
.take()
.ok_or(ProxyError::UnactivatedListener)?;

Ok((owned.token, taken_listener))
}

pub fn add_cluster(
Expand Down
9 changes: 8 additions & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ use hex::FromHexError;
use mio::{net::TcpStream, Interest, Token};
use protocol::http::parser::Method;
use router::RouterError;
use socket::ServerBindError;
use time::{Duration, Instant};
use tls::CertificateResolverError;

Expand Down Expand Up @@ -671,7 +672,7 @@ pub enum ProxyError {
NoListenerFound(SocketAddr),
#[error("a listener is already present for this token")]
ListenerAlreadyPresent,
#[error("could not create add listener: {0}")]
#[error("could not add listener: {0}")]
AddListener(ListenerError),
#[error("failed to activate listener with address {address:?}: {listener_error}")]
ListenerActivation {
Expand Down Expand Up @@ -699,6 +700,12 @@ pub enum ProxyError {
UnsupportedMessage,
#[error("failed to acquire the lock, {0}")]
Lock(String),
#[error("could not bind to socket {0:?}: {1}")]
BindToSocket(SocketAddr, ServerBindError),
#[error("error registering socket of listener: {0}")]
RegisterListener(std::io::Error),
#[error("the listener is not activated")]
UnactivatedListener,
}

use self::server::ListenToken;
Expand Down
Loading
Loading