use std::future::Future;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Duration;
use hyper::server::conn::http1;
use hyper_util::rt::{TokioIo, TokioTimer};
use hyper_util::server::graceful::GracefulShutdown;
use tokio::net::TcpListener;
use tokio::sync::Semaphore;
use crate::service::Svc;
use nitr_core::{Error, Result};
use super::Server;
use super::pool::current_pool;
impl Server {
pub async fn serve(self) -> Result {
self.serve_with_shutdown(shutdown_signal()).await
}
pub async fn serve_with_shutdown(mut self, shutdown: impl Future<Output = ()>) -> Result {
#[cfg(feature = "tls")]
let tls_slot = self.tls.clone();
let listener = match self.listener.take() {
Some(std_listener) => std_listener
.set_nonblocking(true)
.and_then(|()| TcpListener::from_std(std_listener))
.map_err(|err| {
Error::Config(format!("unable to adopt the given listener: {err}"))
})?,
None => TcpListener::bind(self.cfg.listen).await.map_err(|err| {
Error::Config(format!("unable to listen on {}: {err}", self.cfg.listen))
})?,
};
let graceful = GracefulShutdown::new();
let mut shutdown = std::pin::pin!(shutdown);
let (reload_tx, mut reload_rx) = tokio::sync::mpsc::channel::<()>(1);
#[cfg(unix)]
if let Some(mut hangup) = self.reload_signal.take() {
let tx = reload_tx.clone();
tokio::spawn(async move {
while hangup.recv().await.is_some() {
let _ = tx.try_send(());
}
});
}
let _watcher = self
.cfg
.dev_mode
.then(|| crate::watch::spawn(&self.cfg, &[], reload_tx.clone()))
.flatten();
let _reload_tx = reload_tx;
let bound = listener.local_addr().unwrap_or(self.cfg.listen);
let scheme = if self.cfg.tls.enabled {
"https"
} else {
"http"
};
tracing::info!(
"listening on {scheme}://{} with {} Lua state(s)",
bound,
current_pool(&self.pool).size()
);
if let Some(warning) = self.cfg.streams_warning() {
tracing::warn!("{warning}");
}
let max_buf_size = self.cfg.limits.max_header_bytes.max(8 * 1024);
let header_read = match self.cfg.limits.header_read_ms {
0 => None,
ms => Some(Duration::from_millis(ms)),
};
#[cfg(feature = "tls")]
let handshake_deadline = handshake_deadline(&self.cfg.tls, header_read);
let health_state = self.cfg.health.enabled.then(|| {
Arc::new(crate::health::HealthState {
cfg: self.cfg.health.clone(),
ready: self.ready.clone(),
})
});
let probe_guards = crate::health::ProbeGuards {
max_connections: self.cfg.health.max_connections,
header_read,
max_buf_size,
};
let tls_enabled = self.cfg.tls.enabled;
let log_probe_endpoint = move |addr: &str| {
if tls_enabled {
tracing::info!(
"health endpoints on http://{addr} (plaintext; TLS terminates on the \
main listener only)"
);
} else {
tracing::info!("health endpoints on http://{addr}");
}
};
let mut probe_task = None;
let mut probe_stop = None;
let main_health = match (&health_state, self.health_listener.take()) {
(Some(state), Some(std_listener)) => {
let probe_listener = std_listener
.set_nonblocking(true)
.and_then(|()| TcpListener::from_std(std_listener))
.map_err(|err| {
Error::Config(format!("unable to adopt the given health listener: {err}"))
})?;
let addr = probe_listener
.local_addr()
.map(|a| a.to_string())
.unwrap_or_else(|_| "<unknown>".into());
log_probe_endpoint(&addr);
let (stop_tx, stop_rx) = tokio::sync::oneshot::channel();
probe_stop = Some(stop_tx);
probe_task = Some(tokio::spawn(crate::health::serve_probes(
probe_listener,
state.clone(),
probe_guards,
stop_rx,
)));
None
}
(Some(state), None) => match self.cfg.health.bind {
Some(addr) => {
let probe_listener = TcpListener::bind(addr).await.map_err(|err| {
Error::Config(format!("unable to bind [health] bind = {addr}: {err}"))
})?;
let bound = probe_listener
.local_addr()
.map(|a| a.to_string())
.unwrap_or_else(|_| addr.to_string());
log_probe_endpoint(&bound);
let (stop_tx, stop_rx) = tokio::sync::oneshot::channel();
probe_stop = Some(stop_tx);
probe_task = Some(tokio::spawn(crate::health::serve_probes(
probe_listener,
state.clone(),
probe_guards,
stop_rx,
)));
None
}
None => Some(state.clone()),
},
(None, _) => None,
};
let conn_slots = Arc::new(Semaphore::new(self.cfg.limits.max_connections.max(1)));
let http = {
let mut builder = http1::Builder::new();
builder
.timer(TokioTimer::new())
.header_read_timeout(header_read)
.max_buf_size(max_buf_size);
builder
};
let mut draining: Option<std::pin::Pin<Box<tokio::time::Sleep>>> = None;
loop {
tokio::select! {
accepted = async {
#[allow(clippy::expect_used)]
let permit = conn_slots
.clone()
.acquire_owned()
.await
.expect("connection semaphore is never closed");
(permit, listener.accept().await)
} => {
let (permit, accepted) = accepted;
let (stream, peer_addr) = match accepted {
Ok(x) => x,
Err(err) => {
accept_error_backoff("listener", &err).await;
continue;
}
};
let _ = stream.set_nodelay(true);
let svc = Svc::new(
self.pool.clone(),
self.streams.clone(),
self.protection.clone(),
main_health.clone(),
peer_addr,
tls_enabled,
);
let http = http.clone();
let watcher = graceful.watcher();
#[cfg(feature = "tls")]
let acceptor = current_acceptor(&tls_slot);
tokio::spawn(async move {
let _permit = permit;
#[cfg(feature = "tls")]
if let Some(acceptor) = acceptor {
let Some(stream) =
tls_handshake(&acceptor, stream, handshake_deadline, peer_addr)
.await
else {
return;
};
let conn = http.serve_connection(TokioIo::new(stream), svc);
if let Err(err) = watcher.watch(conn).await {
tracing::debug!(
"TLS connection from {peer_addr} ended with: {err}"
);
}
return;
}
let conn = http.serve_connection(TokioIo::new(stream), svc);
if let Err(err) = watcher.watch(conn).await {
tracing::error!("error serving connection: {err}");
}
});
}
Some(()) = reload_rx.recv() => self.reload(),
() = &mut shutdown, if draining.is_none() => {
self.ready.store(false, Ordering::Relaxed);
let delay = self.cfg.readiness_delay();
if delay.is_zero() {
break;
}
tracing::info!("draining: readiness answers 503, still serving for {delay:?}");
draining = Some(Box::pin(tokio::time::sleep(delay)));
}
() = async {
match draining.as_mut() {
Some(delay) => delay.await,
None => std::future::pending().await,
}
}, if draining.is_some() => break,
}
}
drop(listener);
let grace = self.cfg.shutdown.grace();
let total = self.cfg.shutdown.total_grace();
tracing::info!(
"draining: waiting up to {grace:?} for in-flight requests \
({stream_grace:?} more for streaming bodies)",
stream_grace = total.saturating_sub(grace),
);
let deadline = drain_deadline(&self.streams, self.max_streams, grace, total);
let drained = tokio::select! {
_ = graceful.shutdown() => true,
_ = deadline => false,
};
if let Some(mut task) = probe_task {
if let Some(stop) = probe_stop.take() {
let _ = stop.send(());
}
if tokio::time::timeout(Duration::from_secs(1), &mut task)
.await
.is_err()
{
tracing::warn!("probe listener did not drain within 1s, abandoning the drain");
task.abort();
}
}
if drained {
tracing::info!("drained cleanly, shutting down");
Ok(())
} else {
tracing::warn!("drain deadline of {total:?} expired, aborting remaining connections");
Err(Error::ShutdownTimeout)
}
}
}
const ACCEPT_ERROR_BACKOFF: Duration = Duration::from_millis(50);
pub(crate) async fn accept_error_backoff(listener: &str, err: &std::io::Error) {
use std::io::ErrorKind;
match err.kind() {
ErrorKind::ConnectionAborted | ErrorKind::ConnectionReset | ErrorKind::Interrupted => {
tracing::debug!("{listener}: client gone before accept: {err}");
}
_ => {
tracing::warn!("{listener}: failed to accept connection: {err}");
tokio::time::sleep(ACCEPT_ERROR_BACKOFF).await;
}
}
}
#[cfg(feature = "tls")]
async fn tls_handshake(
acceptor: &tokio_rustls::TlsAcceptor,
stream: tokio::net::TcpStream,
deadline: Duration,
peer: std::net::SocketAddr,
) -> Option<tokio_rustls::server::TlsStream<tokio::net::TcpStream>> {
let handshake = match tokio::time::timeout(deadline, acceptor.accept(stream)).await {
Ok(handshake) => handshake,
Err(_) => {
tracing::debug!("TLS handshake from {peer} did not finish within {deadline:?}");
return None;
}
};
match handshake {
Ok(stream) => Some(stream),
Err(err) => {
tracing::debug!("TLS handshake from {peer} failed: {err}");
None
}
}
}
#[cfg(feature = "tls")]
fn handshake_deadline(tls: &crate::config::TlsConfig, header_read: Option<Duration>) -> Duration {
const DEFAULT_HANDSHAKE: Duration = Duration::from_secs(10);
match tls.handshake_ms {
Some(ms) if ms > 0 => Duration::from_millis(ms),
_ => header_read.map_or(DEFAULT_HANDSHAKE, |d| d.min(DEFAULT_HANDSHAKE)),
}
}
#[cfg(feature = "tls")]
fn current_acceptor(
slot: &Arc<std::sync::RwLock<Option<tokio_rustls::TlsAcceptor>>>,
) -> Option<tokio_rustls::TlsAcceptor> {
slot.read()
.map(|s| s.clone())
.unwrap_or_else(|e| e.into_inner().clone())
}
async fn drain_deadline(streams: &Semaphore, max_streams: usize, grace: Duration, total: Duration) {
tokio::time::sleep(grace).await;
if streams.available_permits() < max_streams {
tracing::info!("streaming bodies still live, extending the drain to {total:?}");
tokio::time::sleep(total.saturating_sub(grace)).await;
}
}
async fn shutdown_signal() {
#[cfg(unix)]
{
use tokio::signal::unix::{SignalKind, signal};
let mut term = match signal(SignalKind::terminate()) {
Ok(sig) => sig,
Err(err) => {
tracing::warn!("failed to install the SIGTERM handler: {err}");
let _ = tokio::signal::ctrl_c().await;
return;
}
};
tokio::select! {
_ = term.recv() => tracing::info!("received SIGTERM"),
_ = tokio::signal::ctrl_c() => tracing::info!("received SIGINT"),
}
}
#[cfg(not(unix))]
{
let _ = tokio::signal::ctrl_c().await;
tracing::info!("received ctrl-c");
}
}