YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix: separate read/write DB pools to prevent pool exhaustion
Mykola Kotliarenko994b395db34f
diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rsindex de0894eda..bcba84076 100644--- a/crates/screenpipe-db/src/db.rs+++ b/crates/screenpipe-db/src/db.rs@@ -138,7 +138,15 @@ impl Drop for ImmediateTx {}pub struct DatabaseManager {+ /// Read-only pool (27 connections). Used for all SELECT queries.+ /// Separated from writes so read bursts (search, timeline, API) can never+ /// starve the write pipeline.pub pool: SqlitePool,+ /// Dedicated write pool (3 connections). Used exclusively by+ /// begin_immediate_with_retry(). Small pool is fine because writes are+ /// serialized by write_semaphore anyway — the extra connections handle+ /// the rare case of connection detach without killing the pool.+ write_pool: SqlitePool,/// Serializes write transactions. Writers queue in Rust memory (zero overhead)/// instead of each holding a pool connection while waiting for SQLite's busy_timeout./// With FTS handled by inline triggers (not the removed background indexer),@@ -147,7 +155,7 @@ pub struct DatabaseManager {/// Limits concurrent heavy read queries (e.g. find_video_chunks) to 2./// These queries can take 60+ seconds on large DBs with legacy data,/// starving the pool for writes and fast reads. By capping at 2 concurrent- /// heavy reads, we guarantee 28+ connections remain available for normal ops.+ /// heavy reads, we guarantee 25+ connections remain available for normal ops.heavy_read_semaphore: Arc<Semaphore>,}@@ -194,17 +202,28 @@ impl DatabaseManager {// Crash recovery: ~200ms replay at most..pragma("wal_autocheckpoint", "4000");- let pool = SqlitePoolOptions::new()- // Pool handles both read and write concurrency. Writes are serialized- // by SQLite's WAL mode + busy_timeout(5s).- .max_connections(30)- .min_connections(5) // Minimum number of idle connections+ // Read pool: handles all SELECT queries (search, timeline, API, pipes).+ // 27 connections — large enough to handle read bursts without starving.+ let read_pool = SqlitePoolOptions::new()+ .max_connections(27)+ .min_connections(3)+ .acquire_timeout(Duration::from_secs(5))+ .connect_with(connect_options.clone())+ .await?;++ // Write pool: dedicated to INSERT/UPDATE/DELETE via begin_immediate_with_retry().+ // 3 connections — writes are serialized by write_semaphore so only 1 is active+ // at a time; extras absorb connection detach without killing the pool.+ let write_pool = SqlitePoolOptions::new()+ .max_connections(3)+ .min_connections(1).acquire_timeout(Duration::from_secs(10)).connect_with(connect_options).await?;let db_manager = DatabaseManager {- pool,+ pool: read_pool,+ write_pool,write_semaphore: Arc::new(Semaphore::new(1)),heavy_read_semaphore: Arc::new(Semaphore::new(2)),};@@ -441,7 +460,7 @@ impl DatabaseManager {let mut last_error = None;for attempt in 1..=max_retries {let mut conn =- match tokio::time::timeout(Duration::from_secs(3), self.pool.acquire()).await {+ match tokio::time::timeout(Duration::from_secs(3), self.write_pool.acquire()).await {Ok(Ok(conn)) => conn,Ok(Err(e)) => return Err(e),Err(_) => return Err(sqlx::Error::PoolTimedOut),@@ -482,6 +501,17 @@ impl DatabaseManager {Err(last_error.unwrap_or_else(|| sqlx::Error::PoolTimedOut))}+ /// Returns pool statistics for health monitoring.+ /// (read_size, read_idle, write_size, write_idle)+ pub fn pool_stats(&self) -> (u32, u32, u32, u32) {+ (+ self.pool.size(),+ self.pool.num_idle() as u32,+ self.write_pool.size(),+ self.write_pool.num_idle() as u32,+ )+ }+/// Check if the error indicates a stuck/nested transaction on the connection.fn is_nested_transaction_error(e: &sqlx::Error) -> bool {match e {diff --git a/crates/screenpipe-db/src/migration_worker.rs b/crates/screenpipe-db/src/migration_worker.rsindex 364a56174..1f3a613ce 100644--- a/crates/screenpipe-db/src/migration_worker.rs+++ b/crates/screenpipe-db/src/migration_worker.rs@@ -73,7 +73,7 @@ pub struct MigrationConfig {impl Default for MigrationConfig {fn default() -> Self {Self {- batch_size: 1000,+ batch_size: 200,batch_delay_ms: 100,continue_on_error: true,}diff --git a/crates/screenpipe-engine/src/routes/health.rs b/crates/screenpipe-engine/src/routes/health.rsindex 29ab2f0d8..1d3df6df1 100644--- a/crates/screenpipe-engine/src/routes/health.rs+++ b/crates/screenpipe-engine/src/routes/health.rs@@ -50,6 +50,16 @@ pub struct HealthCheckResponse {pub audio_pipeline: Option<AudioPipelineHealthInfo>,#[serde(skip_serializing_if = "Option::is_none")]pub accessibility: Option<TreeWalkerSnapshot>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub pool_stats: Option<PoolHealthInfo>,+}++#[derive(Serialize, OaSchema, Deserialize)]+pub struct PoolHealthInfo {+ pub read_pool_size: u32,+ pub read_pool_idle: u32,+ pub write_pool_size: u32,+ pub write_pool_idle: u32,}#[derive(Serialize, OaSchema, Deserialize)]@@ -542,6 +552,15 @@ pub async fn health_check(State(state): State<Arc<AppState>>) -> JsonResponse<He} else {None},+ pool_stats: {+ let (rs, ri, ws, wi) = state.db.pool_stats();+ Some(PoolHealthInfo {+ read_pool_size: rs,+ read_pool_idle: ri,+ write_pool_size: ws,+ write_pool_idle: wi,+ })+ },})}diff --git a/crates/screenpipe-engine/src/snapshot_compaction.rs b/crates/screenpipe-engine/src/snapshot_compaction.rsindex 4edd3f885..be61e10de 100644--- a/crates/screenpipe-engine/src/snapshot_compaction.rs+++ b/crates/screenpipe-engine/src/snapshot_compaction.rs@@ -32,7 +32,7 @@ const MIN_AGE_SECS: i64 = 600; // 10 minutesconst POLL_INTERVAL_SECS: u64 = 300; // 5 minutes/// Maximum frames per MP4 chunk at normal thermal load.-const MAX_FRAMES_PER_CHUNK: usize = 500;+const MAX_FRAMES_PER_CHUNK: usize = 100;/// Smaller batch size when system is thermally stressed.const THROTTLED_FRAMES_PER_CHUNK: usize = 50;@@ -517,7 +517,7 @@ mod tests {ThermalState::Serious => (THROTTLED_FRAMES_PER_CHUNK, 30),ThermalState::Critical => (THROTTLED_FRAMES_PER_CHUNK, 120),};- assert_eq!(chunk_size, 500);+ assert_eq!(chunk_size, 100);assert_eq!(delay, 0);}