Primeiro commit do projeto Angular
This commit is contained in:
+20
@@ -0,0 +1,20 @@
|
||||
interface AbortSignalEventTargetAddOptions {
|
||||
once: boolean;
|
||||
}
|
||||
export interface AbortSignalEventTarget {
|
||||
addEventListener: (name: 'abort', listener: () => void, options?: AbortSignalEventTargetAddOptions) => void;
|
||||
removeEventListener: (name: 'abort', listener: () => void) => void;
|
||||
aborted?: boolean;
|
||||
reason?: unknown;
|
||||
}
|
||||
export interface AbortSignalEventEmitter {
|
||||
off: (name: 'abort', listener: () => void) => void;
|
||||
once: (name: 'abort', listener: () => void) => void;
|
||||
}
|
||||
export type AbortSignalAny = AbortSignalEventTarget | AbortSignalEventEmitter;
|
||||
export declare class AbortError extends Error {
|
||||
constructor(reason?: AbortSignalEventTarget['reason']);
|
||||
get name(): string;
|
||||
}
|
||||
export declare function onabort(abortSignal: AbortSignalAny, listener: () => void): () => void;
|
||||
export {};
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AbortError = void 0;
|
||||
exports.onabort = onabort;
|
||||
class AbortError extends Error {
|
||||
constructor(reason) {
|
||||
// TS does not recognizes the cause clause
|
||||
// @ts-expect-error
|
||||
super('The task has been aborted', { cause: reason });
|
||||
}
|
||||
get name() {
|
||||
return 'AbortError';
|
||||
}
|
||||
}
|
||||
exports.AbortError = AbortError;
|
||||
function onabort(abortSignal, listener) {
|
||||
if ('addEventListener' in abortSignal) {
|
||||
abortSignal.addEventListener('abort', listener, { once: true });
|
||||
return () => abortSignal.removeEventListener('abort', listener);
|
||||
}
|
||||
else {
|
||||
abortSignal.once('abort', listener);
|
||||
return () => abortSignal.removeListener('abort', listener);
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=abort.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"abort.js","sourceRoot":"","sources":["../src/abort.ts"],"names":[],"mappings":";;;AAoCA,0BAQC;AApBD,MAAa,UAAW,SAAQ,KAAK;IACnC,YAAa,MAAyC;QACpD,0CAA0C;QAC1C,mBAAmB;QACnB,KAAK,CAAC,2BAA2B,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;IACxD,CAAC;IAED,IAAI,IAAI;QACN,OAAO,YAAY,CAAC;IACtB,CAAC;CACF;AAVD,gCAUC;AAED,SAAgB,OAAO,CAAE,WAA2B,EAAE,QAAoB;IACxE,IAAI,kBAAkB,IAAI,WAAW,EAAE,CAAC;QACtC,WAAW,CAAC,gBAAgB,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QAChE,OAAO,GAAG,EAAE,CAAC,WAAW,CAAC,mBAAmB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IAClE,CAAC;SAAM,CAAC;QACN,WAAW,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACpC,OAAO,GAAG,EAAE,CAAE,WAA4B,CAAC,cAAc,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IAC/E,CAAC;AACH,CAAC"}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
export declare const READY = "_WORKER_READY";
|
||||
/**
|
||||
* True if the object implements the Transferable interface
|
||||
*
|
||||
* @export
|
||||
* @param {unknown} value
|
||||
* @return {*} {boolean}
|
||||
*/
|
||||
export declare function isTransferable(value: unknown): boolean;
|
||||
/**
|
||||
* True if object implements Transferable and has been returned
|
||||
* by the Piscina.move() function
|
||||
*
|
||||
* TODO: narrow down the type of value
|
||||
* @export
|
||||
* @param {(unknown & PiscinaMovable)} value
|
||||
* @return {*} {boolean}
|
||||
*/
|
||||
export declare function isMovable(value: any): boolean;
|
||||
export declare function markMovable(value: {}): void;
|
||||
export declare const commonState: {
|
||||
isWorkerThread: boolean;
|
||||
workerData: undefined;
|
||||
};
|
||||
export declare function maybeFileURLToPath(filename: string): string;
|
||||
export declare function getAvailableParallelism(): number;
|
||||
export declare function promiseResolvers<T = any>(): {
|
||||
promise: Promise<T>;
|
||||
resolve: (res: T) => void;
|
||||
reject: (err: Error) => void;
|
||||
};
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.commonState = exports.READY = void 0;
|
||||
exports.isTransferable = isTransferable;
|
||||
exports.isMovable = isMovable;
|
||||
exports.markMovable = markMovable;
|
||||
exports.maybeFileURLToPath = maybeFileURLToPath;
|
||||
exports.getAvailableParallelism = getAvailableParallelism;
|
||||
exports.promiseResolvers = promiseResolvers;
|
||||
const node_url_1 = require("node:url");
|
||||
const node_os_1 = require("node:os");
|
||||
const symbols_1 = require("./symbols");
|
||||
// States wether the worker is ready to receive tasks
|
||||
exports.READY = '_WORKER_READY';
|
||||
/**
|
||||
* True if the object implements the Transferable interface
|
||||
*
|
||||
* @export
|
||||
* @param {unknown} value
|
||||
* @return {*} {boolean}
|
||||
*/
|
||||
function isTransferable(value) {
|
||||
return (value != null &&
|
||||
typeof value === 'object' &&
|
||||
symbols_1.kTransferable in value &&
|
||||
symbols_1.kValue in value);
|
||||
}
|
||||
/**
|
||||
* True if object implements Transferable and has been returned
|
||||
* by the Piscina.move() function
|
||||
*
|
||||
* TODO: narrow down the type of value
|
||||
* @export
|
||||
* @param {(unknown & PiscinaMovable)} value
|
||||
* @return {*} {boolean}
|
||||
*/
|
||||
function isMovable(value) {
|
||||
return isTransferable(value) && value[symbols_1.kMovable] === true;
|
||||
}
|
||||
function markMovable(value) {
|
||||
Object.defineProperty(value, symbols_1.kMovable, {
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: true
|
||||
});
|
||||
}
|
||||
// State of Piscina pool
|
||||
exports.commonState = {
|
||||
isWorkerThread: false,
|
||||
workerData: undefined
|
||||
};
|
||||
function maybeFileURLToPath(filename) {
|
||||
return filename.startsWith('file:')
|
||||
? (0, node_url_1.fileURLToPath)(new node_url_1.URL(filename))
|
||||
: filename;
|
||||
}
|
||||
function getAvailableParallelism() {
|
||||
return (0, node_os_1.availableParallelism)();
|
||||
}
|
||||
function promiseResolvers() {
|
||||
// @ts-expect-error - available from v24 onwards
|
||||
if (Promise.withResolvers != null)
|
||||
return Promise.withResolvers();
|
||||
let res;
|
||||
let rej;
|
||||
return {
|
||||
promise: new Promise((resolve, reject) => { res = resolve; rej = reject; }),
|
||||
resolve: res,
|
||||
reject: rej
|
||||
};
|
||||
}
|
||||
//# sourceMappingURL=common.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":";;;AAeA,wCAOC;AAWD,8BAEC;AAED,kCAOC;AAQD,gDAIC;AAED,0DAEC;AAED,4CAgBC;AA9ED,uCAA8C;AAC9C,qCAA+C;AAE/C,uCAA4D;AAE5D,qDAAqD;AACxC,QAAA,KAAK,GAAG,eAAe,CAAC;AAErC;;;;;;GAMG;AACH,SAAgB,cAAc,CAAE,KAAc;IAC5C,OAAO,CACL,KAAK,IAAI,IAAI;QACb,OAAO,KAAK,KAAK,QAAQ;QACzB,uBAAa,IAAI,KAAK;QACtB,gBAAM,IAAI,KAAK,CAChB,CAAC;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,SAAS,CAAE,KAAU;IACnC,OAAO,cAAc,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,kBAAQ,CAAC,KAAK,IAAI,CAAC;AAC3D,CAAC;AAED,SAAgB,WAAW,CAAE,KAAS;IACpC,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,kBAAQ,EAAE;QACrC,UAAU,EAAE,KAAK;QACjB,YAAY,EAAE,IAAI;QAClB,QAAQ,EAAE,IAAI;QACd,KAAK,EAAE,IAAI;KACZ,CAAC,CAAC;AACL,CAAC;AAED,wBAAwB;AACX,QAAA,WAAW,GAAG;IACzB,cAAc,EAAE,KAAK;IACrB,UAAU,EAAE,SAAS;CACtB,CAAC;AAEF,SAAgB,kBAAkB,CAAE,QAAiB;IACnD,OAAO,QAAQ,CAAC,UAAU,CAAC,OAAO,CAAC;QACjC,CAAC,CAAC,IAAA,wBAAa,EAAC,IAAI,cAAG,CAAC,QAAQ,CAAC,CAAC;QAClC,CAAC,CAAC,QAAQ,CAAC;AACf,CAAC;AAED,SAAgB,uBAAuB;IACrC,OAAO,IAAA,8BAAoB,GAAE,CAAC;AAChC,CAAC;AAED,SAAgB,gBAAgB;IAK9B,gDAAgD;IAChD,IAAI,OAAO,CAAC,aAAa,IAAI,IAAI;QAAE,OAAO,OAAO,CAAC,aAAa,EAAE,CAAC;IAElE,IAAI,GAAqB,CAAC;IAC1B,IAAI,GAAyB,CAAA;IAE7B,OAAO;QACL,OAAO,EAAE,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,GAAG,GAAG,GAAG,OAAO,CAAC,CAAC,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC,CAAE;QAC/E,OAAO,EAAE,GAAI;QACb,MAAM,EAAE,GAAI;KACb,CAAA;AACH,CAAC"}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
export declare const Errors: {
|
||||
ThreadTermination: () => Error;
|
||||
FilenameNotProvided: () => Error;
|
||||
TaskQueueAtLimit: () => Error;
|
||||
NoTaskQueueAvailable: () => Error;
|
||||
CloseTimeout: () => Error;
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.Errors = void 0;
|
||||
exports.Errors = {
|
||||
ThreadTermination: () => new Error('Terminating worker thread'),
|
||||
FilenameNotProvided: () => new Error('filename must be provided to run() or in options object'),
|
||||
TaskQueueAtLimit: () => new Error('Task queue is at limit'),
|
||||
NoTaskQueueAvailable: () => new Error('No task queue available and all Workers are busy'),
|
||||
CloseTimeout: () => new Error('Close operation timed out')
|
||||
};
|
||||
//# sourceMappingURL=errors.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAa,QAAA,MAAM,GAAG;IACpB,iBAAiB,EAAE,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,2BAA2B,CAAC;IAC/D,mBAAmB,EAAE,GAAG,EAAE,CACxB,IAAI,KAAK,CAAC,yDAAyD,CAAC;IACtE,gBAAgB,EAAE,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,wBAAwB,CAAC;IAC3D,oBAAoB,EAAE,GAAG,EAAE,CACzB,IAAI,KAAK,CAAC,kDAAkD,CAAC;IAC/D,YAAY,EAAE,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,2BAA2B,CAAC;CAC3D,CAAC"}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import mod from "./main.js";
|
||||
|
||||
export default mod;
|
||||
export const ArrayTaskQueue = mod.ArrayTaskQueue;
|
||||
export const FixedQueue = mod.FixedQueue;
|
||||
export const Piscina = mod.Piscina;
|
||||
export const isWorkerThread = mod.isWorkerThread;
|
||||
export const move = mod.move;
|
||||
export const queueOptionsSymbol = mod.queueOptionsSymbol;
|
||||
export const transferableSymbol = mod.transferableSymbol;
|
||||
export const valueSymbol = mod.valueSymbol;
|
||||
export const version = mod.version;
|
||||
export const workerData = mod.workerData;
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
import { RecordableHistogram } from 'node:perf_hooks';
|
||||
export type PiscinaHistogramSummary = {
|
||||
average: number;
|
||||
mean: number;
|
||||
stddev: number;
|
||||
min: number;
|
||||
max: number;
|
||||
p0_001: number;
|
||||
p0_01: number;
|
||||
p0_1: number;
|
||||
p1: number;
|
||||
p2_5: number;
|
||||
p10: number;
|
||||
p25: number;
|
||||
p50: number;
|
||||
p75: number;
|
||||
p90: number;
|
||||
p97_5: number;
|
||||
p99: number;
|
||||
p99_9: number;
|
||||
p99_99: number;
|
||||
p99_999: number;
|
||||
};
|
||||
export type PiscinaHistogram = {
|
||||
runTime: PiscinaHistogramSummary;
|
||||
waitTime: PiscinaHistogramSummary;
|
||||
resetRunTime(): void;
|
||||
resetWaitTime(): void;
|
||||
};
|
||||
export declare class PiscinaHistogramHandler {
|
||||
#private;
|
||||
constructor();
|
||||
get runTimeSummary(): PiscinaHistogramSummary;
|
||||
get waitTimeSummary(): PiscinaHistogramSummary;
|
||||
get runTimeCount(): number;
|
||||
recordRunTime(value: number): void;
|
||||
recordWaitTime(value: number): void;
|
||||
resetWaitTime(): void;
|
||||
resetRunTime(): void;
|
||||
static createHistogramSummary(histogram: RecordableHistogram): PiscinaHistogramSummary;
|
||||
static toHistogramIntegerNano(milliseconds: number): number;
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
"use strict";
|
||||
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
||||
if (kind === "m") throw new TypeError("Private method is not writable");
|
||||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
||||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
||||
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
||||
};
|
||||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
||||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
||||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
||||
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
||||
};
|
||||
var _PiscinaHistogramHandler_runTime, _PiscinaHistogramHandler_waitTime;
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.PiscinaHistogramHandler = void 0;
|
||||
const node_perf_hooks_1 = require("node:perf_hooks");
|
||||
class PiscinaHistogramHandler {
|
||||
constructor() {
|
||||
_PiscinaHistogramHandler_runTime.set(this, void 0);
|
||||
_PiscinaHistogramHandler_waitTime.set(this, void 0);
|
||||
__classPrivateFieldSet(this, _PiscinaHistogramHandler_runTime, (0, node_perf_hooks_1.createHistogram)(), "f");
|
||||
__classPrivateFieldSet(this, _PiscinaHistogramHandler_waitTime, (0, node_perf_hooks_1.createHistogram)(), "f");
|
||||
}
|
||||
get runTimeSummary() {
|
||||
return PiscinaHistogramHandler.createHistogramSummary(__classPrivateFieldGet(this, _PiscinaHistogramHandler_runTime, "f"));
|
||||
}
|
||||
get waitTimeSummary() {
|
||||
return PiscinaHistogramHandler.createHistogramSummary(__classPrivateFieldGet(this, _PiscinaHistogramHandler_waitTime, "f"));
|
||||
}
|
||||
get runTimeCount() {
|
||||
return __classPrivateFieldGet(this, _PiscinaHistogramHandler_runTime, "f").count;
|
||||
}
|
||||
recordRunTime(value) {
|
||||
__classPrivateFieldGet(this, _PiscinaHistogramHandler_runTime, "f").record(PiscinaHistogramHandler.toHistogramIntegerNano(value));
|
||||
}
|
||||
recordWaitTime(value) {
|
||||
__classPrivateFieldGet(this, _PiscinaHistogramHandler_waitTime, "f").record(PiscinaHistogramHandler.toHistogramIntegerNano(value));
|
||||
}
|
||||
resetWaitTime() {
|
||||
__classPrivateFieldGet(this, _PiscinaHistogramHandler_waitTime, "f").reset();
|
||||
}
|
||||
resetRunTime() {
|
||||
__classPrivateFieldGet(this, _PiscinaHistogramHandler_runTime, "f").reset();
|
||||
}
|
||||
static createHistogramSummary(histogram) {
|
||||
const { mean, stddev, min, max } = histogram;
|
||||
return {
|
||||
average: mean / 1000,
|
||||
mean: mean / 1000,
|
||||
stddev: stddev / 1000,
|
||||
min: min / 1000,
|
||||
max: max / 1000,
|
||||
p0_001: histogram.percentile(0.001) / 1000,
|
||||
p0_01: histogram.percentile(0.01) / 1000,
|
||||
p0_1: histogram.percentile(0.1) / 1000,
|
||||
p1: histogram.percentile(1) / 1000,
|
||||
p2_5: histogram.percentile(2.5) / 1000,
|
||||
p10: histogram.percentile(10) / 1000,
|
||||
p25: histogram.percentile(25) / 1000,
|
||||
p50: histogram.percentile(50) / 1000,
|
||||
p75: histogram.percentile(75) / 1000,
|
||||
p90: histogram.percentile(90) / 1000,
|
||||
p97_5: histogram.percentile(97.5) / 1000,
|
||||
p99: histogram.percentile(99) / 1000,
|
||||
p99_9: histogram.percentile(99.9) / 1000,
|
||||
p99_99: histogram.percentile(99.99) / 1000,
|
||||
p99_999: histogram.percentile(99.999) / 1000,
|
||||
};
|
||||
}
|
||||
static toHistogramIntegerNano(milliseconds) {
|
||||
return Math.max(1, Math.trunc(milliseconds * 1000));
|
||||
}
|
||||
}
|
||||
exports.PiscinaHistogramHandler = PiscinaHistogramHandler;
|
||||
_PiscinaHistogramHandler_runTime = new WeakMap(), _PiscinaHistogramHandler_waitTime = new WeakMap();
|
||||
//# sourceMappingURL=histogram.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"histogram.js","sourceRoot":"","sources":["../src/histogram.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qDAAuE;AAgCvE,MAAa,uBAAuB;IAIlC;QAHA,mDAA8B;QAC9B,oDAA+B;QAG7B,uBAAA,IAAI,oCAAY,IAAA,iCAAe,GAAE,MAAA,CAAC;QAClC,uBAAA,IAAI,qCAAa,IAAA,iCAAe,GAAE,MAAA,CAAC;IACrC,CAAC;IAED,IAAI,cAAc;QAChB,OAAO,uBAAuB,CAAC,sBAAsB,CAAC,uBAAA,IAAI,wCAAS,CAAC,CAAC;IACvE,CAAC;IAED,IAAI,eAAe;QACjB,OAAO,uBAAuB,CAAC,sBAAsB,CAAC,uBAAA,IAAI,yCAAU,CAAC,CAAC;IACxE,CAAC;IAED,IAAI,YAAY;QACd,OAAO,uBAAA,IAAI,wCAAS,CAAC,KAAK,CAAC;IAC7B,CAAC;IAED,aAAa,CAAC,KAAa;QACzB,uBAAA,IAAI,wCAAS,CAAC,MAAM,CAAC,uBAAuB,CAAC,sBAAsB,CAAC,KAAK,CAAC,CA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AAS,CAAC,UAAU,CAAC,EAAE,CAAC,GAAG,IAAI;YACpC,KAAK,EAAE,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,IAAI;YACxC,GAAG,EAAE,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,GAAG,IAAI;YACpC,KAAK,EAAE,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,IAAI;YACxC,MAAM,EAAE,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,IAAI;YAC1C,OAAO,EAAE,SAAS,CAAC,UAAU,CAAC,MAAM,CAAC,GAAG,IAAI;SAC7C,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,sBAAsB,CAAC,YAAoB;QAChD,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,IAAI,CAAC,CAAC,CAAC;IACtD,CAAC;CACF;AAvED,0DAuEC"}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
import { Worker, MessagePort } from 'node:worker_threads';
|
||||
import { EventEmitterAsyncResource } from 'node:events';
|
||||
import { version } from '../package.json';
|
||||
import type { Transferable, ResourceLimits, EnvSpecifier } from './types';
|
||||
import { kQueueOptions, kTransferable, kValue } from './symbols';
|
||||
import { TaskQueue, ArrayTaskQueue, FixedQueue, PiscinaTask, TransferList, TransferListItem } from './task_queue';
|
||||
import { PiscinaLoadBalancer } from './worker_pool';
|
||||
import { AbortSignalAny } from './abort';
|
||||
import { PiscinaHistogram } from './histogram';
|
||||
interface Options {
|
||||
filename?: string | null;
|
||||
name?: string;
|
||||
minThreads?: number;
|
||||
maxThreads?: number;
|
||||
idleTimeout?: number;
|
||||
maxQueue?: number | 'auto';
|
||||
concurrentTasksPerWorker?: number;
|
||||
atomics?: 'sync' | 'async' | 'disabled';
|
||||
resourceLimits?: ResourceLimits;
|
||||
argv?: string[];
|
||||
execArgv?: string[];
|
||||
env?: EnvSpecifier;
|
||||
workerData?: any;
|
||||
taskQueue?: TaskQueue;
|
||||
niceIncrement?: number;
|
||||
trackUnmanagedFds?: boolean;
|
||||
closeTimeout?: number;
|
||||
recordTiming?: boolean;
|
||||
loadBalancer?: PiscinaLoadBalancer;
|
||||
workerHistogram?: boolean;
|
||||
}
|
||||
interface FilledOptions extends Options {
|
||||
filename: string | null;
|
||||
name: string;
|
||||
minThreads: number;
|
||||
maxThreads: number;
|
||||
idleTimeout: number;
|
||||
maxQueue: number;
|
||||
concurrentTasksPerWorker: number;
|
||||
atomics: Options['atomics'];
|
||||
taskQueue: TaskQueue;
|
||||
niceIncrement: number;
|
||||
closeTimeout: number;
|
||||
recordTiming: boolean;
|
||||
workerHistogram: boolean;
|
||||
}
|
||||
interface RunOptions {
|
||||
transferList?: TransferList;
|
||||
filename?: string | null;
|
||||
signal?: AbortSignalAny | null;
|
||||
name?: string | null;
|
||||
}
|
||||
interface CloseOptions {
|
||||
force?: boolean;
|
||||
}
|
||||
export default class Piscina<T = any, R = any> extends EventEmitterAsyncResource {
|
||||
#private;
|
||||
constructor(options?: Options);
|
||||
run(task: T, options?: RunOptions): Promise<R>;
|
||||
close(options?: CloseOptions): Promise<void>;
|
||||
destroy(): Promise<void>;
|
||||
[Symbol.dispose](): void;
|
||||
[Symbol.asyncDispose](): Promise<void>;
|
||||
get maxThreads(): number;
|
||||
get minThreads(): number;
|
||||
get options(): FilledOptions;
|
||||
get threads(): Worker[];
|
||||
get queueSize(): number;
|
||||
get completed(): number;
|
||||
get histogram(): PiscinaHistogram;
|
||||
get utilization(): number;
|
||||
get duration(): number;
|
||||
get needsDrain(): boolean;
|
||||
static get isWorkerThread(): boolean;
|
||||
static get workerData(): any;
|
||||
static get version(): string;
|
||||
static get Piscina(): typeof Piscina;
|
||||
static get FixedQueue(): typeof FixedQueue;
|
||||
static get ArrayTaskQueue(): typeof ArrayTaskQueue;
|
||||
static move(val: Transferable | TransferListItem | ArrayBufferView | ArrayBuffer | MessagePort): ArrayBuffer | ArrayBufferView<ArrayBufferLike> | MessagePort | Transferable;
|
||||
static get transferableSymbol(): symbol;
|
||||
static get valueSymbol(): symbol;
|
||||
static get queueOptionsSymbol(): symbol;
|
||||
}
|
||||
export declare const move: typeof Piscina.move;
|
||||
export declare const isWorkerThread: boolean;
|
||||
export declare const workerData: any;
|
||||
export { Piscina, PiscinaTask, TaskQueue, kTransferable as transferableSymbol, kValue as valueSymbol, kQueueOptions as queueOptionsSymbol, version, FixedQueue, ArrayTaskQueue, };
|
||||
+779
@@ -0,0 +1,779 @@
|
||||
"use strict";
|
||||
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
||||
if (kind === "m") throw new TypeError("Private method is not writable");
|
||||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
||||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
||||
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
||||
};
|
||||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
||||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
||||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
||||
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
||||
};
|
||||
var _DirectlyTransferable_value, _ArrayBufferViewTransferable_view, _Piscina_pool, _Piscina_histogram;
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ArrayTaskQueue = exports.FixedQueue = exports.version = exports.queueOptionsSymbol = exports.valueSymbol = exports.transferableSymbol = exports.Piscina = exports.workerData = exports.isWorkerThread = exports.move = void 0;
|
||||
const node_worker_threads_1 = require("node:worker_threads");
|
||||
const node_events_1 = require("node:events");
|
||||
const node_path_1 = require("node:path");
|
||||
const node_util_1 = require("node:util");
|
||||
const node_perf_hooks_1 = require("node:perf_hooks");
|
||||
const promises_1 = require("node:timers/promises");
|
||||
const package_json_1 = require("../package.json");
|
||||
Object.defineProperty(exports, "version", { enumerable: true, get: function () { return package_json_1.version; } });
|
||||
const symbols_1 = require("./symbols");
|
||||
Object.defineProperty(exports, "queueOptionsSymbol", { enumerable: true, get: function () { return symbols_1.kQueueOptions; } });
|
||||
Object.defineProperty(exports, "transferableSymbol", { enumerable: true, get: function () { return symbols_1.kTransferable; } });
|
||||
Object.defineProperty(exports, "valueSymbol", { enumerable: true, get: function () { return symbols_1.kValue; } });
|
||||
const task_queue_1 = require("./task_queue");
|
||||
Object.defineProperty(exports, "ArrayTaskQueue", { enumerable: true, get: function () { return task_queue_1.ArrayTaskQueue; } });
|
||||
Object.defineProperty(exports, "FixedQueue", { enumerable: true, get: function () { return task_queue_1.FixedQueue; } });
|
||||
const worker_pool_1 = require("./worker_pool");
|
||||
const abort_1 = require("./abort");
|
||||
const histogram_1 = require("./histogram");
|
||||
const errors_1 = require("./errors");
|
||||
const common_1 = require("./common");
|
||||
const cpuParallelism = (0, common_1.getAvailableParallelism)();
|
||||
const kDefaultOptions = {
|
||||
filename: null,
|
||||
name: 'default',
|
||||
minThreads: Math.max(Math.floor(cpuParallelism / 2), 1),
|
||||
maxThreads: cpuParallelism * 1.5,
|
||||
idleTimeout: 0,
|
||||
maxQueue: Infinity,
|
||||
concurrentTasksPerWorker: 1,
|
||||
atomics: 'sync',
|
||||
taskQueue: new task_queue_1.ArrayTaskQueue(),
|
||||
niceIncrement: 0,
|
||||
trackUnmanagedFds: true,
|
||||
closeTimeout: 30000,
|
||||
recordTiming: true,
|
||||
workerHistogram: false
|
||||
};
|
||||
const kDefaultRunOptions = {
|
||||
transferList: undefined,
|
||||
filename: null,
|
||||
signal: null,
|
||||
name: null
|
||||
};
|
||||
const kDefaultCloseOptions = {
|
||||
force: false
|
||||
};
|
||||
class DirectlyTransferable {
|
||||
constructor(value) {
|
||||
_DirectlyTransferable_value.set(this, void 0);
|
||||
__classPrivateFieldSet(this, _DirectlyTransferable_value, value, "f");
|
||||
}
|
||||
get [(_DirectlyTransferable_value = new WeakMap(), symbols_1.kTransferable)]() { return __classPrivateFieldGet(this, _DirectlyTransferable_value, "f"); }
|
||||
get [symbols_1.kValue]() { return __classPrivateFieldGet(this, _DirectlyTransferable_value, "f"); }
|
||||
}
|
||||
class ArrayBufferViewTransferable {
|
||||
constructor(view) {
|
||||
_ArrayBufferViewTransferable_view.set(this, void 0);
|
||||
__classPrivateFieldSet(this, _ArrayBufferViewTransferable_view, view, "f");
|
||||
}
|
||||
get [(_ArrayBufferViewTransferable_view = new WeakMap(), symbols_1.kTransferable)]() { return __classPrivateFieldGet(this, _ArrayBufferViewTransferable_view, "f").buffer; }
|
||||
get [symbols_1.kValue]() { return __classPrivateFieldGet(this, _ArrayBufferViewTransferable_view, "f"); }
|
||||
}
|
||||
class ThreadPool {
|
||||
constructor(publicInterface, options) {
|
||||
var _a, _b, _c;
|
||||
this.skipQueue = [];
|
||||
this.completed = 0;
|
||||
this.histogram = null;
|
||||
this.start = node_perf_hooks_1.performance.now();
|
||||
this.inProcessPendingMessages = false;
|
||||
this.startingUp = false;
|
||||
this.closingUp = false;
|
||||
this.workerFailsDuringBootstrap = false;
|
||||
this.destroying = false;
|
||||
this.publicInterface = publicInterface;
|
||||
this.taskQueue = (_a = options.taskQueue) !== null && _a !== void 0 ? _a : new task_queue_1.FixedQueue();
|
||||
const filename = options.filename ? (0, common_1.maybeFileURLToPath)(options.filename) : null;
|
||||
this.options = { ...kDefaultOptions, ...options, filename, maxQueue: 0 };
|
||||
if (this.options.recordTiming) {
|
||||
this.histogram = new histogram_1.PiscinaHistogramHandler();
|
||||
}
|
||||
// The >= and <= could be > and < but this way we get 100 % coverage 🙃
|
||||
if (options.maxThreads !== undefined &&
|
||||
this.options.minThreads >= options.maxThreads) {
|
||||
this.options.minThreads = options.maxThreads;
|
||||
}
|
||||
if (options.minThreads !== undefined &&
|
||||
this.options.maxThreads <= options.minThreads) {
|
||||
this.options.maxThreads = options.minThreads;
|
||||
}
|
||||
if (options.maxQueue === 'auto') {
|
||||
this.options.maxQueue = this.options.maxThreads ** 2;
|
||||
}
|
||||
else {
|
||||
this.options.maxQueue = (_b = options.maxQueue) !== null && _b !== void 0 ? _b : kDefaultOptions.maxQueue;
|
||||
}
|
||||
this.balancer = (_c = this.options.loadBalancer) !== null && _c !== void 0 ? _c : (0, worker_pool_1.LeastBusyBalancer)({ maximumUsage: this.options.concurrentTasksPerWorker });
|
||||
this.workers = new worker_pool_1.AsynchronouslyCreatedResourcePool(this.options.concurrentTasksPerWorker);
|
||||
this.workers.onTaskDone(this._onWorkerTaskDone.bind(this));
|
||||
this.maxCapacity = this.options.maxThreads * this.options.concurrentTasksPerWorker;
|
||||
this.startingUp = true;
|
||||
this._ensureMinimumWorkers();
|
||||
this.startingUp = false;
|
||||
this._needsDrain = false;
|
||||
}
|
||||
_ensureMinimumWorkers() {
|
||||
if (this.closingUp || this.destroying) {
|
||||
return;
|
||||
}
|
||||
while (this.workers.size < this.options.minThreads) {
|
||||
this._addNewWorker();
|
||||
}
|
||||
}
|
||||
_addNewWorker() {
|
||||
if (this.closingUp === true)
|
||||
return;
|
||||
const { port1, port2 } = new node_worker_threads_1.MessageChannel();
|
||||
const workerInfo = new worker_pool_1.WorkerInfo({
|
||||
worker: {
|
||||
filename: (0, node_path_1.resolve)(__dirname, 'worker.js'),
|
||||
env: this.options.env,
|
||||
argv: this.options.argv,
|
||||
execArgv: this.options.execArgv,
|
||||
resourceLimits: this.options.resourceLimits,
|
||||
workerData: this.options.workerData,
|
||||
trackUnmanagedFds: this.options.trackUnmanagedFds
|
||||
},
|
||||
port: port1,
|
||||
enableHistogram: this.options.workerHistogram
|
||||
}, onMessage.bind(this));
|
||||
const message = {
|
||||
filename: this.options.filename,
|
||||
name: this.options.name,
|
||||
port: port2,
|
||||
sharedBuffer: workerInfo.sharedBuffer,
|
||||
atomics: this.options.atomics,
|
||||
niceIncrement: this.options.niceIncrement
|
||||
};
|
||||
workerInfo.onDestroy(() => {
|
||||
this.publicInterface.emit('workerDestroy', workerInfo.interface);
|
||||
});
|
||||
workerInfo.onWorkerMessage(onWorkerMessage.bind(this));
|
||||
workerInfo.onWorkerError(onWorkerError.bind(this));
|
||||
workerInfo.onWorkerExit(onWorkerExit.bind(this));
|
||||
workerInfo.onPortClose(() => { workerInfo.workerRef(); });
|
||||
if (this.startingUp === true) {
|
||||
// There is no point in waiting for the initial set of Workers to indicate
|
||||
// that they are ready, we just mark them as such from the start.
|
||||
workerInfo.markAsReady();
|
||||
// We need to emit the event in the next microtask, so that the user can
|
||||
// attach event listeners before the event is emitted.
|
||||
queueMicrotask(() => {
|
||||
this.publicInterface.emit('workerCreate', workerInfo.interface);
|
||||
this._onWorkerReady(workerInfo);
|
||||
});
|
||||
}
|
||||
else {
|
||||
workerInfo.onReady(() => {
|
||||
this.publicInterface.emit('workerCreate', workerInfo.interface);
|
||||
this._onWorkerReady(workerInfo);
|
||||
});
|
||||
}
|
||||
workerInfo.init(message, [port2]).workerUnref();
|
||||
this.workers.add(workerInfo);
|
||||
function onMessage(message) {
|
||||
const { taskId, result } = message;
|
||||
// In case of success: Call the callback that was passed to `runTask`,
|
||||
// remove the `TaskInfo` associated with the Worker, which marks it as
|
||||
// free again.
|
||||
const taskInfo = workerInfo.popTask(taskId);
|
||||
this.workers.taskDone(workerInfo);
|
||||
if (taskInfo == null) { /* c8 ignore next */
|
||||
const err = new Error(`Unexpected message from Worker: ${(0, node_util_1.inspect)(message)}`);
|
||||
this.publicInterface.emit('error', err);
|
||||
}
|
||||
else {
|
||||
taskInfo.done(message.error, result);
|
||||
}
|
||||
this._processPendingMessages();
|
||||
}
|
||||
function onWorkerReady() {
|
||||
workerInfo.currentUsage() === 0 && workerInfo.unref();
|
||||
workerInfo.isReady() === false && workerInfo.markAsReady();
|
||||
}
|
||||
function onEventMessage(message) {
|
||||
this.publicInterface.emit('message', message);
|
||||
}
|
||||
function onWorkerMessage(message) {
|
||||
message instanceof Object && common_1.READY in message ? onWorkerReady() : onEventMessage.call(this, message);
|
||||
}
|
||||
function onWorkerError(err) {
|
||||
this._onError(workerInfo, err, false);
|
||||
}
|
||||
function onWorkerExit(code) {
|
||||
if (this.destroying === false) {
|
||||
const err = new Error(`worker exited with code: ${code}`);
|
||||
// Only error unfinished tasks on process exit, since there are legitimate
|
||||
// reasons to exit workers and we want to handle that gracefully when possible.
|
||||
this._onError(workerInfo, err, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
_onError(workerInfo, err, onlyErrorUnfinishedTasks) {
|
||||
const taskInfos = [...workerInfo.taskInfos.values()];
|
||||
workerInfo.taskInfos.clear();
|
||||
// Remove the worker from the list and potentially start a new Worker to
|
||||
// replace the current one.
|
||||
this._removeWorker(workerInfo);
|
||||
if (workerInfo.isReady() && !this.workerFailsDuringBootstrap) {
|
||||
this._ensureMinimumWorkers();
|
||||
}
|
||||
else {
|
||||
// Do not start new workers over and over if they already fail during
|
||||
// bootstrap, there's no point.
|
||||
this.workerFailsDuringBootstrap = true;
|
||||
}
|
||||
if (taskInfos.length > 0) {
|
||||
// If there are remaining unfinished tasks, call the callback that was
|
||||
// passed to `postTask` with the error
|
||||
for (const taskInfo of taskInfos) {
|
||||
taskInfo.done(err, null);
|
||||
}
|
||||
}
|
||||
else if (!onlyErrorUnfinishedTasks) {
|
||||
// If there are no unfinished tasks, instead emit an 'error' event
|
||||
this.publicInterface.emit('error', err);
|
||||
}
|
||||
}
|
||||
_processPendingMessages() {
|
||||
if (this.inProcessPendingMessages || this.options.atomics === 'disabled') {
|
||||
return;
|
||||
}
|
||||
this.inProcessPendingMessages = true;
|
||||
try {
|
||||
for (const workerInfo of this.workers) {
|
||||
workerInfo.processPendingMessages();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
this.inProcessPendingMessages = false;
|
||||
}
|
||||
}
|
||||
_removeWorker(workerInfo) {
|
||||
workerInfo.destroy();
|
||||
this.workers.delete(workerInfo);
|
||||
}
|
||||
_onWorkerReady(workerInfo) {
|
||||
this._onWorkerAvailable(workerInfo);
|
||||
}
|
||||
_onWorkerTaskDone(workerInfo) {
|
||||
this._onWorkerAvailable(workerInfo);
|
||||
}
|
||||
_onWorkerAvailable(workerInfo) {
|
||||
let workers = null;
|
||||
while ((this.taskQueue.size > 0 || this.skipQueue.length > 0)) {
|
||||
// The skipQueue will have tasks that we previously shifted off
|
||||
// the task queue but had to skip over... we have to make sure
|
||||
// we drain that before we drain the taskQueue.
|
||||
const taskInfo = this.skipQueue.shift() ||
|
||||
this.taskQueue.shift();
|
||||
if (workers == null) {
|
||||
workers = [...this.workers].map(workerInfo => workerInfo.interface);
|
||||
}
|
||||
const distributed = this._distributeTask(taskInfo, workers);
|
||||
if (distributed) {
|
||||
// If task was distributed, we should continue to distribute more tasks
|
||||
continue;
|
||||
}
|
||||
else if (this.workers.size < this.options.maxThreads) {
|
||||
// We spawn if possible
|
||||
// TODO: scheduler will intercept this.
|
||||
this._addNewWorker();
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
// If balancer states that pool is busy, we should stop trying to distribute tasks
|
||||
break;
|
||||
}
|
||||
}
|
||||
//If Infinity was sent as a parameter, we skip setting the Timeout that clears the worker
|
||||
if (this.options.idleTimeout === Infinity) {
|
||||
return;
|
||||
}
|
||||
// If more workers than minThreads, we can remove idle workers
|
||||
if (workerInfo.currentUsage() === 0 &&
|
||||
this.workers.size > this.options.minThreads) {
|
||||
workerInfo.setIdleTimeout(() => {
|
||||
// Exit early - we can't safely remove the worker as there is workload in the middle.
|
||||
// Once task is exited, timeout will be set once more for tearing up idle worker
|
||||
if (workerInfo.currentUsage() !== 0) {
|
||||
return;
|
||||
}
|
||||
if (this.workers.size > this.options.minThreads) {
|
||||
this._removeWorker(workerInfo);
|
||||
}
|
||||
}, this.options.idleTimeout);
|
||||
}
|
||||
}
|
||||
_distributeTask(task, workers) {
|
||||
var _a;
|
||||
// We need to verify if the task is aborted already or not
|
||||
// otherwise we might be distributing aborted tasks to workers
|
||||
if (task.aborted)
|
||||
return true;
|
||||
const candidate = this.balancer(task.interface, workers);
|
||||
// Seeking for a real worker instead of customized one
|
||||
if (candidate != null && candidate[symbols_1.kWorkerData] != null) {
|
||||
const now = node_perf_hooks_1.performance.now();
|
||||
(_a = this.histogram) === null || _a === void 0 ? void 0 : _a.recordWaitTime(now - task.created);
|
||||
task.started = now;
|
||||
candidate[symbols_1.kWorkerData].postTask(task);
|
||||
queueMicrotask(() => this._maybeDrain());
|
||||
// If candidate, let's try to distribute more tasks
|
||||
return true;
|
||||
}
|
||||
if (task.abortSignal != null) {
|
||||
this.skipQueue.push(task);
|
||||
}
|
||||
else {
|
||||
this.taskQueue.push(task);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
runTask(task, options) {
|
||||
var _a;
|
||||
let { filename, name } = options;
|
||||
const { transferList = [] } = options;
|
||||
filename = filename !== null && filename !== void 0 ? filename : this.options.filename;
|
||||
name = name !== null && name !== void 0 ? name : this.options.name;
|
||||
if (typeof filename !== 'string') {
|
||||
return Promise.reject(errors_1.Errors.FilenameNotProvided());
|
||||
}
|
||||
filename = (0, common_1.maybeFileURLToPath)(filename);
|
||||
let signal;
|
||||
if (this.closingUp || this.destroying) {
|
||||
const closingUpAbortController = new AbortController();
|
||||
closingUpAbortController.abort('queue is being terminated');
|
||||
signal = closingUpAbortController.signal;
|
||||
}
|
||||
else {
|
||||
signal = (_a = options.signal) !== null && _a !== void 0 ? _a : null;
|
||||
}
|
||||
const { promise: ret, resolve, reject } = (0, common_1.promiseResolvers)();
|
||||
const taskInfo = new task_queue_1.TaskInfo({
|
||||
task,
|
||||
transferList,
|
||||
filename,
|
||||
name,
|
||||
abortSignal: signal,
|
||||
triggerAsyncId: this.publicInterface.asyncResource.asyncId()
|
||||
}, (err, result) => {
|
||||
var _a;
|
||||
this.completed++;
|
||||
if (taskInfo.started) {
|
||||
(_a = this.histogram) === null || _a === void 0 ? void 0 : _a.recordRunTime(node_perf_hooks_1.performance.now() - taskInfo.started);
|
||||
}
|
||||
if (err !== null) {
|
||||
reject(err);
|
||||
}
|
||||
else {
|
||||
resolve(result);
|
||||
}
|
||||
queueMicrotask(this._maybeDrain.bind(this));
|
||||
});
|
||||
if (signal != null) {
|
||||
// If the AbortSignal has an aborted property and it's truthy,
|
||||
// reject immediately.
|
||||
if (signal.aborted) {
|
||||
reject(new abort_1.AbortError(signal.reason));
|
||||
return ret;
|
||||
}
|
||||
taskInfo.onAbort(() => {
|
||||
// Call reject() first to make sure we always reject with the AbortError
|
||||
// if the task is aborted, not with an Error from the possible
|
||||
// thread termination below.
|
||||
reject(new abort_1.AbortError(signal.reason));
|
||||
if (taskInfo.workerInfo != null) {
|
||||
// Already running: We cancel the Worker this is running on.
|
||||
this._removeWorker(taskInfo.workerInfo);
|
||||
this._ensureMinimumWorkers();
|
||||
}
|
||||
else {
|
||||
// Not yet running: Remove it from the queue.
|
||||
// Call should be idempotent
|
||||
this.taskQueue.remove(taskInfo);
|
||||
}
|
||||
});
|
||||
taskInfo.setAbortListener(signal);
|
||||
}
|
||||
if (this.taskQueue.size > 0) {
|
||||
const totalCapacity = this.options.maxQueue + this.pendingCapacity();
|
||||
if (this.taskQueue.size >= totalCapacity) {
|
||||
if (this.options.maxQueue === 0) {
|
||||
reject(errors_1.Errors.NoTaskQueueAvailable());
|
||||
}
|
||||
else {
|
||||
reject(errors_1.Errors.TaskQueueAtLimit());
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.taskQueue.push(taskInfo);
|
||||
}
|
||||
queueMicrotask(this._maybeDrain.bind(this));
|
||||
return ret;
|
||||
}
|
||||
const workers = [...this.workers.readyItems].map(workerInfo => workerInfo.interface);
|
||||
const distributed = this._distributeTask(taskInfo, workers);
|
||||
if (!distributed) {
|
||||
// We spawn if possible
|
||||
// TODO: scheduler will intercept this.
|
||||
if (this.workers.size < this.options.maxThreads) {
|
||||
this._addNewWorker();
|
||||
}
|
||||
// We reject if no task queue set and no more pending capacity.
|
||||
if (this.options.maxQueue <= 0 && this.pendingCapacity() === 0) {
|
||||
reject(errors_1.Errors.NoTaskQueueAvailable());
|
||||
}
|
||||
}
|
||||
;
|
||||
queueMicrotask(this._maybeDrain.bind(this));
|
||||
return ret;
|
||||
}
|
||||
pendingCapacity() {
|
||||
return this.workers.pendingItems.size *
|
||||
this.options.concurrentTasksPerWorker;
|
||||
}
|
||||
_maybeDrain() {
|
||||
/**
|
||||
* Our goal is to make it possible for user space to use the pool
|
||||
* in a way where always waiting === 0,
|
||||
* since we want to avoid creating tasks that can't execute
|
||||
* immediately in order to provide back pressure to the task source.
|
||||
*/
|
||||
const { maxCapacity } = this;
|
||||
const currentUsage = this.workers.getCurrentUsage();
|
||||
if (maxCapacity === currentUsage) {
|
||||
this._needsDrain = true;
|
||||
queueMicrotask(() => this.publicInterface.emit('needsDrain'));
|
||||
}
|
||||
else if (maxCapacity > currentUsage && this._needsDrain) {
|
||||
this._needsDrain = false;
|
||||
queueMicrotask(() => this.publicInterface.emit('drain'));
|
||||
}
|
||||
}
|
||||
async destroy() {
|
||||
this.destroying = true;
|
||||
while (this.skipQueue.length > 0) {
|
||||
const taskInfo = this.skipQueue.shift();
|
||||
taskInfo.done(new Error('Terminating worker thread'));
|
||||
}
|
||||
while (this.taskQueue.size > 0) {
|
||||
const taskInfo = this.taskQueue.shift();
|
||||
taskInfo.done(new Error('Terminating worker thread'));
|
||||
}
|
||||
const exitEvents = [];
|
||||
while (this.workers.size > 0) {
|
||||
const [workerInfo] = this.workers;
|
||||
exitEvents.push((0, node_events_1.once)(workerInfo.worker, 'exit'));
|
||||
this._removeWorker(workerInfo);
|
||||
}
|
||||
await Promise.allSettled(exitEvents);
|
||||
this.destroying = false;
|
||||
}
|
||||
async close(options) {
|
||||
this.closingUp = true;
|
||||
if (options.force) {
|
||||
const skipQueueLength = this.skipQueue.length;
|
||||
for (let i = 0; i < skipQueueLength; i++) {
|
||||
const taskInfo = this.skipQueue.shift();
|
||||
if (taskInfo.workerInfo === null) {
|
||||
taskInfo.done(new abort_1.AbortError('pool is closed'));
|
||||
}
|
||||
else {
|
||||
this.skipQueue.push(taskInfo);
|
||||
}
|
||||
}
|
||||
const taskQueueLength = this.taskQueue.size;
|
||||
for (let i = 0; i < taskQueueLength; i++) {
|
||||
const taskInfo = this.taskQueue.shift();
|
||||
if (taskInfo.workerInfo === null) {
|
||||
taskInfo.done(new abort_1.AbortError('pool is closed'));
|
||||
}
|
||||
else {
|
||||
this.taskQueue.push(taskInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
const onPoolFlushed = () => new Promise((resolve) => {
|
||||
const numberOfWorkers = this.workers.size;
|
||||
if (numberOfWorkers === 0) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
let numberOfWorkersDone = 0;
|
||||
const checkIfWorkerIsDone = (workerInfo) => {
|
||||
if (workerInfo.taskInfos.size === 0) {
|
||||
numberOfWorkersDone++;
|
||||
}
|
||||
if (numberOfWorkers === numberOfWorkersDone) {
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
this.workers.onTaskDone(checkIfWorkerIsDone);
|
||||
for (const workerInfo of this.workers) {
|
||||
checkIfWorkerIsDone(workerInfo);
|
||||
this.workers.onTaskDone(checkIfWorkerIsDone);
|
||||
}
|
||||
});
|
||||
const throwOnTimeOut = async (timeout) => {
|
||||
await (0, promises_1.setTimeout)(timeout, null, { ref: false });
|
||||
throw errors_1.Errors.CloseTimeout();
|
||||
};
|
||||
try {
|
||||
await Promise.race([
|
||||
onPoolFlushed(),
|
||||
throwOnTimeOut(this.options.closeTimeout)
|
||||
]);
|
||||
}
|
||||
catch (error) {
|
||||
this.publicInterface.emit('error', error);
|
||||
}
|
||||
finally {
|
||||
await this.destroy();
|
||||
this.publicInterface.emit('close');
|
||||
this.closingUp = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
class Piscina extends node_events_1.EventEmitterAsyncResource {
|
||||
constructor(options = {}) {
|
||||
super({ ...options, name: 'Piscina' });
|
||||
_Piscina_pool.set(this, void 0);
|
||||
_Piscina_histogram.set(this, null);
|
||||
if (typeof options.filename !== 'string' && options.filename != null) {
|
||||
throw new TypeError('options.filename must be a string or null');
|
||||
}
|
||||
if (typeof options.name !== 'string' && options.name != null) {
|
||||
throw new TypeError('options.name must be a string or null');
|
||||
}
|
||||
if (options.minThreads !== undefined &&
|
||||
(typeof options.minThreads !== 'number' || options.minThreads < 0)) {
|
||||
throw new TypeError('options.minThreads must be a non-negative integer');
|
||||
}
|
||||
if (options.maxThreads !== undefined &&
|
||||
(typeof options.maxThreads !== 'number' || options.maxThreads < 1)) {
|
||||
throw new TypeError('options.maxThreads must be a positive integer');
|
||||
}
|
||||
if (options.minThreads !== undefined && options.maxThreads !== undefined &&
|
||||
options.minThreads > options.maxThreads) {
|
||||
throw new RangeError('options.minThreads and options.maxThreads must not conflict');
|
||||
}
|
||||
if (options.idleTimeout !== undefined &&
|
||||
(typeof options.idleTimeout !== 'number' || options.idleTimeout < 0)) {
|
||||
throw new TypeError('options.idleTimeout must be a non-negative integer');
|
||||
}
|
||||
if (options.maxQueue !== undefined &&
|
||||
options.maxQueue !== 'auto' &&
|
||||
(typeof options.maxQueue !== 'number' || options.maxQueue < 0)) {
|
||||
throw new TypeError('options.maxQueue must be a non-negative integer');
|
||||
}
|
||||
if (options.concurrentTasksPerWorker !== undefined &&
|
||||
(typeof options.concurrentTasksPerWorker !== 'number' ||
|
||||
options.concurrentTasksPerWorker < 1)) {
|
||||
throw new TypeError('options.concurrentTasksPerWorker must be a positive integer');
|
||||
}
|
||||
if (options.atomics != null && (typeof options.atomics !== 'string' ||
|
||||
!['sync', 'async', 'disabled'].includes(options.atomics))) {
|
||||
throw new TypeError('options.atomics should be a value of sync, sync or disabled.');
|
||||
}
|
||||
if (options.resourceLimits !== undefined &&
|
||||
(typeof options.resourceLimits !== 'object' ||
|
||||
options.resourceLimits === null)) {
|
||||
throw new TypeError('options.resourceLimits must be an object');
|
||||
}
|
||||
if (options.taskQueue !== undefined && !(0, task_queue_1.isTaskQueue)(options.taskQueue)) {
|
||||
throw new TypeError('options.taskQueue must be a TaskQueue object');
|
||||
}
|
||||
if (options.niceIncrement !== undefined &&
|
||||
(typeof options.niceIncrement !== 'number' || (options.niceIncrement < 0 && process.platform !== 'win32'))) {
|
||||
throw new TypeError('options.niceIncrement must be a non-negative integer on Unix systems');
|
||||
}
|
||||
if (options.trackUnmanagedFds !== undefined &&
|
||||
typeof options.trackUnmanagedFds !== 'boolean') {
|
||||
throw new TypeError('options.trackUnmanagedFds must be a boolean value');
|
||||
}
|
||||
if (options.closeTimeout !== undefined && (typeof options.closeTimeout !== 'number' || options.closeTimeout < 0)) {
|
||||
throw new TypeError('options.closeTimeout must be a non-negative integer');
|
||||
}
|
||||
if (options.loadBalancer !== undefined && (typeof options.loadBalancer !== 'function' || options.loadBalancer.length < 1)) {
|
||||
throw new TypeError('options.loadBalancer must be a function with at least two args');
|
||||
}
|
||||
if (options.workerHistogram !== undefined && (typeof options.workerHistogram !== 'boolean')) {
|
||||
throw new TypeError('options.workerHistogram must be a boolean');
|
||||
}
|
||||
__classPrivateFieldSet(this, _Piscina_pool, new ThreadPool(this, options), "f");
|
||||
}
|
||||
run(task, options = kDefaultRunOptions) {
|
||||
if (options === null || typeof options !== 'object') {
|
||||
return Promise.reject(new TypeError('options must be an object'));
|
||||
}
|
||||
const { transferList, filename, name, signal } = options;
|
||||
if (transferList !== undefined && !Array.isArray(transferList)) {
|
||||
return Promise.reject(new TypeError('transferList argument must be an Array'));
|
||||
}
|
||||
if (filename != null && typeof filename !== 'string') {
|
||||
return Promise.reject(new TypeError('filename argument must be a string'));
|
||||
}
|
||||
if (name != null && typeof name !== 'string') {
|
||||
return Promise.reject(new TypeError('name argument must be a string'));
|
||||
}
|
||||
if (signal != null && typeof signal !== 'object') {
|
||||
return Promise.reject(new TypeError('signal argument must be an object'));
|
||||
}
|
||||
return __classPrivateFieldGet(this, _Piscina_pool, "f").runTask(task, { transferList, filename, name, signal });
|
||||
}
|
||||
async close(options = kDefaultCloseOptions) {
|
||||
if (options === null || typeof options !== 'object') {
|
||||
throw TypeError('options must be an object');
|
||||
}
|
||||
let { force } = options;
|
||||
if (force !== undefined && typeof force !== 'boolean') {
|
||||
return Promise.reject(new TypeError('force argument must be a boolean'));
|
||||
}
|
||||
force !== null && force !== void 0 ? force : (force = kDefaultCloseOptions.force);
|
||||
return __classPrivateFieldGet(this, _Piscina_pool, "f").close({
|
||||
force
|
||||
});
|
||||
}
|
||||
destroy() {
|
||||
return __classPrivateFieldGet(this, _Piscina_pool, "f").destroy();
|
||||
}
|
||||
[(_Piscina_pool = new WeakMap(), _Piscina_histogram = new WeakMap(), Symbol.dispose)]() {
|
||||
this.close();
|
||||
}
|
||||
[Symbol.asyncDispose]() {
|
||||
return this.close();
|
||||
}
|
||||
get maxThreads() {
|
||||
return __classPrivateFieldGet(this, _Piscina_pool, "f").options.maxThreads;
|
||||
}
|
||||
get minThreads() {
|
||||
return __classPrivateFieldGet(this, _Piscina_pool, "f").options.minThreads;
|
||||
}
|
||||
get options() {
|
||||
return __classPrivateFieldGet(this, _Piscina_pool, "f").options;
|
||||
}
|
||||
get threads() {
|
||||
const ret = [];
|
||||
for (const workerInfo of __classPrivateFieldGet(this, _Piscina_pool, "f").workers) {
|
||||
ret.push(workerInfo.worker);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
get queueSize() {
|
||||
const pool = __classPrivateFieldGet(this, _Piscina_pool, "f");
|
||||
return Math.max(pool.taskQueue.size - pool.pendingCapacity(), 0);
|
||||
}
|
||||
get completed() {
|
||||
return __classPrivateFieldGet(this, _Piscina_pool, "f").completed;
|
||||
}
|
||||
get histogram() {
|
||||
if (__classPrivateFieldGet(this, _Piscina_histogram, "f") == null) {
|
||||
const piscinahistogram = {
|
||||
// @ts-expect-error
|
||||
get runTime() { var _a; return (_a = this.histogram) === null || _a === void 0 ? void 0 : _a.runTimeSummary; },
|
||||
// @ts-expect-error
|
||||
get waitTime() { var _a; return (_a = this.histogram) === null || _a === void 0 ? void 0 : _a.waitTimeSummary; },
|
||||
resetRunTime() {
|
||||
var _a;
|
||||
// @ts-expect-error
|
||||
(_a = this.histogram) === null || _a === void 0 ? void 0 : _a.resetRunTime();
|
||||
},
|
||||
resetWaitTime() {
|
||||
var _a;
|
||||
// @ts-expect-error
|
||||
(_a = this.histogram) === null || _a === void 0 ? void 0 : _a.resetWaitTime();
|
||||
},
|
||||
};
|
||||
Object.defineProperty(piscinahistogram, 'histogram', {
|
||||
value: __classPrivateFieldGet(this, _Piscina_pool, "f").histogram,
|
||||
writable: false,
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
});
|
||||
__classPrivateFieldSet(this, _Piscina_histogram, piscinahistogram, "f");
|
||||
}
|
||||
;
|
||||
return __classPrivateFieldGet(this, _Piscina_histogram, "f");
|
||||
}
|
||||
get utilization() {
|
||||
if (__classPrivateFieldGet(this, _Piscina_pool, "f").histogram == null) {
|
||||
return 0;
|
||||
}
|
||||
// count is available as of Node.js v16.14.0 but not present in the types
|
||||
const count = __classPrivateFieldGet(this, _Piscina_pool, "f").histogram.runTimeCount;
|
||||
if (count === 0) {
|
||||
return 0;
|
||||
}
|
||||
// The capacity is the max compute time capacity of the
|
||||
// pool to this point in time as determined by the length
|
||||
// of time the pool has been running multiplied by the
|
||||
// maximum number of threads.
|
||||
const capacity = this.duration * __classPrivateFieldGet(this, _Piscina_pool, "f").options.maxThreads;
|
||||
const totalMeanRuntime = (__classPrivateFieldGet(this, _Piscina_pool, "f").histogram.runTimeSummary.mean / 1000) * count;
|
||||
// We calculate the appoximate pool utilization by multiplying
|
||||
// the mean run time of all tasks by the number of runtime
|
||||
// samples taken and dividing that by the capacity. The
|
||||
// theory here is that capacity represents the absolute upper
|
||||
// limit of compute time this pool could ever attain (but
|
||||
// never will for a variety of reasons. Multiplying the
|
||||
// mean run time by the number of tasks sampled yields an
|
||||
// approximation of the realized compute time. The utilization
|
||||
// then becomes a point-in-time measure of how active the
|
||||
// pool is.
|
||||
return totalMeanRuntime / capacity;
|
||||
}
|
||||
get duration() {
|
||||
return node_perf_hooks_1.performance.now() - __classPrivateFieldGet(this, _Piscina_pool, "f").start;
|
||||
}
|
||||
get needsDrain() {
|
||||
return __classPrivateFieldGet(this, _Piscina_pool, "f")._needsDrain;
|
||||
}
|
||||
static get isWorkerThread() {
|
||||
return common_1.commonState.isWorkerThread;
|
||||
}
|
||||
static get workerData() {
|
||||
return common_1.commonState.workerData;
|
||||
}
|
||||
static get version() {
|
||||
return package_json_1.version;
|
||||
}
|
||||
static get Piscina() {
|
||||
return Piscina;
|
||||
}
|
||||
static get FixedQueue() {
|
||||
return task_queue_1.FixedQueue;
|
||||
}
|
||||
static get ArrayTaskQueue() {
|
||||
return task_queue_1.ArrayTaskQueue;
|
||||
}
|
||||
static move(val) {
|
||||
if (val != null && typeof val === 'object' && typeof val !== 'function') {
|
||||
if (!(0, common_1.isTransferable)(val)) {
|
||||
if (node_util_1.types.isArrayBufferView(val)) {
|
||||
val = new ArrayBufferViewTransferable(val);
|
||||
}
|
||||
else {
|
||||
val = new DirectlyTransferable(val);
|
||||
}
|
||||
}
|
||||
(0, common_1.markMovable)(val);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
static get transferableSymbol() { return symbols_1.kTransferable; }
|
||||
static get valueSymbol() { return symbols_1.kValue; }
|
||||
static get queueOptionsSymbol() { return symbols_1.kQueueOptions; }
|
||||
}
|
||||
exports.default = Piscina;
|
||||
exports.Piscina = Piscina;
|
||||
exports.move = Piscina.move;
|
||||
exports.isWorkerThread = Piscina.isWorkerThread;
|
||||
exports.workerData = Piscina.workerData;
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+2
@@ -0,0 +1,2 @@
|
||||
import Piscina from './index';
|
||||
export = Piscina;
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
const index_1 = __importDefault(require("./index"));
|
||||
module.exports = index_1.default;
|
||||
//# sourceMappingURL=main.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";;;;AAAA,oDAA8B;AAG9B,iBAAS,eAAO,CAAC"}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
export declare const kMovable: unique symbol;
|
||||
export declare const kWorkerData: unique symbol;
|
||||
export declare const kTransferable: unique symbol;
|
||||
export declare const kValue: unique symbol;
|
||||
export declare const kQueueOptions: unique symbol;
|
||||
export declare const kRequestCountField = 0;
|
||||
export declare const kResponseCountField = 1;
|
||||
export declare const kFieldCount = 2;
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.kFieldCount = exports.kResponseCountField = exports.kRequestCountField = exports.kQueueOptions = exports.kValue = exports.kTransferable = exports.kWorkerData = exports.kMovable = void 0;
|
||||
// Internal symbol used to mark Transferable objects returned
|
||||
// by the Piscina.move() function
|
||||
exports.kMovable = Symbol('Piscina.kMovable');
|
||||
exports.kWorkerData = Symbol('Piscina.kWorkerData');
|
||||
exports.kTransferable = Symbol.for('Piscina.transferable');
|
||||
exports.kValue = Symbol.for('Piscina.valueOf');
|
||||
exports.kQueueOptions = Symbol.for('Piscina.queueOptions');
|
||||
exports.kRequestCountField = 0;
|
||||
exports.kResponseCountField = 1;
|
||||
exports.kFieldCount = 2;
|
||||
//# sourceMappingURL=symbols.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"symbols.js","sourceRoot":"","sources":["../src/symbols.ts"],"names":[],"mappings":";;;AAAA,6DAA6D;AAC7D,iCAAiC;AACpB,QAAA,QAAQ,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAC;AACtC,QAAA,WAAW,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAC;AAC5C,QAAA,aAAa,GAAG,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AACnD,QAAA,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AACvC,QAAA,aAAa,GAAG,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AACnD,QAAA,kBAAkB,GAAG,CAAC,CAAC;AACvB,QAAA,mBAAmB,GAAG,CAAC,CAAC;AACxB,QAAA,WAAW,GAAG,CAAC,CAAC"}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import type { TaskQueue, Task } from '.';
|
||||
export declare class ArrayTaskQueue implements TaskQueue {
|
||||
tasks: Task[];
|
||||
get size(): number;
|
||||
shift(): Task | null;
|
||||
push(task: Task): void;
|
||||
remove(task: Task): void;
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ArrayTaskQueue = void 0;
|
||||
const node_assert_1 = __importDefault(require("node:assert"));
|
||||
class ArrayTaskQueue {
|
||||
constructor() {
|
||||
this.tasks = [];
|
||||
}
|
||||
get size() {
|
||||
return this.tasks.length;
|
||||
}
|
||||
shift() {
|
||||
var _a;
|
||||
return (_a = this.tasks.shift()) !== null && _a !== void 0 ? _a : null;
|
||||
}
|
||||
push(task) {
|
||||
this.tasks.push(task);
|
||||
}
|
||||
remove(task) {
|
||||
const index = this.tasks.indexOf(task);
|
||||
node_assert_1.default.notStrictEqual(index, -1);
|
||||
this.tasks.splice(index, 1);
|
||||
}
|
||||
}
|
||||
exports.ArrayTaskQueue = ArrayTaskQueue;
|
||||
//# sourceMappingURL=array_queue.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"array_queue.js","sourceRoot":"","sources":["../../src/task_queue/array_queue.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAiC;AAIjC,MAAa,cAAc;IAA3B;QACE,UAAK,GAAW,EAAE,CAAA;IAmBpB,CAAC;IAjBC,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;IAC3B,CAAC;IAED,KAAK;;QACH,OAAO,MAAA,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,mCAAI,IAAI,CAAC;IACpC,CAAC;IAED,IAAI,CAAE,IAAU;QACd,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IAED,MAAM,CAAE,IAAU;QAChB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACvC,qBAAM,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;QACjC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IAC9B,CAAC;CACF;AApBD,wCAoBC"}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
import type { kQueueOptions } from '../symbols';
|
||||
export interface TaskQueue {
|
||||
readonly size: number;
|
||||
shift(): Task | null;
|
||||
remove(task: Task): void;
|
||||
push(task: Task): void;
|
||||
}
|
||||
export interface PiscinaTask extends Task {
|
||||
taskId: number;
|
||||
filename: string;
|
||||
name: string;
|
||||
created: number;
|
||||
isAbortable: boolean;
|
||||
}
|
||||
export interface Task {
|
||||
readonly [kQueueOptions]: object | null;
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
;
|
||||
//# sourceMappingURL=common.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/task_queue/common.ts"],"names":[],"mappings":";;AAoBC,CAAC"}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import type { Task } from './common';
|
||||
import { TaskQueue } from '.';
|
||||
declare class FixedCircularBuffer {
|
||||
bottom: number;
|
||||
top: number;
|
||||
list: Array<Task | undefined>;
|
||||
next: FixedCircularBuffer | null;
|
||||
isEmpty(): boolean;
|
||||
isFull(): boolean;
|
||||
push(data: Task): void;
|
||||
shift(): Task | null;
|
||||
remove(task: Task): void;
|
||||
}
|
||||
export declare class FixedQueue implements TaskQueue {
|
||||
#private;
|
||||
head: FixedCircularBuffer;
|
||||
tail: FixedCircularBuffer;
|
||||
constructor();
|
||||
isEmpty(): boolean;
|
||||
push(data: Task): void;
|
||||
shift(): Task | null;
|
||||
remove(task: Task): void;
|
||||
get size(): number;
|
||||
}
|
||||
export {};
|
||||
+192
@@ -0,0 +1,192 @@
|
||||
"use strict";
|
||||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
||||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
||||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
||||
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
||||
};
|
||||
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
||||
if (kind === "m") throw new TypeError("Private method is not writable");
|
||||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
||||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
||||
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
var _FixedQueue_size;
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.FixedQueue = void 0;
|
||||
/*
|
||||
* Modified Fixed Queue Implementation based on the one from Node.js Project
|
||||
* License: MIT License
|
||||
* Source: https://github.com/nodejs/node/blob/de7b37880f5a541d5f874c1c2362a65a4be76cd0/lib/internal/fixed_queue.js
|
||||
*/
|
||||
const node_assert_1 = __importDefault(require("node:assert"));
|
||||
// Currently optimal queue size, tested on V8 6.0 - 6.6. Must be power of two.
|
||||
const kSize = 2048;
|
||||
const kMask = kSize - 1;
|
||||
// The FixedQueue is implemented as a singly-linked list of fixed-size
|
||||
// circular buffers. It looks something like this:
|
||||
//
|
||||
// head tail
|
||||
// | |
|
||||
// v v
|
||||
// +-----------+ <-----\ +-----------+ <------\ +-----------+
|
||||
// | [null] | \----- | next | \------- | next |
|
||||
// +-----------+ +-----------+ +-----------+
|
||||
// | item | <-- bottom | item | <-- bottom | [empty] |
|
||||
// | item | | item | | [empty] |
|
||||
// | item | | item | | [empty] |
|
||||
// | item | | item | | [empty] |
|
||||
// | item | | item | bottom --> | item |
|
||||
// | item | | item | | item |
|
||||
// | ... | | ... | | ... |
|
||||
// | item | | item | | item |
|
||||
// | item | | item | | item |
|
||||
// | [empty] | <-- top | item | | item |
|
||||
// | [empty] | | item | | item |
|
||||
// | [empty] | | [empty] | <-- top top --> | [empty] |
|
||||
// +-----------+ +-----------+ +-----------+
|
||||
//
|
||||
// Or, if there is only one circular buffer, it looks something
|
||||
// like either of these:
|
||||
//
|
||||
// head tail head tail
|
||||
// | | | |
|
||||
// v v v v
|
||||
// +-----------+ +-----------+
|
||||
// | [null] | | [null] |
|
||||
// +-----------+ +-----------+
|
||||
// | [empty] | | item |
|
||||
// | [empty] | | item |
|
||||
// | item | <-- bottom top --> | [empty] |
|
||||
// | item | | [empty] |
|
||||
// | [empty] | <-- top bottom --> | item |
|
||||
// | [empty] | | item |
|
||||
// +-----------+ +-----------+
|
||||
//
|
||||
// Adding a value means moving `top` forward by one, removing means
|
||||
// moving `bottom` forward by one. After reaching the end, the queue
|
||||
// wraps around.
|
||||
//
|
||||
// When `top === bottom` the current queue is empty and when
|
||||
// `top + 1 === bottom` it's full. This wastes a single space of storage
|
||||
// but allows much quicker checks.
|
||||
class FixedCircularBuffer {
|
||||
constructor() {
|
||||
this.bottom = 0;
|
||||
this.top = 0;
|
||||
this.list = new Array(kSize);
|
||||
this.next = null;
|
||||
}
|
||||
isEmpty() {
|
||||
return this.top === this.bottom;
|
||||
}
|
||||
isFull() {
|
||||
return ((this.top + 1) & kMask) === this.bottom;
|
||||
}
|
||||
push(data) {
|
||||
this.list[this.top] = data;
|
||||
this.top = (this.top + 1) & kMask;
|
||||
}
|
||||
shift() {
|
||||
const nextItem = this.list[this.bottom];
|
||||
if (nextItem === undefined) {
|
||||
return null;
|
||||
}
|
||||
this.list[this.bottom] = undefined;
|
||||
this.bottom = (this.bottom + 1) & kMask;
|
||||
return nextItem;
|
||||
}
|
||||
remove(task) {
|
||||
const indexToRemove = this.list.indexOf(task);
|
||||
node_assert_1.default.notStrictEqual(indexToRemove, -1);
|
||||
let curr = indexToRemove;
|
||||
while (true) {
|
||||
const next = (curr + 1) & kMask;
|
||||
this.list[curr] = this.list[next];
|
||||
if (this.list[curr] === undefined) {
|
||||
break;
|
||||
}
|
||||
if (next === indexToRemove) {
|
||||
this.list[curr] = undefined;
|
||||
break;
|
||||
}
|
||||
curr = next;
|
||||
}
|
||||
this.top = (this.top - 1) & kMask;
|
||||
}
|
||||
}
|
||||
class FixedQueue {
|
||||
constructor() {
|
||||
_FixedQueue_size.set(this, 0);
|
||||
this.head = this.tail = new FixedCircularBuffer();
|
||||
}
|
||||
isEmpty() {
|
||||
return this.head.isEmpty();
|
||||
}
|
||||
push(data) {
|
||||
var _a;
|
||||
if (this.head.isFull()) {
|
||||
// Head is full: Creates a new queue, sets the old queue's `.next` to it,
|
||||
// and sets it as the new main queue.
|
||||
this.head = this.head.next = new FixedCircularBuffer();
|
||||
}
|
||||
this.head.push(data);
|
||||
__classPrivateFieldSet(this, _FixedQueue_size, (_a = __classPrivateFieldGet(this, _FixedQueue_size, "f"), _a++, _a), "f");
|
||||
}
|
||||
shift() {
|
||||
var _a;
|
||||
const tail = this.tail;
|
||||
const next = tail.shift();
|
||||
if (next !== null)
|
||||
__classPrivateFieldSet(this, _FixedQueue_size, (_a = __classPrivateFieldGet(this, _FixedQueue_size, "f"), _a--, _a), "f");
|
||||
if (tail.isEmpty() && tail.next !== null) {
|
||||
// If there is another queue, it forms the new tail.
|
||||
this.tail = tail.next;
|
||||
tail.next = null;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
remove(task) {
|
||||
var _a;
|
||||
let prev = null;
|
||||
let buffer = this.tail;
|
||||
while (true) {
|
||||
if (buffer.list.includes(task)) {
|
||||
buffer.remove(task);
|
||||
__classPrivateFieldSet(this, _FixedQueue_size, (_a = __classPrivateFieldGet(this, _FixedQueue_size, "f"), _a--, _a), "f");
|
||||
break;
|
||||
}
|
||||
if (buffer.next === null)
|
||||
break;
|
||||
prev = buffer;
|
||||
buffer = buffer.next;
|
||||
}
|
||||
if (buffer.isEmpty()) {
|
||||
// removing tail
|
||||
if (prev === null) {
|
||||
// if tail is not the last buffer
|
||||
if (buffer.next !== null)
|
||||
this.tail = buffer.next;
|
||||
}
|
||||
else {
|
||||
// removing head
|
||||
if (buffer.next === null) {
|
||||
this.head = prev;
|
||||
}
|
||||
else {
|
||||
// removing buffer from middle
|
||||
prev.next = buffer.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
get size() {
|
||||
return __classPrivateFieldGet(this, _FixedQueue_size, "f");
|
||||
}
|
||||
}
|
||||
exports.FixedQueue = FixedQueue;
|
||||
_FixedQueue_size = new WeakMap();
|
||||
;
|
||||
//# sourceMappingURL=fixed_queue.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"fixed_queue.js","sourceRoot":"","sources":["../../src/task_queue/fixed_queue.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA;;;;GAIG;AACH,8DAAiC;AAGjC,8EAA8E;AAC9E,MAAM,KAAK,GAAG,IAAI,CAAC;AACnB,MAAM,KAAK,GAAG,KAAK,GAAG,CAAC,CAAC;AAEx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I,CAAC,CAAC;gBACpB,+CAAA,CAAA,wDAAU,EAAV,IAAY,IAAA,CAAA,MAAA,CAAC;gBACb,MAAM;YACR,CAAC;YACD,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI;gBAAE,MAAM;YAChC,IAAI,GAAG,MAAM,CAAC;YACd,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC;QACvB,CAAC;QACD,IAAI,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC;YACrB,gBAAgB;YAChB,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;gBAClB,iCAAiC;gBACjC,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI;oBAAE,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;YACpD,CAAC;iBAAM,CAAC;gBACN,gBAAgB;gBAChB,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;oBACzB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;gBACnB,CAAC;qBAAM,CAAC;oBACN,8BAA8B;oBAC9B,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;gBAC1B,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,wBAAM,CAAC;IACpB,CAAC;CACF;AApED,gCAoEC;;AAAA,CAAC"}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
import type { MessagePort } from 'node:worker_threads';
|
||||
import { AsyncResource } from 'node:async_hooks';
|
||||
import type { WorkerInfo } from '../worker_pool';
|
||||
import type { Task, TaskQueue, PiscinaTask } from './common';
|
||||
import { type AbortSignalAny } from '../abort';
|
||||
import { kQueueOptions } from '../symbols';
|
||||
export { ArrayTaskQueue } from './array_queue';
|
||||
export { FixedQueue } from './fixed_queue';
|
||||
export type TaskCallback = (err: Error, result: any) => void;
|
||||
export type TransferList = MessagePort extends {
|
||||
postMessage: (value: any, transferList: infer T) => any;
|
||||
} ? T : never;
|
||||
export type TransferListItem = TransferList extends Array<infer T> ? T : never;
|
||||
type TaskInfoParameters = {
|
||||
task: any;
|
||||
transferList: TransferList;
|
||||
filename: string;
|
||||
name: string;
|
||||
abortSignal: AbortSignalAny | null;
|
||||
triggerAsyncId: number;
|
||||
};
|
||||
/**
|
||||
* Verifies if a given TaskQueue is valid
|
||||
*
|
||||
* @export
|
||||
* @param {*} value
|
||||
* @return {*} {boolean}
|
||||
*/
|
||||
export declare function isTaskQueue(value: TaskQueue): boolean;
|
||||
export declare class TaskInfo extends AsyncResource implements Task {
|
||||
callback: TaskCallback;
|
||||
task: any;
|
||||
transferList: TransferList;
|
||||
filename: string;
|
||||
name: string;
|
||||
taskId: number;
|
||||
abortSignal: AbortSignalAny | null;
|
||||
workerInfo: WorkerInfo | null;
|
||||
created: number;
|
||||
started: number;
|
||||
aborted: boolean;
|
||||
_abortListener: (() => void);
|
||||
_abortCleaner: (() => void) | null;
|
||||
constructor({ task, transferList, filename, name, abortSignal, triggerAsyncId, }: TaskInfoParameters, callback: TaskCallback);
|
||||
onAbort(value: (() => void)): void;
|
||||
setAbortListener(signal: AbortSignalAny): void;
|
||||
releaseTask(): any;
|
||||
onResponse(_result: any): void;
|
||||
done(err: Error | null, result?: any): void;
|
||||
get [kQueueOptions](): {} | null;
|
||||
get interface(): PiscinaTask;
|
||||
}
|
||||
export { Task, TaskQueue, PiscinaTask };
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.TaskInfo = exports.FixedQueue = exports.ArrayTaskQueue = void 0;
|
||||
exports.isTaskQueue = isTaskQueue;
|
||||
const node_perf_hooks_1 = require("node:perf_hooks");
|
||||
const node_async_hooks_1 = require("node:async_hooks");
|
||||
const abort_1 = require("../abort");
|
||||
const common_1 = require("../common");
|
||||
const symbols_1 = require("../symbols");
|
||||
var array_queue_1 = require("./array_queue");
|
||||
Object.defineProperty(exports, "ArrayTaskQueue", { enumerable: true, get: function () { return array_queue_1.ArrayTaskQueue; } });
|
||||
var fixed_queue_1 = require("./fixed_queue");
|
||||
Object.defineProperty(exports, "FixedQueue", { enumerable: true, get: function () { return fixed_queue_1.FixedQueue; } });
|
||||
/**
|
||||
* Verifies if a given TaskQueue is valid
|
||||
*
|
||||
* @export
|
||||
* @param {*} value
|
||||
* @return {*} {boolean}
|
||||
*/
|
||||
function isTaskQueue(value) {
|
||||
return (typeof value === 'object' &&
|
||||
value !== null &&
|
||||
'size' in value &&
|
||||
typeof value.shift === 'function' &&
|
||||
typeof value.remove === 'function' &&
|
||||
typeof value.push === 'function');
|
||||
}
|
||||
let taskIdCounter = 0;
|
||||
// Extend AsyncResource so that async relations between posting a task and
|
||||
// receiving its result are visible to diagnostic tools.
|
||||
class TaskInfo extends node_async_hooks_1.AsyncResource {
|
||||
constructor({ task, transferList, filename, name, abortSignal, triggerAsyncId, }, callback) {
|
||||
super('Piscina.Task', { requireManualDestroy: true, triggerAsyncId });
|
||||
this.workerInfo = null;
|
||||
this.aborted = false;
|
||||
this._abortListener = () => { this.aborted = true; };
|
||||
this._abortCleaner = null;
|
||||
this.callback = callback;
|
||||
this.task = task;
|
||||
this.transferList = transferList;
|
||||
// If the task is a Transferable returned by
|
||||
// Piscina.move(), then add it to the transferList
|
||||
// automatically
|
||||
if ((0, common_1.isMovable)(task)) {
|
||||
// This condition should never be hit but typescript
|
||||
// complains if we dont do the check.
|
||||
/* c8 ignore next */
|
||||
if (this.transferList == null) {
|
||||
this.transferList = [];
|
||||
}
|
||||
this.transferList = this.transferList.concat(task[symbols_1.kTransferable]);
|
||||
this.task = task[symbols_1.kValue];
|
||||
}
|
||||
this.filename = filename;
|
||||
this.name = name;
|
||||
// TODO: This should not be global
|
||||
this.taskId = taskIdCounter++;
|
||||
this.abortSignal = abortSignal;
|
||||
this.created = node_perf_hooks_1.performance.now();
|
||||
this.started = 0;
|
||||
}
|
||||
onAbort(value) {
|
||||
this._abortListener = () => {
|
||||
this.aborted = true;
|
||||
value();
|
||||
};
|
||||
}
|
||||
setAbortListener(signal) {
|
||||
this._abortCleaner = (0, abort_1.onabort)(signal, this._abortListener);
|
||||
}
|
||||
releaseTask() {
|
||||
const ret = this.task;
|
||||
this.task = null;
|
||||
return ret;
|
||||
}
|
||||
// TODO: implement - helpful for streaming chunks of data from worker to parent
|
||||
onResponse(_result) { }
|
||||
done(err, result) {
|
||||
var _a;
|
||||
this.runInAsyncScope(this.callback, null, err, result);
|
||||
this.emitDestroy(); // `TaskInfo`s are used only once.
|
||||
// If an abort signal was used, remove the listener from it when
|
||||
// done to make sure we do not accidentally leak.
|
||||
(_a = this._abortCleaner) === null || _a === void 0 ? void 0 : _a.call(this);
|
||||
}
|
||||
get [symbols_1.kQueueOptions]() {
|
||||
var _a, _b;
|
||||
return (_b = (_a = this.task) === null || _a === void 0 ? void 0 : _a[symbols_1.kQueueOptions]) !== null && _b !== void 0 ? _b : null;
|
||||
}
|
||||
get interface() {
|
||||
return {
|
||||
taskId: this.taskId,
|
||||
filename: this.filename,
|
||||
name: this.name,
|
||||
created: this.created,
|
||||
isAbortable: this.abortSignal !== null,
|
||||
[symbols_1.kQueueOptions]: this[symbols_1.kQueueOptions]
|
||||
};
|
||||
}
|
||||
}
|
||||
exports.TaskInfo = TaskInfo;
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/task_queue/index.ts"],"names":[],"mappings":";;;AA0CA,kCASC;AAlDD,qDAA8C;AAC9C,uDAAiD;AAKjD,oCAAwD;AACxD,sCAAsC;AACtC,wCAAkE;AAGlE,6CAA+C;AAAtC,6GAAA,cAAc,OAAA;AACvB,6CAA2C;AAAlC,yGAAA,UAAU,OAAA;AAsBnB;;;;;;GAMG;AACH,SAAgB,WAAW,CAAE,KAAgB;IAC3C,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,O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;YACvB,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,WAAW,EAAE,IAAI,CAAC,WAAW,KAAK,IAAI;YACtC,CAAC,uBAAa,CAAC,EAAE,IAAI,CAAC,uBAAa,CAAC;SACrC,CAAC;IACJ,CAAC;CACJ;AA9FD,4BA8FC"}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
import type { MessagePort, Worker } from 'node:worker_threads';
|
||||
import type { READY } from './common';
|
||||
import type { kTransferable, kValue } from './symbols';
|
||||
export interface StartupMessage {
|
||||
filename: string | null;
|
||||
name: string;
|
||||
port: MessagePort;
|
||||
sharedBuffer: Int32Array;
|
||||
atomics: 'async' | 'sync' | 'disabled';
|
||||
niceIncrement: number;
|
||||
}
|
||||
export interface RequestMessage {
|
||||
taskId: number;
|
||||
task: any;
|
||||
filename: string;
|
||||
name: string;
|
||||
histogramEnabled: number;
|
||||
}
|
||||
export interface ReadyMessage {
|
||||
[READY]: true;
|
||||
}
|
||||
export interface ResponseMessage {
|
||||
taskId: number;
|
||||
result: any;
|
||||
error: Error | null;
|
||||
time: number | null;
|
||||
}
|
||||
export declare const commonState: {
|
||||
isWorkerThread: boolean;
|
||||
workerData: undefined;
|
||||
};
|
||||
export interface Transferable {
|
||||
readonly [kTransferable]: object;
|
||||
readonly [kValue]: object;
|
||||
}
|
||||
export type ResourceLimits = Worker extends {
|
||||
resourceLimits?: infer T;
|
||||
} ? T : {};
|
||||
export type EnvSpecifier = typeof Worker extends {
|
||||
new (filename: never, options?: {
|
||||
env: infer T;
|
||||
}): Worker;
|
||||
} ? T : never;
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.commonState = void 0;
|
||||
exports.commonState = {
|
||||
isWorkerThread: false,
|
||||
workerData: undefined
|
||||
};
|
||||
//# sourceMappingURL=types.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAgCa,QAAA,WAAW,GAAG;IACzB,cAAc,EAAE,KAAK;IACrB,UAAU,EAAE,SAAS;CACtB,CAAC"}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
+234
@@ -0,0 +1,234 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || (function () {
|
||||
var ownKeys = function(o) {
|
||||
ownKeys = Object.getOwnPropertyNames || function (o) {
|
||||
var ar = [];
|
||||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
||||
return ar;
|
||||
};
|
||||
return ownKeys(o);
|
||||
};
|
||||
return function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const node_worker_threads_1 = require("node:worker_threads");
|
||||
const node_url_1 = require("node:url");
|
||||
const node_perf_hooks_1 = require("node:perf_hooks");
|
||||
const symbols_1 = require("./symbols");
|
||||
const common_1 = require("./common");
|
||||
common_1.commonState.isWorkerThread = true;
|
||||
common_1.commonState.workerData = node_worker_threads_1.workerData;
|
||||
/* c8 ignore next*/
|
||||
function noop() { }
|
||||
const handlerCache = new Map();
|
||||
let useAtomics = process.env.PISCINA_DISABLE_ATOMICS !== '1';
|
||||
let useAsyncAtomics = process.env.PISCINA_ENABLE_ASYNC_ATOMICS === '1';
|
||||
// Get `import(x)` as a function that isn't transpiled to `require(x)` by
|
||||
// TypeScript for dual ESM/CJS support.
|
||||
// Load this lazily, so that there is no warning about the ESM loader being
|
||||
// experimental (on Node v12.x) until we actually try to use it.
|
||||
let importESMCached;
|
||||
function getImportESM() {
|
||||
if (importESMCached === undefined) {
|
||||
// eslint-disable-next-line no-new-func
|
||||
importESMCached = new Function('specifier', 'return import(specifier)');
|
||||
}
|
||||
return importESMCached;
|
||||
}
|
||||
// Look up the handler function that we call when a task is posted.
|
||||
// This is either going to be "the" export from a file, or the default export.
|
||||
async function getHandler(filename, name) {
|
||||
let handler = handlerCache.get(`${filename}/${name}`);
|
||||
if (handler != null) {
|
||||
return handler;
|
||||
}
|
||||
try {
|
||||
// With our current set of TypeScript options, this is transpiled to
|
||||
// `require(filename)`.
|
||||
handler = await Promise.resolve(`${filename}`).then(s => __importStar(require(s)));
|
||||
if (typeof handler !== 'function') {
|
||||
handler = await (handler[name]);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
if (typeof handler !== 'function') {
|
||||
handler = await getImportESM()((0, node_url_1.pathToFileURL)(filename).href);
|
||||
if (typeof handler !== 'function') {
|
||||
handler = await (handler[name]);
|
||||
}
|
||||
}
|
||||
if (typeof handler !== 'function') {
|
||||
return null;
|
||||
}
|
||||
// Limit the handler cache size. This should not usually be an issue and is
|
||||
// only provided for pathological cases.
|
||||
/* c8 ignore next */
|
||||
if (handlerCache.size > 1000) {
|
||||
const [[key]] = handlerCache;
|
||||
handlerCache.delete(key);
|
||||
}
|
||||
handlerCache.set(`${filename}/${name}`, handler);
|
||||
return handler;
|
||||
}
|
||||
// We should only receive this message once, when the Worker starts. It gives
|
||||
// us the MessagePort used for receiving tasks, a SharedArrayBuffer for fast
|
||||
// communication using Atomics, and the name of the default filename for tasks
|
||||
// (so we can pre-load and cache the handler).
|
||||
node_worker_threads_1.parentPort.on('message', async (message) => {
|
||||
var _a;
|
||||
const { port, sharedBuffer, filename, name, niceIncrement } = message;
|
||||
if (niceIncrement !== 0) {
|
||||
(_a = (await Promise.resolve().then(() => __importStar(require('@napi-rs/nice'))).catch(noop))) === null || _a === void 0 ? void 0 : _a.nice(niceIncrement);
|
||||
}
|
||||
try {
|
||||
if (filename != null) {
|
||||
await getHandler(filename, name);
|
||||
}
|
||||
const readyMessage = { [common_1.READY]: true };
|
||||
useAtomics = useAtomics !== false && message.atomics !== 'disabled';
|
||||
useAsyncAtomics = useAtomics !== false && (useAsyncAtomics || message.atomics === 'async');
|
||||
node_worker_threads_1.parentPort.postMessage(readyMessage);
|
||||
port.on('message', onMessage.bind(null, port, sharedBuffer));
|
||||
if (useAtomics) {
|
||||
const res = atomicsWaitLoop(port, sharedBuffer);
|
||||
if ((res === null || res === void 0 ? void 0 : res.then) != null)
|
||||
await res;
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
throwInNextTick(error);
|
||||
}
|
||||
});
|
||||
let currentTasks = 0;
|
||||
let lastSeenRequestCount = 0;
|
||||
function atomicsWaitLoop(port, sharedBuffer) {
|
||||
// This function is entered either after receiving the startup message, or
|
||||
// when we are done with a task. In those situations, the *only* thing we
|
||||
// expect to happen next is a 'message' on `port`.
|
||||
// That call would come with the overhead of a C++ → JS boundary crossing,
|
||||
// including async tracking. So, instead, if there is no task currently
|
||||
// running, we wait for a signal from the parent thread using Atomics.wait(),
|
||||
// and read the message from the port instead of generating an event,
|
||||
// in order to avoid that overhead.
|
||||
// The one catch is that this stops asynchronous operations that are still
|
||||
// running from proceeding. Generally, tasks should not spawn asynchronous
|
||||
// operations without waiting for them to finish, though.
|
||||
if (useAsyncAtomics === true) {
|
||||
// @ts-expect-error - for some reason not supported by TS
|
||||
const { async, value } = Atomics.waitAsync(sharedBuffer, symbols_1.kRequestCountField, lastSeenRequestCount);
|
||||
// We do not check for result
|
||||
/* c8 ignore start */
|
||||
return async === true && value.then(() => {
|
||||
lastSeenRequestCount = Atomics.load(sharedBuffer, symbols_1.kRequestCountField);
|
||||
// We have to read messages *after* updating lastSeenRequestCount in order
|
||||
// to avoid race conditions.
|
||||
let entry;
|
||||
while ((entry = (0, node_worker_threads_1.receiveMessageOnPort)(port)) !== undefined) {
|
||||
onMessage(port, sharedBuffer, entry.message);
|
||||
}
|
||||
});
|
||||
/* c8 ignore stop */
|
||||
}
|
||||
while (currentTasks === 0) {
|
||||
// Check whether there are new messages by testing whether the current
|
||||
// number of requests posted by the parent thread matches the number of
|
||||
// requests received.
|
||||
// We do not check for result
|
||||
Atomics.wait(sharedBuffer, symbols_1.kRequestCountField, lastSeenRequestCount);
|
||||
lastSeenRequestCount = Atomics.load(sharedBuffer, symbols_1.kRequestCountField);
|
||||
// We have to read messages *after* updating lastSeenRequestCount in order
|
||||
// to avoid race conditions.
|
||||
let entry;
|
||||
while ((entry = (0, node_worker_threads_1.receiveMessageOnPort)(port)) !== undefined) {
|
||||
onMessage(port, sharedBuffer, entry.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
async function onMessage(port, sharedBuffer, message) {
|
||||
currentTasks++;
|
||||
const { taskId, task, filename, name } = message;
|
||||
let response;
|
||||
let transferList = [];
|
||||
const start = message.histogramEnabled === 1 ? node_perf_hooks_1.performance.now() : null;
|
||||
try {
|
||||
const handler = await getHandler(filename, name);
|
||||
if (handler === null) {
|
||||
throw new Error(`No handler function exported from ${filename}`);
|
||||
}
|
||||
let result = await handler(task);
|
||||
if ((0, common_1.isMovable)(result)) {
|
||||
transferList = transferList.concat(result[symbols_1.kTransferable]);
|
||||
result = result[symbols_1.kValue];
|
||||
}
|
||||
response = {
|
||||
taskId,
|
||||
result,
|
||||
error: null,
|
||||
time: start == null ? null : Math.round(node_perf_hooks_1.performance.now() - start)
|
||||
};
|
||||
if (useAtomics && !useAsyncAtomics) {
|
||||
// If the task used e.g. console.log(), wait for the stream to drain
|
||||
// before potentially entering the `Atomics.wait()` loop, and before
|
||||
// returning the result so that messages will always be printed even
|
||||
// if the process would otherwise be ready to exit.
|
||||
if (process.stdout.writableLength > 0) {
|
||||
await new Promise((resolve) => process.stdout.write('', resolve));
|
||||
}
|
||||
if (process.stderr.writableLength > 0) {
|
||||
await new Promise((resolve) => process.stderr.write('', resolve));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
response = {
|
||||
taskId,
|
||||
result: null,
|
||||
// It may be worth taking a look at the error cloning algorithm we
|
||||
// use in Node.js core here, it's quite a bit more flexible
|
||||
error: error,
|
||||
time: start == null ? null : Math.round(node_perf_hooks_1.performance.now() - start)
|
||||
};
|
||||
}
|
||||
currentTasks--;
|
||||
try {
|
||||
// Post the response to the parent thread, and let it know that we have
|
||||
// an additional message available. If possible, use Atomics.wait()
|
||||
// to wait for the next message.
|
||||
port.postMessage(response, transferList);
|
||||
Atomics.add(sharedBuffer, symbols_1.kResponseCountField, 1);
|
||||
if (useAtomics) {
|
||||
const res = atomicsWaitLoop(port, sharedBuffer);
|
||||
if ((res === null || res === void 0 ? void 0 : res.then) != null)
|
||||
await res;
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
throwInNextTick(error);
|
||||
}
|
||||
}
|
||||
function throwInNextTick(error) {
|
||||
queueMicrotask(() => { throw error; });
|
||||
}
|
||||
//# sourceMappingURL=worker.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+7
@@ -0,0 +1,7 @@
|
||||
import type { PiscinaTask } from '../../task_queue';
|
||||
import type { PiscinaWorker } from '..';
|
||||
export type PiscinaLoadBalancer = (task: PiscinaTask, workers: PiscinaWorker[]) => PiscinaWorker | null;
|
||||
export type LeastBusyBalancerOptions = {
|
||||
maximumUsage: number;
|
||||
};
|
||||
export declare function LeastBusyBalancer(opts: LeastBusyBalancerOptions): PiscinaLoadBalancer;
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.LeastBusyBalancer = LeastBusyBalancer;
|
||||
function LeastBusyBalancer(opts) {
|
||||
const { maximumUsage } = opts;
|
||||
return (task, workers) => {
|
||||
let candidate = null;
|
||||
let checkpoint = maximumUsage;
|
||||
for (const worker of workers) {
|
||||
if (worker.currentUsage === 0) {
|
||||
candidate = worker;
|
||||
break;
|
||||
}
|
||||
if (worker.isRunningAbortableTask)
|
||||
continue;
|
||||
if (task.isAbortable === false &&
|
||||
(worker.currentUsage < checkpoint)) {
|
||||
candidate = worker;
|
||||
checkpoint = worker.currentUsage;
|
||||
}
|
||||
}
|
||||
return candidate;
|
||||
};
|
||||
}
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/worker_pool/balancer/index.ts"],"names":[],"mappings":";;AAWA,8CA2BC;AA3BD,SAAgB,iBAAiB,CAC/B,IAA8B;IAE9B,MAAM,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC;IAE9B,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE;QACvB,IAAI,SAAS,GAAyB,IAAI,CAAC;QAC3C,IAAI,UAAU,GAAG,YAAY,CAAC;QAC9B,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC7B,IAAI,MAAM,CAAC,YAAY,KAAK,CAAC,EAAE,CAAC;gBAC9B,SAAS,GAAG,MAAM,CAAC;gBACnB,MAAM;YACR,CAAC;YAED,IAAI,MAAM,CAAC,sBAAsB;gBAAE,SAAS;YAE5C,IACE,IAAI,CAAC,WAAW,KAAK,KAAK;gBAC1B,CAAC,MAAM,CAAC,YAAY,GAAG,UAAU,CAAC,EAClC,CAAC;gBACD,SAAS,GAAG,MAAM,CAAC;gBACnB,UAAU,GAAG,MAAM,CAAC,YAAY,CAAC;YACnC,CAAC;QACH,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC,CAAC;AACJ,CAAC"}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
export declare abstract class AsynchronouslyCreatedResource {
|
||||
onreadyListeners: (() => void)[] | null;
|
||||
ondestroyListeners: (() => void)[] | null;
|
||||
markAsReady(): void;
|
||||
isReady(): boolean;
|
||||
onReady(fn: () => void): void;
|
||||
onDestroy(fn: () => void): void;
|
||||
markAsDestroyed(): void;
|
||||
isDestroyed(): boolean;
|
||||
abstract currentUsage(): number;
|
||||
}
|
||||
export declare class AsynchronouslyCreatedResourcePool<T extends AsynchronouslyCreatedResource> {
|
||||
pendingItems: Set<T>;
|
||||
readyItems: Set<T>;
|
||||
maximumUsage: number;
|
||||
onAvailableListeners: ((item: T) => void)[];
|
||||
onTaskDoneListeners: ((item: T) => void)[];
|
||||
constructor(maximumUsage: number);
|
||||
add(item: T): void;
|
||||
delete(item: T): void;
|
||||
[Symbol.iterator](): Generator<T, void, unknown>;
|
||||
get size(): number;
|
||||
maybeAvailable(item: T): void;
|
||||
onAvailable(fn: (item: T) => void): void;
|
||||
taskDone(item: T): void;
|
||||
onTaskDone(fn: (item: T) => void): void;
|
||||
getCurrentUsage(): number;
|
||||
}
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AsynchronouslyCreatedResourcePool = exports.AsynchronouslyCreatedResource = void 0;
|
||||
const node_assert_1 = __importDefault(require("node:assert"));
|
||||
class AsynchronouslyCreatedResource {
|
||||
constructor() {
|
||||
this.onreadyListeners = [];
|
||||
this.ondestroyListeners = [];
|
||||
}
|
||||
markAsReady() {
|
||||
const listeners = this.onreadyListeners;
|
||||
(0, node_assert_1.default)(listeners != null);
|
||||
this.onreadyListeners = null;
|
||||
for (const listener of listeners) {
|
||||
listener();
|
||||
}
|
||||
}
|
||||
isReady() {
|
||||
return this.onreadyListeners == null;
|
||||
}
|
||||
onReady(fn) {
|
||||
if (this.onreadyListeners == null) {
|
||||
fn(); // Zalgo is okay here.
|
||||
return;
|
||||
}
|
||||
this.onreadyListeners.push(fn);
|
||||
}
|
||||
onDestroy(fn) {
|
||||
if (this.ondestroyListeners == null) {
|
||||
return;
|
||||
}
|
||||
this.ondestroyListeners.push(fn);
|
||||
}
|
||||
markAsDestroyed() {
|
||||
const listeners = this.ondestroyListeners;
|
||||
(0, node_assert_1.default)(listeners != null);
|
||||
this.ondestroyListeners = null;
|
||||
for (const listener of listeners) {
|
||||
listener();
|
||||
}
|
||||
}
|
||||
isDestroyed() {
|
||||
return this.ondestroyListeners == null;
|
||||
}
|
||||
}
|
||||
exports.AsynchronouslyCreatedResource = AsynchronouslyCreatedResource;
|
||||
// TODO: this will eventually become an scheduler
|
||||
class AsynchronouslyCreatedResourcePool {
|
||||
constructor(maximumUsage) {
|
||||
this.pendingItems = new Set();
|
||||
this.readyItems = new Set();
|
||||
this.maximumUsage = maximumUsage;
|
||||
this.onAvailableListeners = [];
|
||||
this.onTaskDoneListeners = [];
|
||||
}
|
||||
add(item) {
|
||||
this.pendingItems.add(item);
|
||||
item.onReady(() => {
|
||||
/* istanbul ignore else */
|
||||
if (this.pendingItems.has(item)) {
|
||||
this.pendingItems.delete(item);
|
||||
this.readyItems.add(item);
|
||||
this.maybeAvailable(item);
|
||||
}
|
||||
});
|
||||
}
|
||||
delete(item) {
|
||||
this.pendingItems.delete(item);
|
||||
this.readyItems.delete(item);
|
||||
}
|
||||
*[Symbol.iterator]() {
|
||||
yield* this.pendingItems;
|
||||
yield* this.readyItems;
|
||||
}
|
||||
get size() {
|
||||
return this.pendingItems.size + this.readyItems.size;
|
||||
}
|
||||
maybeAvailable(item) {
|
||||
/* istanbul ignore else */
|
||||
if (item.currentUsage() < this.maximumUsage) {
|
||||
for (const listener of this.onAvailableListeners) {
|
||||
listener(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
onAvailable(fn) {
|
||||
this.onAvailableListeners.push(fn);
|
||||
}
|
||||
taskDone(item) {
|
||||
for (let i = 0; i < this.onTaskDoneListeners.length; i++) {
|
||||
this.onTaskDoneListeners[i](item);
|
||||
}
|
||||
}
|
||||
onTaskDone(fn) {
|
||||
this.onTaskDoneListeners.push(fn);
|
||||
}
|
||||
getCurrentUsage() {
|
||||
let inFlight = 0;
|
||||
for (const worker of this.readyItems) {
|
||||
const currentUsage = worker.currentUsage();
|
||||
if (Number.isFinite(currentUsage))
|
||||
inFlight += currentUsage;
|
||||
}
|
||||
return inFlight;
|
||||
}
|
||||
}
|
||||
exports.AsynchronouslyCreatedResourcePool = AsynchronouslyCreatedResourcePool;
|
||||
//# sourceMappingURL=base.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/worker_pool/base.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAiC;AAEjC,MAAsB,6BAA6B;IAAnD;QACI,qBAAgB,GAA2B,EAAE,CAAC;QAC9C,uBAAkB,GAA2B,EAAE,CAAC;IA6CpD,CAAC;IA3CG,WAAW;QACT,MAAM,SAAS,GAAG,IAAI,CAAC,gBAAgB,CAAC;QACxC,IAAA,qBAAM,EAAC,SAAS,IAAI,IAAI,CAAC,CAAC;QAC1B,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC;QAC7B,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YACjC,QAAQ,EAAE,CAAC;QACb,CAAC;IACH,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,gBAAgB,IAAI,IAAI,CAAC;IACvC,CAAC;IAED,OAAO,CAAE,EAAe;QACtB,IAAI,IAAI,CAAC,gBAAgB,IAAI,IAAI,EAAE,CAAC;YAClC,EAAE,EAAE,CAAC,CA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;IAED,eAAe;QACb,IAAI,QAAQ,GAAG,CAAC,CAAC;QACjB,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACrC,MAAM,YAAY,GAAG,MAAM,CAAC,YAAY,EAAE,CAAC;YAE3C,IAAI,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC;gBAAE,QAAQ,IAAI,YAAY,CAAC;QAC9D,CAAC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF;AAzED,8EAyEC"}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
import { Worker, MessagePort, WorkerOptions, Transferable } from 'node:worker_threads';
|
||||
import { RecordableHistogram } from 'node:perf_hooks';
|
||||
import { ResponseMessage, StartupMessage } from '../types';
|
||||
import { TaskInfo } from '../task_queue';
|
||||
import { kWorkerData } from '../symbols';
|
||||
import { PiscinaHistogramSummary } from '../histogram';
|
||||
import { AsynchronouslyCreatedResource, AsynchronouslyCreatedResourcePool } from './base';
|
||||
export * from './balancer';
|
||||
type ResponseCallback = (response: ResponseMessage) => void;
|
||||
export type PiscinaWorker = {
|
||||
id: number;
|
||||
currentUsage: number;
|
||||
isRunningAbortableTask: boolean;
|
||||
histogram: PiscinaHistogramSummary | null;
|
||||
terminating: boolean;
|
||||
destroyed: boolean;
|
||||
[kWorkerData]: WorkerInfo;
|
||||
};
|
||||
type WorkerInfoParams = {
|
||||
worker: {
|
||||
filename: string;
|
||||
} & WorkerOptions;
|
||||
port: MessagePort;
|
||||
enableHistogram: boolean;
|
||||
};
|
||||
export declare class WorkerInfo extends AsynchronouslyCreatedResource {
|
||||
worker: Worker;
|
||||
taskInfos: Map<number, TaskInfo>;
|
||||
idleTimeout: NodeJS.Timeout | null;
|
||||
port: MessagePort;
|
||||
sharedBuffer: Int32Array;
|
||||
lastSeenResponseCount: number;
|
||||
onMessage: ResponseCallback;
|
||||
histogram: RecordableHistogram | null;
|
||||
terminating: boolean;
|
||||
destroyed: boolean;
|
||||
constructor({ worker, port, enableHistogram }: WorkerInfoParams, onMessage: ResponseCallback);
|
||||
get id(): number;
|
||||
onWorkerMessage(handler: (msg: any) => void): void;
|
||||
onWorkerError(handler: (err: Error) => void): void;
|
||||
onWorkerExit(handler: (code: number) => void): void;
|
||||
onPortClose(handler: () => void): void;
|
||||
init(msg: StartupMessage, toTransfer: Transferable[]): WorkerInfo;
|
||||
workerRef(): WorkerInfo;
|
||||
workerUnref(): WorkerInfo;
|
||||
destroy(): void;
|
||||
setIdleTimeout(handler: (_: void) => void, ms: number, ...args: any[]): void;
|
||||
clearIdleTimeout(): void;
|
||||
ref(): WorkerInfo;
|
||||
unref(): WorkerInfo;
|
||||
_handleResponse(message: ResponseMessage): void;
|
||||
postTask(taskInfo: TaskInfo): void;
|
||||
processPendingMessages(): void;
|
||||
isRunningAbortableTask(): boolean;
|
||||
currentUsage(): number;
|
||||
popTask(taskId: number): TaskInfo | null;
|
||||
get interface(): PiscinaWorker;
|
||||
}
|
||||
export { AsynchronouslyCreatedResourcePool };
|
||||
+205
@@ -0,0 +1,205 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AsynchronouslyCreatedResourcePool = exports.WorkerInfo = void 0;
|
||||
const node_worker_threads_1 = require("node:worker_threads");
|
||||
const node_perf_hooks_1 = require("node:perf_hooks");
|
||||
const node_assert_1 = __importDefault(require("node:assert"));
|
||||
const errors_1 = require("../errors");
|
||||
const symbols_1 = require("../symbols");
|
||||
const histogram_1 = require("../histogram");
|
||||
const base_1 = require("./base");
|
||||
Object.defineProperty(exports, "AsynchronouslyCreatedResourcePool", { enumerable: true, get: function () { return base_1.AsynchronouslyCreatedResourcePool; } });
|
||||
__exportStar(require("./balancer"), exports);
|
||||
class WorkerInfo extends base_1.AsynchronouslyCreatedResource {
|
||||
constructor({ worker, port, enableHistogram }, onMessage) {
|
||||
super();
|
||||
this.idleTimeout = null;
|
||||
this.lastSeenResponseCount = 0;
|
||||
this.terminating = false;
|
||||
this.destroyed = false;
|
||||
const { filename, ...workerOpts } = worker;
|
||||
this.worker = new node_worker_threads_1.Worker(filename, workerOpts);
|
||||
this.port = port;
|
||||
this.onMessage = onMessage;
|
||||
this.port.on('message', this._handleResponse.bind(this));
|
||||
this.taskInfos = new Map();
|
||||
this.sharedBuffer = new Int32Array(new SharedArrayBuffer(symbols_1.kFieldCount * Int32Array.BYTES_PER_ELEMENT));
|
||||
this.histogram = enableHistogram ? (0, node_perf_hooks_1.createHistogram)() : null;
|
||||
}
|
||||
get id() {
|
||||
return this.worker.threadId;
|
||||
}
|
||||
onWorkerMessage(handler) {
|
||||
this.worker.on('message', handler);
|
||||
}
|
||||
onWorkerError(handler) {
|
||||
this.worker.on('error', handler);
|
||||
}
|
||||
onWorkerExit(handler) {
|
||||
this.worker.on('exit', handler);
|
||||
}
|
||||
onPortClose(handler) {
|
||||
this.port.on('close', handler);
|
||||
}
|
||||
init(msg, toTransfer) {
|
||||
this.worker.postMessage(msg, toTransfer);
|
||||
return this;
|
||||
}
|
||||
workerRef() {
|
||||
this.worker.ref();
|
||||
return this;
|
||||
}
|
||||
workerUnref() {
|
||||
this.worker.unref();
|
||||
return this;
|
||||
}
|
||||
destroy() {
|
||||
if (this.terminating || this.destroyed)
|
||||
return;
|
||||
this.terminating = true;
|
||||
this.clearIdleTimeout();
|
||||
this.worker.terminate();
|
||||
this.port.close();
|
||||
for (const taskInfo of this.taskInfos.values()) {
|
||||
taskInfo.done(errors_1.Errors.ThreadTermination());
|
||||
}
|
||||
this.taskInfos.clear();
|
||||
this.terminating = false;
|
||||
this.destroyed = true;
|
||||
this.markAsDestroyed();
|
||||
}
|
||||
setIdleTimeout(handler, ms, ...args) {
|
||||
this.idleTimeout = setTimeout(handler, ms, ...args).unref();
|
||||
}
|
||||
clearIdleTimeout() {
|
||||
if (this.idleTimeout != null) {
|
||||
clearTimeout(this.idleTimeout);
|
||||
this.idleTimeout = null;
|
||||
}
|
||||
}
|
||||
ref() {
|
||||
this.port.ref();
|
||||
return this;
|
||||
}
|
||||
unref() {
|
||||
this.port.unref();
|
||||
return this;
|
||||
}
|
||||
_handleResponse(message) {
|
||||
var _a;
|
||||
// Both cannot be in different state if histogram enabled.
|
||||
(_a = this.histogram) === null || _a === void 0 ? void 0 : _a.record(histogram_1.PiscinaHistogramHandler.toHistogramIntegerNano(message === null || message === void 0 ? void 0 : message.time));
|
||||
this.onMessage(message);
|
||||
if (this.taskInfos.size === 0) {
|
||||
// No more tasks running on this Worker means it should not keep the
|
||||
// process running.
|
||||
this.unref();
|
||||
}
|
||||
}
|
||||
postTask(taskInfo) {
|
||||
// Avoid duplicates
|
||||
(0, node_assert_1.default)(!this.taskInfos.has(taskInfo.taskId));
|
||||
// Avoid posting when pool is shutting down or worker already destroyed
|
||||
(0, node_assert_1.default)(!this.terminating && !this.destroyed);
|
||||
const message = {
|
||||
task: taskInfo.releaseTask(),
|
||||
taskId: taskInfo.taskId,
|
||||
filename: taskInfo.filename,
|
||||
name: taskInfo.name,
|
||||
histogramEnabled: this.histogram != null ? 1 : 0
|
||||
};
|
||||
try {
|
||||
this.port.postMessage(message, taskInfo.transferList);
|
||||
queueMicrotask(() => this.clearIdleTimeout());
|
||||
taskInfo.workerInfo = this;
|
||||
this.taskInfos.set(taskInfo.taskId, taskInfo);
|
||||
this.ref();
|
||||
// Inform the worker that there are new messages posted, and wake it up
|
||||
// if it is waiting for one.
|
||||
Atomics.add(this.sharedBuffer, symbols_1.kRequestCountField, 1);
|
||||
Atomics.notify(this.sharedBuffer, symbols_1.kRequestCountField, 1);
|
||||
}
|
||||
catch (err) {
|
||||
// This would mostly happen if e.g. message contains unserializable data
|
||||
// or transferList is invalid.
|
||||
taskInfo.done(err);
|
||||
}
|
||||
}
|
||||
processPendingMessages() {
|
||||
if (this.destroyed)
|
||||
return;
|
||||
// If we *know* that there are more messages than we have received using
|
||||
// 'message' events yet, then try to load and handle them synchronously,
|
||||
// without the need to wait for more expensive events on the event loop.
|
||||
// This would usually break async tracking, but in our case, we already have
|
||||
// the extra TaskInfo/AsyncResource layer that rectifies that situation.
|
||||
const actualResponseCount = Atomics.load(this.sharedBuffer, symbols_1.kResponseCountField);
|
||||
if (actualResponseCount !== this.lastSeenResponseCount) {
|
||||
this.lastSeenResponseCount = actualResponseCount;
|
||||
let entry;
|
||||
while ((entry = (0, node_worker_threads_1.receiveMessageOnPort)(this.port)) != null) {
|
||||
this._handleResponse(entry.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
isRunningAbortableTask() {
|
||||
// If there are abortable tasks, we are running one at most per Worker.
|
||||
if (this.taskInfos.size !== 1)
|
||||
return false;
|
||||
const [[, task]] = this.taskInfos;
|
||||
return task.abortSignal != null;
|
||||
}
|
||||
currentUsage() {
|
||||
return this.isRunningAbortableTask() ? Infinity : this.taskInfos.size;
|
||||
}
|
||||
popTask(taskId) {
|
||||
var _a;
|
||||
const task = (_a = this.taskInfos.get(taskId)) !== null && _a !== void 0 ? _a : null;
|
||||
if (task != null)
|
||||
this.taskInfos.delete(taskId);
|
||||
return task;
|
||||
}
|
||||
get interface() {
|
||||
const worker = this;
|
||||
return {
|
||||
get id() {
|
||||
return worker.worker.threadId;
|
||||
},
|
||||
get currentUsage() {
|
||||
return worker.currentUsage();
|
||||
},
|
||||
get isRunningAbortableTask() {
|
||||
return worker.isRunningAbortableTask();
|
||||
},
|
||||
get histogram() {
|
||||
return worker.histogram != null ? histogram_1.PiscinaHistogramHandler.createHistogramSummary(worker.histogram) : null;
|
||||
},
|
||||
get terminating() {
|
||||
return worker.terminating;
|
||||
},
|
||||
get destroyed() {
|
||||
return worker.destroyed;
|
||||
},
|
||||
[symbols_1.kWorkerData]: worker
|
||||
};
|
||||
}
|
||||
}
|
||||
exports.WorkerInfo = WorkerInfo;
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user