Skip to content

Commit

Permalink
Use mutex instead of queue
Browse files Browse the repository at this point in the history
  • Loading branch information
DallasHoff committed Jul 22, 2024
1 parent 6ae283e commit ab928b4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { sqlTag } from './lib/sql-tag.js';
import { convertRowsToObjects } from './lib/convert-rows-to-objects.js';

export class SQLocal {
protected databasePath: string;
protected config: ClientConfig;
protected worker: Worker;
protected proxy: WorkerProxy;
protected isWorkerDestroyed: boolean = false;
Expand All @@ -48,7 +48,7 @@ export class SQLocal {
this.worker.addEventListener('message', this.processMessageEvent);

this.proxy = coincident(this.worker) as WorkerProxy;
this.databasePath = config.databasePath;
this.config = config;
this.worker.postMessage({
type: 'config',
config,
Expand Down Expand Up @@ -257,7 +257,9 @@ export class SQLocal {
};

getDatabaseFile = async (): Promise<File> => {
const path = this.databasePath.split(/[\\/]/).filter((part) => part !== '');
const path = this.config.databasePath
.split(/[\\/]/)
.filter((part) => part !== '');
const fileName = path.pop();

if (!fileName) {
Expand Down
28 changes: 28 additions & 0 deletions src/lib/create-mutex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
type Mutex = {
lock: () => Promise<void>;
unlock: () => Promise<void>;
};

export function createMutex(): Mutex {
let promise: Promise<void> | undefined;
let resolve: (() => void) | undefined;

const lock = async () => {
while (promise) {
await promise;
}

promise = new Promise((res) => {
resolve = res;
});
};

const unlock = async () => {
const res = resolve;
promise = undefined;
resolve = undefined;
res?.();
};

return { lock, unlock };
}
26 changes: 11 additions & 15 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import type {
Sqlite3StorageType,
ConfigMessage,
} from './types.js';
import { createMutex } from './lib/create-mutex.js';

export class SQLocalProcessor {
protected proxy: WorkerProxy;
protected sqlite3: Sqlite3 | undefined;
protected db: Sqlite3Db | undefined;
protected dbStorageType: Sqlite3StorageType | undefined;
protected config: ProcessorConfig = {};
protected queuedMessages: InputMessage[] = [];
protected initMutex = createMutex();
protected userFunctions = new Map<string, UserFunction>();

onmessage: ((message: OutputMessage) => void) | undefined;
Expand All @@ -39,6 +40,8 @@ export class SQLocalProcessor {
protected init = async (): Promise<void> => {
if (!this.config.databasePath) return;

await this.initMutex.lock();

const { databasePath, readOnly, verbose } = this.config;
const flags = [
readOnly === true ? 'r' : 'cw',
Expand Down Expand Up @@ -76,18 +79,17 @@ export class SQLocalProcessor {
}

this.userFunctions.forEach(this.initUserFunction);
this.flushQueue();
await this.initMutex.unlock();
};

postMessage = (message: InputMessage | MessageEvent<InputMessage>): void => {
postMessage = async (
message: InputMessage | MessageEvent<InputMessage>
): Promise<void> => {
if (message instanceof MessageEvent) {
message = message.data;
}

if (!this.db && message.type !== 'config') {
this.queuedMessages.push(message);
return;
}
await this.initMutex.lock();

switch (message.type) {
case 'config':
Expand All @@ -110,6 +112,8 @@ export class SQLocalProcessor {
this.destroy(message);
break;
}

await this.initMutex.unlock();
};

protected emitMessage = (message: OutputMessage): void => {
Expand Down Expand Up @@ -332,14 +336,6 @@ export class SQLocalProcessor {
}
};

protected flushQueue = (): void => {
while (this.queuedMessages.length > 0) {
const message = this.queuedMessages.shift();
if (message === undefined) continue;
this.postMessage(message);
}
};

protected destroy = (message?: DestroyMessage): void => {
if (this.db) {
this.db.exec({ sql: 'PRAGMA optimize' });
Expand Down

0 comments on commit ab928b4

Please sign in to comment.