use std::cell::Cell;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::sync::{Condvar, Mutex, MutexGuard};
use std::thread;
use std::time::{Duration, Instant};
use anyhow::{Context, Result};
use rusqlite::{Connection, ErrorCode};
pub(super) const BUSY_TIMEOUT: Duration = Duration::from_secs(5);
pub(super) fn open_connection(path: &Path) -> Result<Connection> {
let conn = Connection::open(path)
.with_context(|| format!("opening sqlite db at {}", path.display()))?;
conn.busy_timeout(BUSY_TIMEOUT)
.context("arming busy_timeout")?;
set_wal_mode(&conn)?;
conn.pragma_update(None, "synchronous", "NORMAL")
.context("setting synchronous=NORMAL")?;
Ok(conn)
}
fn set_wal_mode(conn: &Connection) -> Result<()> {
let deadline = Instant::now() + BUSY_TIMEOUT;
loop {
match conn.pragma_update(None, "journal_mode", "WAL") {
Ok(()) => return Ok(()),
Err(err) if is_database_busy(&err) && Instant::now() < deadline => {
thread::sleep(Duration::from_millis(20));
}
Err(err) => return Err(err).context("setting journal_mode=WAL"),
}
}
}
fn is_database_busy(err: &rusqlite::Error) -> bool {
err.sqlite_error_code() == Some(ErrorCode::DatabaseBusy)
}
pub(super) fn default_pool_size() -> usize {
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(4)
.clamp(2, 16)
}
thread_local! {
static READ_DEADLINE: Cell<Option<Instant>> = const { Cell::new(None) };
}
pub struct ReadDeadline {
previous: Option<Instant>,
not_send: PhantomData<*const ()>,
}
impl ReadDeadline {
pub fn set(deadline: Instant) -> Self {
ReadDeadline {
previous: READ_DEADLINE.replace(Some(deadline)),
not_send: PhantomData,
}
}
}
impl Drop for ReadDeadline {
fn drop(&mut self) {
READ_DEADLINE.set(self.previous);
}
}
pub(super) struct ReaderPool {
path: PathBuf,
free: Mutex<Vec<Connection>>,
returned: Condvar,
}
impl ReaderPool {
pub(super) fn open(path: &Path, size: usize) -> Result<Self> {
let free = (0..size)
.map(|_| open_connection(path))
.collect::<Result<Vec<_>>>()?;
Ok(Self {
path: path.to_path_buf(),
free: Mutex::new(free),
returned: Condvar::new(),
})
}
pub(super) fn checkout(&self) -> PooledConn<'_> {
let mut free = self.lock();
if let Some(deadline) = READ_DEADLINE.get() {
let wait = deadline.saturating_duration_since(Instant::now());
free = self
.returned
.wait_timeout_while(free, wait, |f| f.is_empty())
.unwrap_or_else(|e| e.into_inner())
.0;
if free.is_empty() {
drop(free);
match open_connection(&self.path) {
Ok(conn) => {
return PooledConn {
conn: Some(conn),
pool: None,
};
}
Err(e) => {
tracing::warn!("no read connection to spare past a deadline: {e:#}");
free = self.lock();
}
}
}
}
let mut free = self
.returned
.wait_while(free, |f| f.is_empty())
.unwrap_or_else(|e| e.into_inner());
PooledConn {
conn: free.pop(),
pool: Some(self),
}
}
fn lock(&self) -> MutexGuard<'_, Vec<Connection>> {
self.free.lock().unwrap_or_else(|e| e.into_inner())
}
}
pub(crate) struct PooledConn<'a> {
conn: Option<Connection>,
pool: Option<&'a ReaderPool>,
}
impl std::ops::Deref for PooledConn<'_> {
type Target = Connection;
fn deref(&self) -> &Connection {
self.conn.as_ref().expect("taken only in Drop")
}
}
impl Drop for PooledConn<'_> {
fn drop(&mut self) {
if let (Some(conn), Some(pool)) = (self.conn.take(), self.pool) {
pool.lock().push(conn);
pool.returned.notify_one();
}
}
}
#[cfg(test)]
mod tests {
use std::sync::mpsc;
use std::time::Duration;
use super::*;
fn pool(size: usize) -> (tempfile::TempDir, ReaderPool) {
let dir = tempfile::tempdir().unwrap();
let pool = ReaderPool::open(&dir.path().join("brain.db"), size).unwrap();
(dir, pool)
}
#[test]
fn a_read_past_its_deadline_opens_a_spare_connection_instead_of_waiting() {
let (_dir, pool) = pool(1);
let held = pool.checkout();
let (done, answered) = mpsc::channel();
thread::scope(|scope| {
scope.spawn(|| {
let _bound = ReadDeadline::set(Instant::now() + Duration::from_millis(50));
let spare = pool.checkout();
let one: i64 = spare.query_row("SELECT 1", [], |r| r.get(0)).unwrap();
done.send(one).unwrap();
});
let answer = answered.recv_timeout(Duration::from_secs(10));
drop(held);
assert_eq!(
answer,
Ok(1),
"the bounded read waited for the held connection"
);
});
assert_eq!(pool.lock().len(), 1, "only the pooled connection came back");
}
#[test]
fn a_read_without_a_deadline_waits_for_a_pooled_connection() {
let (_dir, pool) = pool(1);
let held = pool.checkout();
let (done, answered) = mpsc::channel();
thread::scope(|scope| {
scope.spawn(|| {
let conn = pool.checkout();
done.send(conn.is_autocommit()).unwrap();
});
assert!(answered.recv_timeout(Duration::from_millis(100)).is_err());
drop(held);
assert!(answered.recv_timeout(Duration::from_secs(10)).is_ok());
});
}
#[test]
fn a_deadline_guard_restores_the_one_before_it() {
let outer = Instant::now() + Duration::from_secs(60);
let _outer = ReadDeadline::set(outer);
{
let _inner = ReadDeadline::set(Instant::now());
}
assert_eq!(READ_DEADLINE.get(), Some(outer));
}
}