use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, RwLock};
use nitr_core::ModuleFn;
use tokio::sync::Semaphore;
use crate::config::Config;
use crate::protect::Protection;
use nitr_core::RuntimePool;
use nitr_std::Builtins;
type SetupFn = Box<dyn Fn(&mlua::Lua) -> mlua::Result<()> + Send + Sync>;
type Module = (String, Arc<ModuleFn>);
pub struct Server {
cfg: Config,
builtins: Builtins,
setup_fns: Arc<Vec<SetupFn>>,
modules: Arc<Vec<Module>>,
pool: Arc<RwLock<Arc<RuntimePool>>>,
streams: Arc<Semaphore>,
max_streams: usize,
protection: Arc<Protection>,
listener: Option<std::net::TcpListener>,
health_listener: Option<std::net::TcpListener>,
ready: Arc<AtomicBool>,
#[cfg(unix)]
reload_signal: Option<tokio::signal::unix::Signal>,
cache: Option<nitr_std::Cache>,
cfg_snapshot: Option<serde_json::Value>,
reloading: Arc<std::sync::atomic::AtomicU8>,
#[cfg(feature = "tls")]
tls: Arc<RwLock<Option<tokio_rustls::TlsAcceptor>>>,
#[cfg(feature = "openapi")]
docs: crate::openapi::docs::DocsSlot,
}
mod builder;
mod pool;
mod serve;
pub use builder::ServerBuilder;
use pool::{build_runtimes, new_pool};
pub(crate) use pool::{current_pool, input_env};
pub(crate) use serve::accept_error_backoff;
impl Server {
pub fn builder() -> ServerBuilder {
ServerBuilder::default()
}
pub fn pool(&self) -> Arc<RuntimePool> {
current_pool(&self.pool)
}
pub fn is_ready(&self) -> bool {
self.ready.load(Ordering::Relaxed)
}
#[cfg(feature = "openapi")]
pub fn openapi_json(&self) -> Option<bytes::Bytes> {
self.docs
.read()
.ok()
.and_then(|slot| slot.as_ref().map(|d| d.spec().clone()))
}
#[cfg(feature = "swagger")]
pub fn openapi_site(&self) -> Option<Vec<(String, bytes::Bytes)>> {
let docs = self.docs.read().ok()?.clone()?;
docs.static_site(&self.cfg.swagger).ok()
}
pub fn cfg_snapshot(&self) -> Option<&serde_json::Value> {
self.cfg_snapshot.as_ref()
}
pub fn test_client(&self) -> crate::testing::TestClient {
crate::testing::TestClient::new(
self.pool.clone(),
self.streams.clone(),
self.protection.clone(),
self.cfg.tls.enabled,
)
}
fn reload(&self) {
#[cfg(feature = "tls")]
self.reload_tls();
if self
.reloading
.compare_exchange(
RELOAD_IDLE,
RELOAD_RUNNING,
Ordering::AcqRel,
Ordering::Acquire,
)
.is_err()
{
self.reloading.store(RELOAD_PENDING, Ordering::Release);
tracing::info!("reload requested while one is in progress; it will run again");
return;
}
tracing::info!("reload requested: rebuilding the runtime pool");
let cfg = self.cfg.clone();
let builtins = self.builtins;
let setup_fns = self.setup_fns.clone();
let modules = self.modules.clone();
let cache = self.cache.clone();
let pool = self.pool.clone();
#[cfg(feature = "openapi")]
let docs = self.docs.clone();
let state = self.reloading.clone();
tokio::spawn(async move {
let _reset = ReloadReset(state.clone());
loop {
let rebuild = std::panic::AssertUnwindSafe(rebuild_pool(
&cfg,
builtins,
&setup_fns,
&modules,
&cache,
&pool,
#[cfg(feature = "openapi")]
&docs,
));
if let Err(payload) = futures_util::FutureExt::catch_unwind(rebuild).await {
let message = payload
.downcast_ref::<&str>()
.map(|s| s.to_string())
.or_else(|| payload.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "non-string panic payload".into());
tracing::error!("reload failed: the rebuild panicked: {message}");
}
if state
.compare_exchange(
RELOAD_RUNNING,
RELOAD_IDLE,
Ordering::AcqRel,
Ordering::Acquire,
)
.is_ok()
{
break;
}
state.store(RELOAD_RUNNING, Ordering::Release);
tracing::info!("running the reload requested during the last rebuild");
}
});
}
#[cfg(feature = "tls")]
fn reload_tls(&self) {
if !self.cfg.tls.enabled {
tracing::debug!("reload: [tls] is disabled, no certificate to re-read");
return;
}
match crate::tls::load(&self.cfg.tls) {
Ok(loaded) => {
let acceptor = tokio_rustls::TlsAcceptor::from(loaded.config);
match self.tls.write() {
Ok(mut slot) => {
*slot = Some(acceptor);
tracing::info!(
"reload complete: TLS material re-read ({} certificate(s))",
loaded.certs
);
}
Err(_) => tracing::error!("TLS reload failed: the acceptor lock is poisoned"),
}
}
Err(err) => {
tracing::warn!("TLS reload failed, keeping the current certificate: {err}");
}
}
}
}
const RELOAD_IDLE: u8 = 0;
const RELOAD_RUNNING: u8 = 1;
const RELOAD_PENDING: u8 = 2;
struct ReloadReset(Arc<std::sync::atomic::AtomicU8>);
impl Drop for ReloadReset {
fn drop(&mut self) {
self.0.store(RELOAD_IDLE, Ordering::Release);
}
}
async fn rebuild_pool(
cfg: &Config,
builtins: Builtins,
setup_fns: &Arc<Vec<SetupFn>>,
modules: &Arc<Vec<Module>>,
cache: &Option<nitr_std::Cache>,
pool: &Arc<RwLock<Arc<RuntimePool>>>,
#[cfg(feature = "openapi")] docs: &crate::openapi::docs::DocsSlot,
) {
match build_runtimes(cfg, builtins, setup_fns, modules, cache.as_ref()).await {
Ok(built) => {
#[cfg(feature = "openapi")]
let fresh_docs = match pool::build_docs(cfg, &built.runtimes) {
Ok(docs) => docs,
Err(err) => {
tracing::error!("reload failed, keeping the current pool: {err}");
return;
}
};
let fresh = Arc::new(new_pool(
built,
cfg,
builtins,
setup_fns,
modules,
cache.clone(),
));
match pool.write() {
Ok(mut pool) => {
*pool = fresh;
#[cfg(feature = "openapi")]
match docs.write() {
Ok(mut slot) => *slot = Some(fresh_docs),
Err(_) => tracing::error!("reload: the docs lock is poisoned"),
}
tracing::info!("reload complete: new runtime pool is live");
}
Err(_) => tracing::error!("reload failed: pool lock is poisoned"),
}
}
Err(err) => {
tracing::error!("reload failed, keeping the current pool: {err}");
}
}
}
impl std::fmt::Debug for Server {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Server")
.field("cfg", &self.cfg)
.field("builtins", &self.builtins)
.finish_non_exhaustive()
}
}