use std::{
net::{SocketAddr, TcpListener, TcpStream},
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
thread,
time::Duration,
};
use indicatrix_vault::db::sqlite::Database;
use crate::{cli::ServeArgs, enroll};
#[cfg(feature = "worker")]
use crate::cli::ComputeMode;
mod connection;
pub mod library;
#[cfg(feature = "worker")]
mod tilt;
mod tls;
#[cfg(all(test, feature = "worker"))]
mod tests;
use connection::report_connection_result;
use tls::{Auth, Transport, accept_tls, build_transport};
#[cfg(not(feature = "worker"))]
pub use connection::handle_connection;
#[cfg(feature = "worker")]
pub use connection::{handle_connection, handle_connection_with_gpu};
fn open_library_database(path: &std::path::Path) -> Result<Database, String> {
let path_str = path
.to_str()
.ok_or_else(|| format!("--db {}: path is not valid Unicode", path.display()))?;
Database::open_read_only(path_str).map_err(|e| {
format!(
"failed to open the design-library database read-only at {}: {e:#}",
path.display()
)
})
}
#[must_use]
fn resolve_library_db_path(args: &ServeArgs) -> std::path::PathBuf {
args.db
.clone()
.unwrap_or_else(|| std::path::PathBuf::from(indicatrix_vault::db::sqlite::DEFAULT_DB_FILE))
}
const TCP_KEEPALIVE_IDLE: Duration = Duration::from_secs(30);
const TCP_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(10);
fn tune_accepted_socket(stream: &TcpStream, peer: Option<SocketAddr>) {
if let Err(e) = stream.set_nodelay(true) {
tracing::debug!("connection {peer:?}: failed to set TCP_NODELAY: {e}");
}
let sock_ref = socket2::SockRef::from(stream);
let keepalive = socket2::TcpKeepalive::new()
.with_time(TCP_KEEPALIVE_IDLE)
.with_interval(TCP_KEEPALIVE_INTERVAL);
if let Err(e) = sock_ref.set_tcp_keepalive(&keepalive) {
tracing::debug!("connection {peer:?}: failed to set TCP keepalive: {e}");
}
}
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(20);
fn apply_handshake_timeout(stream: &TcpStream, peer: Option<SocketAddr>) {
if let Err(e) = stream.set_read_timeout(Some(HANDSHAKE_TIMEOUT)) {
tracing::debug!("connection {peer:?}: failed to set handshake read timeout: {e}");
}
if let Err(e) = stream.set_write_timeout(Some(HANDSHAKE_TIMEOUT)) {
tracing::debug!("connection {peer:?}: failed to set handshake write timeout: {e}");
}
}
#[derive(Debug)]
struct ConnectionLimiter {
active: Arc<AtomicUsize>,
max: usize,
}
impl ConnectionLimiter {
fn new(max: usize) -> Self {
Self {
active: Arc::new(AtomicUsize::new(0)),
max,
}
}
fn try_acquire(&self) -> Result<ConnectionSlot, usize> {
let active = self.active.fetch_add(1, Ordering::SeqCst) + 1;
if active > self.max {
self.active.fetch_sub(1, Ordering::SeqCst);
Err(active - 1)
} else {
Ok(ConnectionSlot {
active: Arc::clone(&self.active),
})
}
}
}
#[derive(Debug)]
struct ConnectionSlot {
active: Arc<AtomicUsize>,
}
impl Drop for ConnectionSlot {
fn drop(&mut self) {
self.active.fetch_sub(1, Ordering::SeqCst);
}
}
pub fn run(args: &ServeArgs) -> Result<(), String> {
let bind_addr: SocketAddr = args
.bind
.parse()
.map_err(|e| format!("invalid --bind address {:?}: {e}", args.bind))?;
if !bind_addr.ip().is_loopback() && !args.allow_remote {
return Err(format!(
"refusing to bind non-loopback address {bind_addr} without --allow-remote -- exposing this worker \
beyond localhost must be explicit (see --help)"
));
}
let transport = build_transport(args, bind_addr)?;
let db_path = resolve_library_db_path(args);
drop(open_library_database(&db_path)?);
let limiter = ConnectionLimiter::new(args.max_connections);
#[cfg(feature = "worker")]
let gpu = Arc::new(match args.compute_mode {
ComputeMode::OnlyCpu => indicatrix::renderer::gpu_backend::GpuBackend::disabled(),
ComputeMode::Hybrid | ComputeMode::OnlyGpu => {
indicatrix::renderer::gpu_backend::GpuBackend::acquire()
}
});
let listener =
TcpListener::bind(bind_addr).map_err(|e| format!("failed to bind {bind_addr}: {e}"))?;
tracing::info!(
"indicatrix-worker serve: listening on {bind_addr} ({}, {})",
match &transport {
Transport::Tls {
auth: Auth::Allowlist(_),
..
} => "mutual TLS, allowlist enforced",
Transport::Tls {
auth: Auth::AnyCaSignedClient,
..
} => "mutual TLS, ANY CA-signed client trusted",
Transport::Insecure => "PLAINTEXT, no TLS",
},
capability_summary(args),
);
enroll::maybe_start_from_serve_args(args, bind_addr)?;
for incoming in listener.incoming() {
match incoming {
Ok(stream) => {
let peer = stream.peer_addr().ok();
tune_accepted_socket(&stream, peer);
apply_handshake_timeout(&stream, peer);
let slot = limiter.try_acquire();
#[cfg(feature = "worker")]
spawn_connection_handler(
stream,
peer,
&transport,
slot,
&ConnectionContext {
db_path: &db_path,
threads: args.threads,
gpu: &gpu,
compute_mode: args.compute_mode,
max_connections: args.max_connections,
},
);
#[cfg(not(feature = "worker"))]
spawn_connection_handler(
stream,
peer,
&transport,
&db_path,
slot,
args.max_connections,
);
}
Err(e) => tracing::warn!("accept error: {e}"),
}
}
Ok(())
}
#[cfg(feature = "worker")]
struct ConnectionContext<'a> {
db_path: &'a std::path::Path,
threads: usize,
gpu: &'a Arc<indicatrix::renderer::gpu_backend::GpuBackend>,
compute_mode: ComputeMode,
max_connections: usize,
}
#[cfg(feature = "worker")]
fn spawn_connection_handler(
stream: TcpStream,
peer: Option<SocketAddr>,
transport: &Transport,
slot: Result<ConnectionSlot, usize>,
ctx: &ConnectionContext<'_>,
) {
let db_path = ctx.db_path.to_path_buf();
let gpu = Arc::clone(ctx.gpu);
let threads = ctx.threads;
let compute_mode = ctx.compute_mode;
let max_connections = ctx.max_connections;
match transport {
Transport::Insecure => {
tracing::warn!(
"--insecure-no-tls: accepted PLAINTEXT connection from {peer:?} -- no TLS, no \
authentication"
);
thread::spawn(move || {
let mut stream = stream;
match slot {
Ok(_slot) => {
let Some(db) = open_connection_database(&db_path, peer) else {
return;
};
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
connection::handle_connection_with_gpu(
stream,
threads,
&gpu,
&db,
compute_mode,
)
}));
report_connection_result(peer, result);
}
Err(active) => {
connection::refuse_for_capacity(&mut stream, peer, active, max_connections);
}
}
});
}
Transport::Tls { config, auth } => {
let config = Arc::clone(config);
let auth = auth.clone();
thread::spawn(move || {
let Some(mut tls_stream) = accept_tls(stream, &config, &auth, peer) else {
return; };
match slot {
Ok(_slot) => {
let Some(db) = open_connection_database(&db_path, peer) else {
return;
};
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
connection::handle_connection_with_gpu(
tls_stream,
threads,
&gpu,
&db,
compute_mode,
)
}));
report_connection_result(peer, result);
}
Err(active) => {
connection::refuse_for_capacity(
&mut tls_stream,
peer,
active,
max_connections,
);
}
}
});
}
}
}
#[cfg(not(feature = "worker"))]
fn spawn_connection_handler(
stream: TcpStream,
peer: Option<SocketAddr>,
transport: &Transport,
db_path: &std::path::Path,
slot: Result<ConnectionSlot, usize>,
max_connections: usize,
) {
let db_path = db_path.to_path_buf();
match transport {
Transport::Insecure => {
tracing::warn!(
"--insecure-no-tls: accepted PLAINTEXT connection from {peer:?} -- no TLS, no \
authentication"
);
thread::spawn(move || {
let mut stream = stream;
match slot {
Ok(_slot) => {
let Some(db) = open_connection_database(&db_path, peer) else {
return;
};
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
connection::handle_connection(stream, &db)
}));
report_connection_result(peer, result);
}
Err(active) => {
connection::refuse_for_capacity(&mut stream, peer, active, max_connections);
}
}
});
}
Transport::Tls { config, auth } => {
let config = Arc::clone(config);
let auth = auth.clone();
thread::spawn(move || {
let Some(mut tls_stream) = accept_tls(stream, &config, &auth, peer) else {
return; };
match slot {
Ok(_slot) => {
let Some(db) = open_connection_database(&db_path, peer) else {
return;
};
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
connection::handle_connection(tls_stream, &db)
}));
report_connection_result(peer, result);
}
Err(active) => {
connection::refuse_for_capacity(
&mut tls_stream,
peer,
active,
max_connections,
);
}
}
});
}
}
}
fn open_connection_database(path: &std::path::Path, peer: Option<SocketAddr>) -> Option<Database> {
match open_library_database(path) {
Ok(db) => Some(db),
Err(e) => {
tracing::warn!("connection {peer:?}: {e}");
None
}
}
}
#[cfg(feature = "worker")]
fn capability_summary(args: &ServeArgs) -> String {
match args.compute_mode {
ComputeMode::OnlyCpu => "library + render (CPU only, --only-cpu)".to_string(),
ComputeMode::OnlyGpu => "library + render (GPU only, --only-gpu)".to_string(),
ComputeMode::Hybrid => "library + render".to_string(),
}
}
#[cfg(not(feature = "worker"))]
fn capability_summary(_args: &ServeArgs) -> String {
"library only (no render capacity -- build with --features worker to add it)".to_string()
}
#[cfg(test)]
mod limiter_tests {
use super::{ConnectionLimiter, connection};
use std::sync::atomic::Ordering;
#[test]
fn acquires_up_to_max_and_refuses_the_next() {
let limiter = ConnectionLimiter::new(2);
let a = limiter
.try_acquire()
.expect("1st connection is under the cap");
let b = limiter
.try_acquire()
.expect("2nd connection is exactly at the cap");
let refused = limiter
.try_acquire()
.expect_err("3rd connection is over the cap");
assert_eq!(
refused, 2,
"the refusal should report the active count that caused it"
);
drop(a);
let c = limiter
.try_acquire()
.expect("a slot freed by drop can be reacquired");
drop(b);
drop(c);
assert_eq!(limiter.active.load(Ordering::SeqCst), 0);
}
#[test]
fn a_refused_attempt_never_permanently_eats_into_the_cap() {
let limiter = ConnectionLimiter::new(1);
let _held = limiter.try_acquire().unwrap();
for _ in 0..5 {
limiter.try_acquire().unwrap_err();
}
assert_eq!(limiter.active.load(Ordering::SeqCst), 1);
}
#[test]
fn the_slot_releases_its_count_even_when_the_holder_panics() {
let limiter = ConnectionLimiter::new(1);
let slot = limiter.try_acquire().unwrap();
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _slot = slot; panic!("simulated connection-thread panic while holding a ConnectionSlot");
}));
assert!(result.is_err());
assert_eq!(limiter.active.load(Ordering::SeqCst), 0);
}
#[test]
fn refuse_for_capacity_writes_a_decodable_error_naming_the_capacity_code() {
let mut buf: Vec<u8> = Vec::new();
connection::refuse_for_capacity(&mut buf, None, 64, 64);
let mut cursor = std::io::Cursor::new(buf);
let err: indicatrix_net::messages::ErrorMsg =
indicatrix_net::messages::read_message(&mut cursor).unwrap();
assert_eq!(err.code, connection::CONNECTION_LIMIT_REACHED_CODE);
assert!(err.message.contains("64"), "{}", err.message);
}
}