Multi-thread support for applyTemplate
This commit is contained in:
parent
f7e84e3ee7
commit
bfb07c633d
10 changed files with 1260 additions and 154 deletions
35
lib/docx-pool.ts
Normal file
35
lib/docx-pool.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import Piscina from 'piscina';
|
||||
import path from 'node:path';
|
||||
import { availableParallelism } from 'node:os';
|
||||
|
||||
const configuredThreads = Number(
|
||||
process.env.DOCX_WORKERS || 4
|
||||
);
|
||||
|
||||
const maxThreads = Math.max(
|
||||
1,
|
||||
Math.min(
|
||||
Number.isFinite(configuredThreads) && configuredThreads > 0
|
||||
? Math.floor(configuredThreads)
|
||||
: 4,
|
||||
Math.max(1, availableParallelism() - 1)
|
||||
)
|
||||
);
|
||||
|
||||
const globalForDocx = globalThis as typeof globalThis & {
|
||||
docxPool?: Piscina;
|
||||
};
|
||||
|
||||
export const docxPool =
|
||||
globalForDocx.docxPool ??=
|
||||
new Piscina({
|
||||
filename: path.resolve(
|
||||
process.cwd(),
|
||||
'workers/dist/docx.worker.mjs'
|
||||
),
|
||||
minThreads: 1,
|
||||
maxThreads,
|
||||
idleTimeout: 30000,
|
||||
maxQueue: 'auto',
|
||||
concurrentTasksPerWorker: 1
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue