feat: pool spacetime client connections

This commit is contained in:
2026-04-23 14:33:15 +08:00
parent da7c1ff0c5
commit 9d25a47b23
5 changed files with 415 additions and 52 deletions

View File

@@ -67,6 +67,7 @@ pub struct AppConfig {
pub spacetime_server_url: String,
pub spacetime_database: String,
pub spacetime_token: Option<String>,
pub spacetime_pool_size: u32,
pub llm_provider: LlmProvider,
pub llm_base_url: String,
pub llm_api_key: Option<String>,
@@ -139,6 +140,7 @@ impl Default for AppConfig {
spacetime_server_url: "http://127.0.0.1:3000".to_string(),
spacetime_database: "genarrative-dev".to_string(),
spacetime_token: None,
spacetime_pool_size: 4,
llm_provider: LlmProvider::Ark,
llm_base_url: DEFAULT_ARK_BASE_URL.to_string(),
llm_api_key: None,
@@ -370,6 +372,11 @@ impl AppConfig {
}
config.spacetime_token = read_first_non_empty_env(&["GENARRATIVE_SPACETIME_TOKEN"]);
if let Some(spacetime_pool_size) =
read_first_positive_u32_env(&["GENARRATIVE_SPACETIME_POOL_SIZE"])
{
config.spacetime_pool_size = spacetime_pool_size;
}
if let Some(llm_provider) =
read_first_llm_provider_env(&["GENARRATIVE_LLM_PROVIDER", "LLM_PROVIDER"])
@@ -620,3 +627,51 @@ fn parse_positive_u16(raw: &str) -> Option<u16> {
Some(value)
}
#[cfg(test)]
mod tests {
use super::AppConfig;
use std::sync::{Mutex, OnceLock};
static ENV_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
#[test]
fn from_env_reads_spacetime_pool_size() {
let _guard = ENV_LOCK
.get_or_init(|| Mutex::new(()))
.lock()
.expect("env lock should not poison");
unsafe {
std::env::remove_var("GENARRATIVE_SPACETIME_POOL_SIZE");
std::env::set_var("GENARRATIVE_SPACETIME_POOL_SIZE", "7");
}
let config = AppConfig::from_env();
assert_eq!(config.spacetime_pool_size, 7);
unsafe {
std::env::remove_var("GENARRATIVE_SPACETIME_POOL_SIZE");
}
}
#[test]
fn from_env_ignores_zero_spacetime_pool_size() {
let _guard = ENV_LOCK
.get_or_init(|| Mutex::new(()))
.lock()
.expect("env lock should not poison");
unsafe {
std::env::remove_var("GENARRATIVE_SPACETIME_POOL_SIZE");
std::env::set_var("GENARRATIVE_SPACETIME_POOL_SIZE", "0");
}
let config = AppConfig::from_env();
assert_eq!(config.spacetime_pool_size, 4);
unsafe {
std::env::remove_var("GENARRATIVE_SPACETIME_POOL_SIZE");
}
}
}

View File

@@ -116,6 +116,7 @@ impl AppState {
server_url: config.spacetime_server_url.clone(),
database: config.spacetime_database.clone(),
token: config.spacetime_token.clone(),
pool_size: config.spacetime_pool_size,
});
let llm_client = build_llm_client(&config)?;