use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Condvar, Mutex};
use rusqlite::{Connection, OpenFlags};
use crate::error::{MemdError, Result};
pub const DEFAULT_POOL_MAX_SIZE: usize = 16;
pub struct SqliteConnectionPool {
inner: Arc<PoolInner>,
}
struct PoolInner {
db_path: PathBuf,
open_flags: Option<OpenFlags>,
state: Mutex<PoolState>,
not_empty: Condvar,
max_size: usize,
}
struct PoolState {
idle: Vec<Connection>,
outstanding: usize,
}
impl SqliteConnectionPool {
pub fn open(db_path: &Path) -> Result<Self> {
let max_size = std::env::var("MEMD_SQLITE_POOL_MAX")
.ok()
.and_then(|v| v.parse::<usize>().ok())
.filter(|v| *v > 0)
.unwrap_or(DEFAULT_POOL_MAX_SIZE);
Self::open_with_max(db_path, max_size)
}
pub fn open_with_max(db_path: &Path, max_size: usize) -> Result<Self> {
Self::open_with_max_and_flags(db_path, max_size, None)
}
pub fn open_uri_with_max(uri: &str, max_size: usize) -> Result<Self> {
let flags = OpenFlags::SQLITE_OPEN_READ_WRITE
| OpenFlags::SQLITE_OPEN_CREATE
| OpenFlags::SQLITE_OPEN_URI
| OpenFlags::SQLITE_OPEN_SHARED_CACHE;
Self::open_with_max_and_flags(Path::new(uri), max_size, Some(flags))
}
fn open_with_max_and_flags(
db_path: &Path,
max_size: usize,
open_flags: Option<OpenFlags>,
) -> Result<Self> {
assert!(max_size > 0, "SQLite pool max_size must be > 0");
let warm = open_configured_connection(db_path, open_flags)?;
let inner = Arc::new(PoolInner {
db_path: db_path.to_path_buf(),
open_flags,
state: Mutex::new(PoolState {
idle: vec![warm],
outstanding: 0,
}),
not_empty: Condvar::new(),
max_size,
});
Ok(Self { inner })
}
pub fn get(&self) -> PooledConnection {
let mut state = self.inner.state.lock().unwrap_or_else(|e| e.into_inner());
loop {
if let Some(conn) = state.idle.pop() {
state.outstanding += 1;
return PooledConnection {
conn: Some(conn),
pool: Arc::clone(&self.inner),
};
}
let total = state.idle.len() + state.outstanding;
if total < self.inner.max_size {
state.outstanding += 1;
drop(state);
match open_configured_connection(&self.inner.db_path, self.inner.open_flags) {
Ok(conn) => {
return PooledConnection {
conn: Some(conn),
pool: Arc::clone(&self.inner),
};
}
Err(err) => {
let mut state = self.inner.state.lock().unwrap_or_else(|e| e.into_inner());
state.outstanding -= 1;
self.inner.not_empty.notify_one();
tracing::error!(
error = %err,
path = %self.inner.db_path.display(),
"failed to open SQLite pool connection — panicking to surface the hard failure"
);
panic!("SqliteConnectionPool: failed to open connection: {}", err);
}
}
}
state = self
.inner
.not_empty
.wait(state)
.unwrap_or_else(|e| e.into_inner());
}
}
pub fn idle_count(&self) -> usize {
self.inner
.state
.lock()
.unwrap_or_else(|e| e.into_inner())
.idle
.len()
}
pub fn max_size(&self) -> usize {
self.inner.max_size
}
}
impl Clone for SqliteConnectionPool {
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
}
}
}
pub struct PooledConnection {
conn: Option<Connection>,
pool: Arc<PoolInner>,
}
impl Deref for PooledConnection {
type Target = Connection;
fn deref(&self) -> &Connection {
self.conn
.as_ref()
.expect("PooledConnection used after drop")
}
}
impl DerefMut for PooledConnection {
fn deref_mut(&mut self) -> &mut Connection {
self.conn
.as_mut()
.expect("PooledConnection used after drop")
}
}
impl Drop for PooledConnection {
fn drop(&mut self) {
if let Some(conn) = self.conn.take() {
let mut state = self.pool.state.lock().unwrap_or_else(|e| e.into_inner());
state.outstanding -= 1;
state.idle.push(conn);
self.pool.not_empty.notify_one();
}
}
}
fn open_configured_connection(path: &Path, flags: Option<OpenFlags>) -> Result<Connection> {
let conn = match flags {
Some(flags) => Connection::open_with_flags(path, flags),
None => Connection::open(path),
}
.map_err(|e| MemdError::StorageError(format!("open sqlite at {}: {}", path.display(), e)))?;
conn.pragma_update(None, "journal_mode", "WAL")
.map_err(|e| MemdError::StorageError(format!("set journal_mode=WAL: {}", e)))?;
conn.pragma_update(None, "synchronous", "NORMAL")
.map_err(|e| MemdError::StorageError(format!("set synchronous=NORMAL: {}", e)))?;
conn.pragma_update(None, "busy_timeout", 5000)
.map_err(|e| MemdError::StorageError(format!("set busy_timeout=5000: {}", e)))?;
conn.pragma_update(None, "cache_size", -64000)
.map_err(|e| MemdError::StorageError(format!("set cache_size=-64000: {}", e)))?;
conn.pragma_update(None, "foreign_keys", "ON")
.map_err(|e| MemdError::StorageError(format!("enable foreign_keys: {}", e)))?;
Ok(conn)
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
#[test]
fn pool_grows_and_returns_idle() {
let tmp = tempdir().unwrap();
let db = tmp.path().join("test.db");
let pool = SqliteConnectionPool::open_with_max(&db, 4).unwrap();
assert_eq!(pool.idle_count(), 1);
let c1 = pool.get();
assert_eq!(pool.idle_count(), 0);
let c2 = pool.get();
let c3 = pool.get();
assert_eq!(pool.idle_count(), 0);
drop(c1);
assert_eq!(pool.idle_count(), 1);
drop(c2);
drop(c3);
assert_eq!(pool.idle_count(), 3);
}
#[test]
fn pool_applies_wal_pragma() {
let tmp = tempdir().unwrap();
let db = tmp.path().join("test.db");
let pool = SqliteConnectionPool::open_with_max(&db, 2).unwrap();
let conn = pool.get();
let mode: String = conn
.pragma_query_value(None, "journal_mode", |row| row.get(0))
.unwrap();
assert_eq!(mode.to_lowercase(), "wal");
}
#[test]
fn pool_supports_mixed_concurrent_reads_and_writes() {
use std::sync::Arc;
use std::thread;
let tmp = tempdir().unwrap();
let db = tmp.path().join("stress.db");
let pool = Arc::new(SqliteConnectionPool::open_with_max(&db, 8).unwrap());
{
let conn = pool.get();
conn.execute(
"CREATE TABLE stress (id INTEGER PRIMARY KEY, tag TEXT NOT NULL)",
[],
)
.unwrap();
}
let mut handles = Vec::new();
for w in 0..4 {
let pool = Arc::clone(&pool);
handles.push(thread::spawn(move || {
for i in 0..10 {
let conn = pool.get();
conn.execute(
"INSERT INTO stress (tag) VALUES (?1)",
rusqlite::params![format!("w{}-{}", w, i)],
)
.expect("writer insert must succeed (pool + busy_timeout)");
}
}));
}
for _ in 0..16 {
let pool = Arc::clone(&pool);
handles.push(thread::spawn(move || {
for _ in 0..25 {
let conn = pool.get();
let mut stmt = conn.prepare("SELECT COUNT(*) FROM stress").unwrap();
let count: i64 = stmt.query_row([], |row| row.get(0)).unwrap();
assert!(count >= 0);
}
}));
}
for handle in handles {
handle
.join()
.expect("concurrent pool worker must not panic");
}
let conn = pool.get();
let total: i64 = conn
.query_row("SELECT COUNT(*) FROM stress", [], |row| row.get(0))
.unwrap();
assert_eq!(total, 40, "all 4 writers x 10 inserts must commit");
}
#[test]
fn pool_blocks_and_releases_when_saturated() {
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
let tmp = tempdir().unwrap();
let db = tmp.path().join("test.db");
let pool = Arc::new(SqliteConnectionPool::open_with_max(&db, 1).unwrap());
let _held = pool.get();
let (tx, rx) = mpsc::channel();
let pool_for_thread = Arc::clone(&pool);
let handle = thread::spawn(move || {
let c = pool_for_thread.get();
tx.send(()).unwrap();
drop(c);
});
assert!(
rx.recv_timeout(Duration::from_millis(150)).is_err(),
"second get() must block while pool is saturated"
);
drop(_held);
rx.recv_timeout(Duration::from_millis(500))
.expect("get() must unblock after the holder releases");
handle.join().unwrap();
}
}