(
path: &str,
max_connections: usize,
max_idle_duration: Duration,
health_check_interval: Duration,
)
| 104 | } |
| 105 | |
| 106 | pub fn new_with_config( |
| 107 | path: &str, |
| 108 | max_connections: usize, |
| 109 | max_idle_duration: Duration, |
| 110 | health_check_interval: Duration, |
| 111 | ) -> Result<Self> { |
| 112 | let stats = Arc::new(Mutex::new(PoolStats { |
| 113 | total_connections: 0, |
| 114 | idle_connections: 0, |
| 115 | active_connections: 0, |
| 116 | health_checks_performed: 0, |
| 117 | connections_created: 0, |
| 118 | connections_dropped: 0, |
| 119 | health_check_failures: 0, |
| 120 | })); |
| 121 | |
| 122 | let pool = SqlitePool { |
| 123 | path: path.to_string(), |
| 124 | connections: Arc::new(Mutex::new(Vec::new())), |
| 125 | semaphore: Arc::new(Semaphore::new(max_connections)), |
| 126 | max_connections, |
| 127 | max_idle_duration, |
| 128 | health_check_interval, |
| 129 | stats, |
| 130 | }; |
| 131 | |
| 132 | // Pre-create initial connections (half of max) |
| 133 | let initial_connections = (max_connections / 2).max(1); |
| 134 | let mut conns = pool.connections.lock().unwrap(); |
| 135 | let mut stats = pool.stats.lock().unwrap(); |
| 136 | for _ in 0..initial_connections { |
| 137 | let conn = pool.create_connection()?; |
| 138 | conns.push(PoolConnection::new(conn)); |
| 139 | stats.connections_created += 1; |
| 140 | stats.total_connections += 1; |
| 141 | stats.idle_connections += 1; |
| 142 | } |
| 143 | drop(conns); |
| 144 | drop(stats); |
| 145 | |
| 146 | // Start background health check task only if not in test mode |
| 147 | #[cfg(not(test))] |
| 148 | { |
| 149 | let pool_clone = SqlitePool { |
| 150 | path: pool.path.clone(), |
| 151 | connections: pool.connections.clone(), |
| 152 | semaphore: Arc::new(Semaphore::new(0)), // Not used in background task |
| 153 | max_connections: pool.max_connections, |
| 154 | max_idle_duration: pool.max_idle_duration, |
| 155 | health_check_interval: pool.health_check_interval, |
| 156 | stats: pool.stats.clone(), |
| 157 | }; |
| 158 | |
| 159 | tokio::spawn(async move { |
| 160 | pool_clone.background_health_check().await; |
| 161 | }); |
| 162 | } |
| 163 |
nothing calls this directly
no test coverage detected