Skip to content

Commit 53248e8

Browse files
committed
Connection pool with workers; initial concurrency test.
1 parent 87d2ce2 commit 53248e8

4 files changed

Lines changed: 96 additions & 4 deletions

File tree

packages/driver-tests/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './driver-tests.js';
2+
export * from './test.js';

packages/wa-sqlite-driver/src/pool.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { SqliteDriverConnectionPool } from '@sqlite-js/driver';
2-
import { LazyConnectionPool } from '@sqlite-js/driver/util';
2+
import {
3+
LazyConnectionPool,
4+
MultiConnectionPool
5+
} from '@sqlite-js/driver/util';
6+
import {
7+
ReserveConnectionOptions,
8+
SqliteDriverConnection
9+
} from '@sqlite-js/driver';
10+
311
import { WorkerDriverConnection } from './worker_threads';
412
// import { WaSqliteConnection } from './wa-sqlite-driver';
513

@@ -9,7 +17,7 @@ import { WorkerDriverConnection } from './worker_threads';
917
// });
1018
// }
1119

12-
export function waSqliteWorkerPool(path: string): SqliteDriverConnectionPool {
20+
export function waSqliteSingleWorker(path: string): SqliteDriverConnectionPool {
1321
return new LazyConnectionPool(async () => {
1422
const connection = new WorkerDriverConnection(
1523
new Worker(new URL('./wa-sqlite-worker.js', import.meta.url), {
@@ -22,3 +30,23 @@ export function waSqliteWorkerPool(path: string): SqliteDriverConnectionPool {
2230
// return await WaSqliteConnection.open(path);
2331
});
2432
}
33+
34+
export function waSqliteWorkerPool(path: string): SqliteDriverConnectionPool {
35+
return new MultiConnectionPool(
36+
{
37+
async openConnection(
38+
options?: ReserveConnectionOptions & { connectionName?: string }
39+
): Promise<SqliteDriverConnection> {
40+
const connection = new WorkerDriverConnection(
41+
new Worker(new URL('./wa-sqlite-worker.js', import.meta.url), {
42+
type: 'module'
43+
}),
44+
{ path }
45+
);
46+
await connection.open();
47+
return connection;
48+
}
49+
},
50+
{}
51+
);
52+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { beforeEach, describe, expect, test } from '@sqlite-js/driver-tests';
2+
import { waSqliteSingleWorker, waSqliteWorkerPool } from '../../lib/index.js';
3+
4+
describe('concurrency tests', () => {
5+
let dbPath: string;
6+
7+
const open = async () => {
8+
const db = await waSqliteWorkerPool(dbPath);
9+
return db;
10+
};
11+
12+
beforeEach((context) => {
13+
let testNameSanitized = context.fullName.replaceAll(
14+
/[\s\/\\>\.\-\:]+/g,
15+
'_'
16+
);
17+
18+
if (testNameSanitized.length > 10) {
19+
testNameSanitized =
20+
testNameSanitized.substring(testNameSanitized.length - 7) +
21+
String(Math.random()).substring(1, 4);
22+
}
23+
dbPath = `test-db/${testNameSanitized}.db`;
24+
});
25+
26+
test('concurrent select', async () => {
27+
await using driver = await open();
28+
{
29+
await using connection = await driver.reserveConnection();
30+
using s1 = connection.prepare(
31+
'create table test_data(id integer primary key, data text)'
32+
);
33+
await s1.step();
34+
using s2 = connection.prepare(
35+
"insert into test_data(data) values('test')"
36+
);
37+
await s2.step();
38+
}
39+
40+
let promises: Promise<void>[] = [];
41+
42+
for (let i = 0; i < 5; i++) {
43+
const p = (async () => {
44+
const start = Date.now();
45+
await using connection = await driver.reserveConnection();
46+
47+
using b = connection.prepare('begin immediate');
48+
await b.step();
49+
using s = connection.prepare('select * from test_data');
50+
const { rows } = await s.step();
51+
52+
expect(rows).toEqual([{ id: 1, data: 'test' }]);
53+
await new Promise((resolve) => setTimeout(resolve, 500));
54+
55+
using e = connection.prepare('commit');
56+
await e.step();
57+
console.log('tx done in', Date.now() - start);
58+
})();
59+
promises.push(p);
60+
}
61+
await Promise.all(promises);
62+
});
63+
});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { describeDriverTests } from '@sqlite-js/driver-tests';
2-
import { waSqliteWorkerPool } from '../../lib/index.js';
2+
import { waSqliteSingleWorker } from '../../lib/index.js';
33

44
describeDriverTests(
55
'wa-sqlite',
66
{ getColumns: true, rawResults: true, allowsMissingParameters: false },
77
async (path) => {
88
console.log('open', path);
9-
return waSqliteWorkerPool(path);
9+
return waSqliteSingleWorker(path);
1010
}
1111
);

0 commit comments

Comments
 (0)