Skip to main content

cerno_server/
state.rs

1//! What every handler shares.
2
3use crate::config::Config;
4use cerno_core::Engine;
5use std::sync::Arc;
6use tokio::sync::Semaphore;
7
8#[derive(Clone)]
9pub struct AppState {
10    pub engine: Arc<Engine>,
11    pub config: Arc<Config>,
12    /// Caps how many questions are in flight against the host at once.
13    ///
14    /// Ollama serialises beyond its own parallelism setting, so firing an unbounded number of
15    /// questions at it turns a burst into a queue with no backpressure. A bound keeps the
16    /// latency of any single request predictable.
17    pub semaphore: Arc<Semaphore>,
18}
19
20impl AppState {
21    pub fn new(engine: Engine, config: Config) -> Self {
22        let permits = config.max_concurrent_questions;
23        Self {
24            engine: Arc::new(engine),
25            config: Arc::new(config),
26            semaphore: Arc::new(Semaphore::new(permits)),
27        }
28    }
29}