Parallelize SQL queries
This commit is contained in:
parent
86b4da1ec2
commit
c64762858e
178 changed files with 3377 additions and 3618 deletions
|
@ -5,13 +5,21 @@ import { ipcRenderer } from 'electron';
|
|||
import * as log from '../logging/log';
|
||||
import createTaskWithTimeout from '../textsecure/TaskWithTimeout';
|
||||
import { explodePromise } from '../util/explodePromise';
|
||||
import { missingCaseError } from '../util/missingCaseError';
|
||||
|
||||
const SQL_CHANNEL_KEY = 'sql-channel';
|
||||
const SQL_READ_KEY = 'sql-channel:read';
|
||||
const SQL_WRITE_KEY = 'sql-channel:write';
|
||||
let activeJobCount = 0;
|
||||
let resolveShutdown: (() => void) | undefined;
|
||||
let shutdownPromise: Promise<void> | null = null;
|
||||
|
||||
export enum AccessType {
|
||||
Read = 'Read',
|
||||
Write = 'Write',
|
||||
}
|
||||
|
||||
export async function ipcInvoke<T>(
|
||||
access: AccessType,
|
||||
name: string,
|
||||
args: ReadonlyArray<unknown>
|
||||
): Promise<T> {
|
||||
|
@ -19,21 +27,31 @@ export async function ipcInvoke<T>(
|
|||
|
||||
if (shutdownPromise && name !== 'close') {
|
||||
throw new Error(
|
||||
`Rejecting SQL channel job (${fnName}); application is shutting down`
|
||||
`Rejecting SQL channel job (${access}, ${fnName}); ` +
|
||||
'application is shutting down'
|
||||
);
|
||||
}
|
||||
|
||||
let channel: string;
|
||||
if (access === AccessType.Read) {
|
||||
channel = SQL_READ_KEY;
|
||||
} else if (access === AccessType.Write) {
|
||||
channel = SQL_WRITE_KEY;
|
||||
} else {
|
||||
throw missingCaseError(access);
|
||||
}
|
||||
|
||||
activeJobCount += 1;
|
||||
return createTaskWithTimeout(async () => {
|
||||
try {
|
||||
return await ipcRenderer.invoke(SQL_CHANNEL_KEY, name, ...args);
|
||||
return await ipcRenderer.invoke(channel, name, ...args);
|
||||
} finally {
|
||||
activeJobCount -= 1;
|
||||
if (activeJobCount === 0) {
|
||||
resolveShutdown?.();
|
||||
}
|
||||
}
|
||||
}, `SQL channel call (${fnName})`)();
|
||||
}, `SQL channel call (${access}, ${fnName})`)();
|
||||
}
|
||||
|
||||
export async function doShutdown(): Promise<void> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue