use leindex::cli::cli;
#[cfg(feature = "memprof")]
#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
const TOKIO_WORKERS_ENV: &str = "LEINDEX_TOKIO_WORKERS";
const DEFAULT_TOKIO_WORKERS: usize = 2;
fn configured_worker_count() -> usize {
std::env::var(TOKIO_WORKERS_ENV)
.ok()
.and_then(|v| v.parse::<usize>().ok())
.filter(|&n| n > 0)
.unwrap_or(DEFAULT_TOKIO_WORKERS)
}
fn build_runtime() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(configured_worker_count())
.enable_all()
.build()
.expect("failed to build Tokio runtime")
}
fn main() -> anyhow::Result<()> {
#[cfg(feature = "onnx")]
if leindex::embed::worker_main::is_internal_worker_invocation() {
let mut argv: Vec<String> = std::env::args().collect();
argv.remove(1);
leindex::embed::worker_main::run_from(argv);
}
#[cfg(not(feature = "onnx"))]
{
}
#[cfg(all(feature = "daemon-client", unix))]
{
let args: Vec<String> = std::env::args().skip(1).collect();
if let Some(project) = leindex::cli::daemon::client::eligible_project(&args) {
match leindex::cli::daemon::client::run(project) {
leindex::cli::daemon::client::Outcome::Done(status) => {
std::process::exit(status);
}
leindex::cli::daemon::client::Outcome::Fallback(reason) => {
eprintln!("leindex: running standalone ({reason})");
}
}
}
}
let rt = build_runtime();
rt.block_on(cli::main())
}
#[cfg(test)]
mod test {
use super::*;
use std::sync::Mutex;
static ENV_LOCK: Mutex<()> = Mutex::new(());
#[test]
fn test_default_workers_is_two() {
let _guard = ENV_LOCK.lock().unwrap();
unsafe {
std::env::remove_var("LEINDEX_TOKIO_WORKERS");
}
assert_eq!(configured_worker_count(), DEFAULT_TOKIO_WORKERS);
assert_eq!(configured_worker_count(), 2);
}
#[test]
fn test_explicit_worker_count_is_respected() {
let _guard = ENV_LOCK.lock().unwrap();
unsafe {
std::env::set_var("LEINDEX_TOKIO_WORKERS", "4");
}
assert_eq!(configured_worker_count(), 4);
unsafe {
std::env::remove_var("LEINDEX_TOKIO_WORKERS");
}
}
#[test]
fn test_zero_workers_falls_back_to_default() {
let _guard = ENV_LOCK.lock().unwrap();
unsafe {
std::env::set_var("LEINDEX_TOKIO_WORKERS", "0");
}
assert_eq!(configured_worker_count(), 2);
unsafe {
std::env::remove_var("LEINDEX_TOKIO_WORKERS");
}
}
#[test]
fn test_non_numeric_workers_falls_back_to_default() {
let _guard = ENV_LOCK.lock().unwrap();
unsafe {
std::env::set_var("LEINDEX_TOKIO_WORKERS", "abc");
}
assert_eq!(configured_worker_count(), 2);
unsafe {
std::env::remove_var("LEINDEX_TOKIO_WORKERS");
}
}
}