use mold_core::Config;
use mold_inference::InferenceEngine;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::Mutex;
#[derive(Clone)]
pub struct AppState {
pub engine: Arc<Mutex<Option<Box<dyn InferenceEngine>>>>,
pub config: Arc<tokio::sync::RwLock<Config>>,
pub start_time: Instant,
pub model_load_lock: Arc<Mutex<()>>,
pub pull_lock: Arc<Mutex<()>>,
}
impl AppState {
pub fn new(engine: Box<dyn InferenceEngine>, config: Config) -> Self {
Self {
engine: Arc::new(Mutex::new(Some(engine))),
config: Arc::new(tokio::sync::RwLock::new(config)),
start_time: Instant::now(),
model_load_lock: Arc::new(Mutex::new(())),
pull_lock: Arc::new(Mutex::new(())),
}
}
pub fn empty(config: Config) -> Self {
Self {
engine: Arc::new(Mutex::new(None)),
config: Arc::new(tokio::sync::RwLock::new(config)),
start_time: Instant::now(),
model_load_lock: Arc::new(Mutex::new(())),
pull_lock: Arc::new(Mutex::new(())),
}
}
#[cfg(test)]
pub fn with_engine(engine: impl InferenceEngine + 'static) -> Self {
Self {
engine: Arc::new(Mutex::new(Some(Box::new(engine)))),
config: Arc::new(tokio::sync::RwLock::new(Config::default())),
start_time: Instant::now(),
model_load_lock: Arc::new(Mutex::new(())),
pull_lock: Arc::new(Mutex::new(())),
}
}
}