Skip to content

Commit bc44dba

Browse files
authored
SDK - Expose config for sqlx pool (#1389)
1 parent f75114b commit bc44dba

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

pgml-sdks/pgml/src/lib.rs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
//!
55
//! With this SDK, you can seamlessly manage various database tables related to documents, text chunks, text splitters, LLM (Language Model) models, and embeddings. By leveraging the SDK's capabilities, you can efficiently index LLM embeddings using PgVector for fast and accurate queries.
66
7+
use anyhow::Context;
78
use once_cell::sync::Lazy;
89
use parking_lot::RwLock;
910
use sqlx::{postgres::PgPoolOptions, PgPool};
10-
use std::collections::HashMap;
1111
use std::env;
12+
use std::{collections::HashMap, time::Duration};
1213
use tokio::runtime::{Builder, Runtime};
1314
use tracing::Level;
1415
use tracing_subscriber::FmtSubscriber;
@@ -67,13 +68,60 @@ async fn get_or_initialize_pool(database_url: &Option<String>) -> anyhow::Result
6768
if let Some(pool) = pools.get(&url) {
6869
Ok(pool.clone())
6970
} else {
70-
let timeout = std::env::var("PGML_CHECKOUT_TIMEOUT")
71-
.unwrap_or_else(|_| "5000".to_string())
72-
.parse::<u64>()
73-
.expect("Error parsing PGML_CHECKOUT_TIMEOUT, expected an integer");
71+
let acquire_timeout = std::env::var("PGML_CHECKOUT_TIMEOUT")
72+
.ok()
73+
.map(|v| v.parse::<u64>())
74+
.transpose()
75+
.context("Error parsing PGML_CHECKOUT_TIMEOUT, expected an integer")?
76+
.map(anyhow::Ok)
77+
.unwrap_or_else(|| {
78+
Ok(std::env::var("PGML_POOL_ACQUIRE_TIMEOUT")
79+
.ok()
80+
.map(|v| v.parse::<u64>())
81+
.transpose()
82+
.context("Error parsing PGML_POOL_ACQUIRE_TIMEOUT, expected an integer")?
83+
.unwrap_or(30000))
84+
})?;
85+
let acquire_timeout = Duration::from_millis(acquire_timeout);
86+
87+
let max_lifetime = std::env::var("PGML_POOL_MAX_LIFETIME")
88+
.ok()
89+
.map(|v| {
90+
anyhow::Ok(Duration::from_millis(v.parse::<u64>().context(
91+
"Error parsing PGML_POOL_MAX_LIFETIME, expected an integer",
92+
)?))
93+
})
94+
.transpose()?;
95+
96+
let idle_timeout = std::env::var("PGML_POOL_IDLE_TIMEOUT")
97+
.ok()
98+
.map(|v| {
99+
anyhow::Ok(Duration::from_millis(v.parse::<u64>().context(
100+
"Error parsing PGML_POOL_IDLE_TIMEOUT, expected an integer",
101+
)?))
102+
})
103+
.transpose()?;
104+
105+
let max_connections = std::env::var("PGML_POOL_MAX_CONNECTIONS")
106+
.ok()
107+
.map(|v| v.parse::<u32>())
108+
.transpose()
109+
.context("Error parsing PGML_POOL_MAX_CONNECTIONS, expected an integer")?
110+
.unwrap_or(10);
111+
112+
let min_connections = std::env::var("PGML_POOL_MIN_CONNECTIONS")
113+
.ok()
114+
.map(|v| v.parse::<u32>())
115+
.transpose()
116+
.context("Error parsing PGML_POOL_MIN_CONNECTIONS, expected an integer")?
117+
.unwrap_or(0);
74118

75119
let pool = PgPoolOptions::new()
76-
.acquire_timeout(std::time::Duration::from_millis(timeout))
120+
.max_connections(max_connections)
121+
.min_connections(min_connections)
122+
.acquire_timeout(acquire_timeout)
123+
.max_lifetime(max_lifetime)
124+
.idle_timeout(idle_timeout)
77125
.connect_lazy(&url)?;
78126

79127
pools.insert(url.to_string(), pool.clone());

0 commit comments

Comments
 (0)