-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionPool.ts
More file actions
388 lines (340 loc) · 10.2 KB
/
ConnectionPool.ts
File metadata and controls
388 lines (340 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
* Database Connection Pool
*
* Manages a pool of read-only SQLite connections for concurrent read operations.
* This prevents lock contention and improves read performance under load.
*
* Features:
* - Multiple read-only connections for concurrent queries
* - Single write connection for data modifications
* - Automatic connection reuse and cleanup
* - Connection health monitoring
* - Configurable pool size
*/
import Database from 'better-sqlite3';
import { EventEmitter } from 'node:events';
export interface PoolConfig {
/** Path to the database file */
dbPath: string;
/** Maximum number of read connections (default: 4) */
maxReadConnections?: number;
/** Connection idle timeout in ms (default: 60000) */
idleTimeout?: number;
/** Enable connection monitoring (default: true) */
monitoring?: boolean;
}
export interface PoolStats {
activeReadConnections: number;
idleReadConnections: number;
totalReadQueries: number;
totalWriteQueries: number;
averageReadTime: number;
averageWriteTime: number;
}
interface PooledConnection {
connection: Database.Database;
inUse: boolean;
lastUsed: number;
queryCount: number;
}
/**
* Connection pool for SQLite database
*/
export class ConnectionPool extends EventEmitter {
private writeConnection: Database.Database | null = null;
private readConnections: PooledConnection[] = [];
private config: Required<PoolConfig>;
private stats: PoolStats = {
activeReadConnections: 0,
idleReadConnections: 0,
totalReadQueries: 0,
totalWriteQueries: 0,
averageReadTime: 0,
averageWriteTime: 0,
};
private totalReadTime = 0;
private totalWriteTime = 0;
private cleanupInterval: NodeJS.Timeout | null = null;
constructor(config: PoolConfig) {
super();
this.config = {
dbPath: config.dbPath,
maxReadConnections: config.maxReadConnections ?? 4,
idleTimeout: config.idleTimeout ?? 60000,
monitoring: config.monitoring ?? true,
};
}
/**
* Initialize the connection pool
*/
initialize(): void {
// Create write connection with WAL mode
this.writeConnection = this.createConnection(false);
// Pre-create one read connection
this.createReadConnection();
// Start cleanup interval
if (this.config.monitoring) {
this.cleanupInterval = setInterval(() => {
this.cleanupIdleConnections();
}, this.config.idleTimeout);
}
this.emit('initialized', { maxReadConnections: this.config.maxReadConnections });
}
/**
* Execute a read query using a connection from the pool
*/
async read<T = unknown>(
sql: string,
params?: unknown[]
): Promise<T[]> {
const connection = await this.acquireReadConnection();
const startTime = Date.now();
try {
const stmt = connection.prepare(sql);
const results = params ? stmt.all(...params) : stmt.all();
// Update stats
const duration = Date.now() - startTime;
this.updateReadStats(duration);
return results as T[];
} finally {
this.releaseReadConnection(connection);
}
}
/**
* Execute a read query and return the first result
*/
async readOne<T = unknown>(
sql: string,
params?: unknown[]
): Promise<T | null> {
const results = await this.read<T>(sql, params);
return results.length > 0 ? results[0] : null;
}
/**
* Execute a write query using the write connection
*/
write(
sql: string,
params?: unknown[]
): Database.RunResult {
if (!this.writeConnection) {
throw new Error('Connection pool not initialized');
}
const startTime = Date.now();
try {
const stmt = this.writeConnection.prepare(sql);
const result = params ? stmt.run(...params) : stmt.run();
// Update stats
const duration = Date.now() - startTime;
this.updateWriteStats(duration);
return result;
} catch (error) {
this.emit('write-error', { sql, error });
throw error;
}
}
/**
* Execute multiple write operations in a transaction
*/
writeTransaction(
operations: Array<{ sql: string; params?: unknown[] }>
): void {
if (!this.writeConnection) {
throw new Error('Connection pool not initialized');
}
const startTime = Date.now();
try {
const transaction = this.writeConnection.transaction(() => {
for (const op of operations) {
const stmt = this.writeConnection!.prepare(op.sql);
if (op.params) {
stmt.run(...op.params);
} else {
stmt.run();
}
}
});
transaction();
// Update stats
const duration = Date.now() - startTime;
this.updateWriteStats(duration);
this.emit('transaction-complete', {
operations: operations.length,
duration,
});
} catch (error) {
this.emit('transaction-error', { operations, error });
throw error;
}
}
/**
* Get pool statistics
*/
getStats(): PoolStats {
this.stats.activeReadConnections = this.readConnections.filter(c => c.inUse).length;
this.stats.idleReadConnections = this.readConnections.filter(c => !c.inUse).length;
return { ...this.stats };
}
/**
* Close all connections in the pool
*/
close(): void {
// Clear cleanup interval
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
this.cleanupInterval = null;
}
// Close all read connections
for (const pooledConn of this.readConnections) {
try {
pooledConn.connection.close();
} catch (error) {
this.emit('error', { type: 'close-read-connection', error });
}
}
this.readConnections = [];
// Close write connection
if (this.writeConnection) {
try {
this.writeConnection.close();
this.writeConnection = null;
} catch (error) {
this.emit('error', { type: 'close-write-connection', error });
}
}
this.emit('closed');
}
/**
* Create a new database connection
*/
private createConnection(readOnly: boolean): Database.Database {
const connection = new Database(this.config.dbPath, {
readonly: readOnly,
});
// Apply optimizations
if (!readOnly) {
// Write connection optimizations
connection.pragma('journal_mode = WAL');
connection.pragma('foreign_keys = ON');
connection.pragma('synchronous = NORMAL');
connection.pragma('temp_store = MEMORY');
connection.pragma('mmap_size = 30000000000');
connection.pragma('page_size = 4096');
connection.pragma('cache_size = -64000');
connection.pragma('busy_timeout = 5000');
} else {
// Read-only connection optimizations
connection.pragma('temp_store = MEMORY');
connection.pragma('cache_size = -32000'); // 32MB cache for reads
connection.pragma('mmap_size = 30000000000');
connection.pragma('page_size = 4096');
connection.pragma('locking_mode = NORMAL');
connection.pragma('read_uncommitted = true'); // Allow dirty reads
}
return connection;
}
/**
* Create a new read connection and add it to the pool
*/
private createReadConnection(): PooledConnection {
const connection = this.createConnection(true);
const pooledConn: PooledConnection = {
connection,
inUse: false,
lastUsed: Date.now(),
queryCount: 0,
};
this.readConnections.push(pooledConn);
this.emit('read-connection-created', {
total: this.readConnections.length,
});
return pooledConn;
}
/**
* Acquire a read connection from the pool
*/
private async acquireReadConnection(): Promise<Database.Database> {
// Try to find an idle connection
const idle = this.readConnections.find(c => !c.inUse);
if (idle) {
idle.inUse = true;
idle.lastUsed = Date.now();
idle.queryCount++;
return idle.connection;
}
// Create a new connection if we haven't reached the limit
if (this.readConnections.length < this.config.maxReadConnections) {
const newConn = this.createReadConnection();
newConn.inUse = true;
newConn.lastUsed = Date.now();
newConn.queryCount++;
return newConn.connection;
}
// Wait for a connection to become available
return new Promise((resolve) => {
const checkInterval = setInterval(() => {
const available = this.readConnections.find(c => !c.inUse);
if (available) {
clearInterval(checkInterval);
available.inUse = true;
available.lastUsed = Date.now();
available.queryCount++;
resolve(available.connection);
}
}, 10);
});
}
/**
* Release a read connection back to the pool
*/
private releaseReadConnection(connection: Database.Database): void {
const pooledConn = this.readConnections.find(c => c.connection === connection);
if (pooledConn) {
pooledConn.inUse = false;
pooledConn.lastUsed = Date.now();
}
}
/**
* Clean up idle connections that haven't been used recently
*/
private cleanupIdleConnections(): void {
const now = Date.now();
const threshold = now - this.config.idleTimeout;
// Keep at least one read connection
if (this.readConnections.length <= 1) return;
const toRemove = this.readConnections.filter(
c => !c.inUse && c.lastUsed < threshold
);
for (const pooledConn of toRemove) {
try {
pooledConn.connection.close();
const index = this.readConnections.indexOf(pooledConn);
if (index > -1) {
this.readConnections.splice(index, 1);
}
this.emit('connection-cleaned', {
lastUsed: new Date(pooledConn.lastUsed),
queryCount: pooledConn.queryCount,
});
} catch (error) {
this.emit('error', { type: 'cleanup-connection', error });
}
}
}
/**
* Update read statistics
*/
private updateReadStats(duration: number): void {
this.stats.totalReadQueries++;
this.totalReadTime += duration;
this.stats.averageReadTime = this.totalReadTime / this.stats.totalReadQueries;
}
/**
* Update write statistics
*/
private updateWriteStats(duration: number): void {
this.stats.totalWriteQueries++;
this.totalWriteTime += duration;
this.stats.averageWriteTime = this.totalWriteTime / this.stats.totalWriteQueries;
}
}