|
4 | 4 | //!
|
5 | 5 | //! 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.
|
6 | 6 |
|
| 7 | +use anyhow::Context; |
7 | 8 | use once_cell::sync::Lazy;
|
8 | 9 | use parking_lot::RwLock;
|
9 | 10 | use sqlx::{postgres::PgPoolOptions, PgPool};
|
10 |
| -use std::collections::HashMap; |
11 | 11 | use std::env;
|
| 12 | +use std::{collections::HashMap, time::Duration}; |
12 | 13 | use tokio::runtime::{Builder, Runtime};
|
13 | 14 | use tracing::Level;
|
14 | 15 | use tracing_subscriber::FmtSubscriber;
|
@@ -67,13 +68,60 @@ async fn get_or_initialize_pool(database_url: &Option<String>) -> anyhow::Result
|
67 | 68 | if let Some(pool) = pools.get(&url) {
|
68 | 69 | Ok(pool.clone())
|
69 | 70 | } 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); |
74 | 118 |
|
75 | 119 | 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) |
77 | 125 | .connect_lazy(&url)?;
|
78 | 126 |
|
79 | 127 | pools.insert(url.to_string(), pool.clone());
|
|
0 commit comments