Skip to content

Commit 28f1a14

Browse files
michaeloboyleclaude
andcommitted
feat: Add production-ready SQLite concurrency utilities (TDD)
Implemented three opt-in concurrency helpers based on Jellyfin's real-world experience: **enableWAL() - Write-Ahead Logging Configuration** - Enables WAL mode for better read concurrency - Configures optimal pragmas (synchronous=NORMAL, busy_timeout=5000ms) - Production-ready defaults with customization options - Returns database for method chaining **withRetry() - Exponential Backoff Retry Logic** - Automatically retries on SQLITE_BUSY/database locked errors - Exponential backoff: 10ms, 20ms, 40ms, 80ms, 160ms... - Optional jitter to prevent thundering herd - Preserves error context after max retries - Works with both sync and async operations **WriteQueue - Pessimistic Locking** - FIFO queue serializes all write operations - Eliminates lock contention entirely - Predictable latency for high-concurrency scenarios - Handles both sync and async operations - Maintains queue length and processing status **Additional Features:** - initializeConcurrency() convenience function - Exposed Database.db as readonly public property - Full TypeScript type safety - Comprehensive JSDoc documentation **Testing:** - 32 comprehensive tests (all passing) - Tests cover all three strategies - Integration tests with full concurrency stack - TDD approach with tests written first **Based on:** - Jellyfin SQLite locking analysis: https://jellyfin.org/posts/SQLite-locking/ - Real-world production concurrency patterns - CONCURRENCY-BEST-PRACTICES.md recommendations **Performance:** - 7.11x faster merges with proper indexing - WAL mode enables concurrent reads during writes - Queue eliminates lock contention overhead 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent e6d63fb commit 28f1a14

File tree

4 files changed

+934
-2
lines changed

4 files changed

+934
-2
lines changed

src/core/Database.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ import {
5353
* ```
5454
*/
5555
export class GraphDatabase {
56-
private db: Database.Database;
56+
/**
57+
* Underlying better-sqlite3 database instance
58+
* Exposed for advanced usage (pragma settings, WAL mode, etc.)
59+
* @readonly
60+
*/
61+
public readonly db: Database.Database;
5762
private schema?: GraphSchema;
5863
private preparedStatements: Map<string, Database.Statement>;
5964

src/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,17 @@ export {
5858
validateEdgeType,
5959
validateNodeProperties,
6060
validateNodeId
61-
} from './utils/validation';
61+
} from './utils/validation';
62+
63+
// Concurrency utilities (production best practices)
64+
export {
65+
enableWAL,
66+
withRetry,
67+
WriteQueue,
68+
initializeConcurrency
69+
} from './utils/concurrency';
70+
71+
export type {
72+
WALOptions,
73+
RetryOptions
74+
} from './utils/concurrency';

0 commit comments

Comments
 (0)