CNPG's cluster-ro service already handles load balancing and failover, so the application-level Vec + random_range is redundant. - db_read: Vec<DatabaseConnection> → Option<DatabaseConnection> - database_read_replicas returns Option<String> instead of Vec<String> - health checks now explicitly ping both writer() and reader() - remove unused rand dependency from libs/db
74 lines
2.7 KiB
Rust
74 lines
2.7 KiB
Rust
use crate::AppConfig;
|
|
|
|
impl AppConfig {
|
|
pub fn database_url(&self) -> anyhow::Result<String> {
|
|
if let Some(url) = self.env.get("APP_DATABASE_URL") {
|
|
return Ok(url.to_string());
|
|
}
|
|
Err(anyhow::anyhow!("APP_DATABASE_URL not found"))
|
|
}
|
|
pub fn database_max_connections(&self) -> anyhow::Result<u32> {
|
|
if let Some(max_connections) = self.env.get("APP_DATABASE_MAX_CONNECTIONS") {
|
|
return Ok(max_connections.parse::<u32>()?);
|
|
}
|
|
Ok(10)
|
|
}
|
|
pub fn database_min_connections(&self) -> anyhow::Result<u32> {
|
|
if let Some(min_connections) = self.env.get("APP_DATABASE_MIN_CONNECTIONS") {
|
|
return Ok(min_connections.parse::<u32>()?);
|
|
}
|
|
Ok(2)
|
|
}
|
|
pub fn database_idle_timeout(&self) -> anyhow::Result<u64> {
|
|
if let Some(idle_timeout) = self.env.get("APP_DATABASE_IDLE_TIMEOUT") {
|
|
return Ok(idle_timeout.parse::<u64>()?);
|
|
}
|
|
Ok(60000)
|
|
}
|
|
pub fn database_max_lifetime(&self) -> anyhow::Result<u64> {
|
|
if let Some(max_lifetime) = self.env.get("APP_DATABASE_MAX_LIFETIME") {
|
|
return Ok(max_lifetime.parse::<u64>()?);
|
|
}
|
|
Ok(300000)
|
|
}
|
|
pub fn database_connection_timeout(&self) -> anyhow::Result<u64> {
|
|
if let Some(connection_timeout) = self.env.get("APP_DATABASE_CONNECTION_TIMEOUT") {
|
|
return Ok(connection_timeout.parse::<u64>()?);
|
|
}
|
|
Ok(5000)
|
|
}
|
|
pub fn database_schema_search_path(&self) -> anyhow::Result<String> {
|
|
if let Some(schema_search_path) = self.env.get("APP_DATABASE_SCHEMA_SEARCH_PATH") {
|
|
return Ok(schema_search_path.to_string());
|
|
}
|
|
Ok("public".to_string())
|
|
}
|
|
pub fn database_read_replicas(&self) -> anyhow::Result<Option<String>> {
|
|
if let Some(replicas) = self.env.get("APP_DATABASE_REPLICAS") {
|
|
if replicas.is_empty() {
|
|
return Ok(None);
|
|
}
|
|
return Ok(Some(replicas.to_string()));
|
|
}
|
|
Ok(None)
|
|
}
|
|
pub fn database_health_check_interval(&self) -> anyhow::Result<u64> {
|
|
if let Some(interval) = self.env.get("APP_DATABASE_HEALTH_CHECK_INTERVAL") {
|
|
return Ok(interval.parse::<u64>()?);
|
|
}
|
|
Ok(30)
|
|
}
|
|
pub fn database_retry_attempts(&self) -> anyhow::Result<u32> {
|
|
if let Some(attempts) = self.env.get("APP_DATABASE_RETRY_ATTEMPTS") {
|
|
return Ok(attempts.parse::<u32>()?);
|
|
}
|
|
Ok(3)
|
|
}
|
|
pub fn database_retry_delay(&self) -> anyhow::Result<u64> {
|
|
if let Some(delay) = self.env.get("APP_DATABASE_RETRY_DELAY") {
|
|
return Ok(delay.parse::<u64>()?);
|
|
}
|
|
Ok(5)
|
|
}
|
|
}
|