poolifier 2.1.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/index.d.ts +808 -15
- package/lib/index.js +1 -1
- package/package.json +31 -27
- package/CHANGELOG.md +0 -107
- package/lib/pools/abstract-pool.d.ts +0 -245
- package/lib/pools/cluster/dynamic.d.ts +0 -30
- package/lib/pools/cluster/fixed.d.ts +0 -58
- package/lib/pools/pool-internal.d.ts +0 -74
- package/lib/pools/pool.d.ts +0 -26
- package/lib/pools/selection-strategies.d.ts +0 -58
- package/lib/pools/thread/dynamic.d.ts +0 -31
- package/lib/pools/thread/fixed.d.ts +0 -48
- package/lib/utility-types.d.ts +0 -58
- package/lib/utils.d.ts +0 -4
- package/lib/worker/abstract-worker.d.ts +0 -79
- package/lib/worker/cluster-worker.d.ts +0 -32
- package/lib/worker/thread-worker.d.ts +0 -30
- package/lib/worker/worker-options.d.ts +0 -61
package/lib/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("events"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("events"),r=require("cluster"),t=require("worker_threads"),s=require("async_hooks");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i,n=o(e),a=o(r);!function(e){e.FIXED="fixed",e.DYNAMIC="dynamic"}(i||(i={}));class h extends n.default{}const c=()=>{},l=Object.freeze({SOFT:"SOFT",HARD:"HARD"});const k=Object.freeze({ROUND_ROBIN:"ROUND_ROBIN",LESS_RECENTLY_USED:"LESS_RECENTLY_USED"});class u{constructor(e){this.pool=e,this.isDynamicPool=this.pool.type===i.DYNAMIC}}class p extends u{choose(){let e,r=1/0;for(const t of this.pool.workers){const s=this.pool.getWorkerRunningTasks(t);if(!1===this.isDynamicPool&&0===s)return t;s<r&&(e=t,r=s)}return e}}class d extends u{constructor(){super(...arguments),this.nextWorkerIndex=0}choose(){const e=this.pool.workers[this.nextWorkerIndex];return this.nextWorkerIndex=this.nextWorkerIndex===this.pool.workers.length-1?0:this.nextWorkerIndex+1,e}}class W{static getWorkerChoiceStrategy(e,r=k.ROUND_ROBIN){switch(r){case k.ROUND_ROBIN:return new d(e);case k.LESS_RECENTLY_USED:return new p(e);default:throw new Error(`Worker choice strategy '${r}' not found`)}}}class g extends u{constructor(e,r,t=k.ROUND_ROBIN){super(e),this.createDynamicallyWorkerCallback=r,this.workerChoiceStrategy=W.getWorkerChoiceStrategy(this.pool,t)}choose(){const e=this.pool.findFreeWorker();return e||(this.pool.busy?this.workerChoiceStrategy.choose():this.createDynamicallyWorkerCallback())}}class m{constructor(e,r,t=k.ROUND_ROBIN){this.pool=e,this.createDynamicallyWorkerCallback=r,this.setWorkerChoiceStrategy(t)}getPoolWorkerChoiceStrategy(e=k.ROUND_ROBIN){return this.pool.type===i.DYNAMIC?new g(this.pool,this.createDynamicallyWorkerCallback,e):W.getWorkerChoiceStrategy(this.pool,e)}setWorkerChoiceStrategy(e){this.workerChoiceStrategy=this.getPoolWorkerChoiceStrategy(e)}execute(){return this.workerChoiceStrategy.choose()}}class y{constructor(e,r,t){if(this.numberOfWorkers=e,this.filePath=r,this.opts=t,this.workers=[],this.tasks=new Map,this.promiseMap=new Map,this.nextMessageId=0,!this.isMain())throw new Error("Cannot start a pool from a worker!");this.checkNumberOfWorkers(this.numberOfWorkers),this.checkFilePath(this.filePath),this.checkPoolOptions(this.opts),this.setupHook();for(let e=1;e<=this.numberOfWorkers;e++)this.createAndSetupWorker();this.opts.enableEvents&&(this.emitter=new h),this.workerChoiceStrategyContext=new m(this,(()=>{const e=this.createAndSetupWorker();return this.registerWorkerMessageListener(e,(r=>{var t;t=l.HARD,(r.kill===t||0===this.getWorkerRunningTasks(e))&&this.destroyWorker(e)})),e}),this.opts.workerChoiceStrategy)}checkFilePath(e){if(!e)throw new Error("Please specify a file with a worker implementation")}checkNumberOfWorkers(e){if(null==e)throw new Error("Cannot instantiate a pool without specifying the number of workers");if(!Number.isSafeInteger(e))throw new Error("Cannot instantiate a pool with a non integer number of workers");if(e<0)throw new Error("Cannot instantiate a pool with a negative number of workers");if(this.type===i.FIXED&&0===e)throw new Error("Cannot instantiate a fixed pool with no worker")}checkPoolOptions(e){var r,t;this.opts.workerChoiceStrategy=null!==(r=e.workerChoiceStrategy)&&void 0!==r?r:k.ROUND_ROBIN,this.opts.enableEvents=null===(t=e.enableEvents)||void 0===t||t}get numberOfRunningTasks(){return this.promiseMap.size}getWorkerRunningTasks(e){return this.tasks.get(e)}getWorkerIndex(e){return this.workers.indexOf(e)}setWorkerChoiceStrategy(e){this.opts.workerChoiceStrategy=e,this.workerChoiceStrategyContext.setWorkerChoiceStrategy(e)}internalGetBusyStatus(){return this.numberOfRunningTasks>=this.numberOfWorkers&&!1===this.findFreeWorker()}findFreeWorker(){for(const e of this.workers)if(0===this.getWorkerRunningTasks(e))return e;return!1}execute(e){const r=this.chooseWorker(),t=++this.nextMessageId,s=this.internalExecute(r,t);return this.checkAndEmitBusy(),e=null!=e?e:{},this.sendToWorker(r,{data:e,id:t}),s}async destroy(){await Promise.all(this.workers.map((e=>this.destroyWorker(e))))}setupHook(){}increaseWorkersTask(e){this.stepWorkerNumberOfTasks(e,1)}decreaseWorkersTasks(e){this.stepWorkerNumberOfTasks(e,-1)}stepWorkerNumberOfTasks(e,r){const t=this.tasks.get(e);if(void 0===t)throw Error("Worker could not be found in tasks map");this.tasks.set(e,t+r)}removeWorker(e){this.workers.splice(this.getWorkerIndex(e),1),this.tasks.delete(e)}chooseWorker(){return this.workerChoiceStrategyContext.execute()}internalExecute(e,r){return this.increaseWorkersTask(e),new Promise(((t,s)=>{this.promiseMap.set(r,{resolve:t,reject:s,worker:e})}))}createAndSetupWorker(){var e,r,t,s;const o=this.createWorker();return o.on("message",null!==(e=this.opts.messageHandler)&&void 0!==e?e:c),o.on("error",null!==(r=this.opts.errorHandler)&&void 0!==r?r:c),o.on("online",null!==(t=this.opts.onlineHandler)&&void 0!==t?t:c),o.on("exit",null!==(s=this.opts.exitHandler)&&void 0!==s?s:c),o.once("exit",(()=>this.removeWorker(o))),this.workers.push(o),this.tasks.set(o,0),this.afterWorkerSetup(o),o}workerListener(){return e=>{if(void 0!==e.id){const r=this.promiseMap.get(e.id);void 0!==r&&(this.decreaseWorkersTasks(r.worker),e.error?r.reject(e.error):r.resolve(e.data),this.promiseMap.delete(e.id))}}}checkAndEmitBusy(){var e;this.opts.enableEvents&&this.busy&&(null===(e=this.emitter)||void 0===e||e.emit("busy"))}}class w extends y{constructor(e,r,t={}){super(e,r,t),this.opts=t}setupHook(){a.default.setupPrimary({exec:this.filePath})}isMain(){return a.default.isPrimary}destroyWorker(e){this.sendToWorker(e,{kill:1}),e.kill()}sendToWorker(e,r){e.send(r)}registerWorkerMessageListener(e,r){e.on("message",r)}createWorker(){return a.default.fork(this.opts.env)}afterWorkerSetup(e){this.registerWorkerMessageListener(e,super.workerListener())}get type(){return i.FIXED}get busy(){return this.internalGetBusyStatus()}}class f extends y{constructor(e,r,t={}){super(e,r,t)}isMain(){return t.isMainThread}async destroyWorker(e){this.sendToWorker(e,{kill:1}),await e.terminate()}sendToWorker(e,r){e.postMessage(r)}registerWorkerMessageListener(e,r){var t;null===(t=e.port2)||void 0===t||t.on("message",r)}createWorker(){return new t.Worker(this.filePath,{env:t.SHARE_ENV})}afterWorkerSetup(e){const{port1:r,port2:s}=new t.MessageChannel;e.postMessage({parent:r},[r]),e.port1=r,e.port2=s,this.registerWorkerMessageListener(e,super.workerListener())}get type(){return i.FIXED}get busy(){return this.internalGetBusyStatus()}}const v=l.SOFT;class x extends s.AsyncResource{constructor(e,r,t,s,o={killBehavior:v,maxInactiveTime:6e4}){var i,n;super(e),this.mainWorker=s,this.opts=o,this.checkFunctionInput(t),this.checkWorkerOptions(this.opts),this.lastTaskTimestamp=Date.now(),r||(this.aliveInterval=setInterval(this.checkAlive.bind(this),(null!==(i=this.opts.maxInactiveTime)&&void 0!==i?i:6e4)/2),this.checkAlive.bind(this)()),null===(n=this.mainWorker)||void 0===n||n.on("message",(e=>{this.messageListener(e,t)}))}messageListener(e,r){void 0!==e.data&&void 0!==e.id?this.opts.async?this.runInAsyncScope(this.runAsync.bind(this),this,r,e):this.runInAsyncScope(this.run.bind(this),this,r,e):void 0!==e.parent?this.mainWorker=e.parent:void 0!==e.kill&&(this.aliveInterval&&clearInterval(this.aliveInterval),this.emitDestroy())}checkWorkerOptions(e){var r,t;this.opts.killBehavior=null!==(r=e.killBehavior)&&void 0!==r?r:v,this.opts.maxInactiveTime=null!==(t=e.maxInactiveTime)&&void 0!==t?t:6e4,this.opts.async=!!e.async}checkFunctionInput(e){if(!e)throw new Error("fn parameter is mandatory")}getMainWorker(){if(!this.mainWorker)throw new Error("Main worker was not set");return this.mainWorker}checkAlive(){var e;Date.now()-this.lastTaskTimestamp>(null!==(e=this.opts.maxInactiveTime)&&void 0!==e?e:6e4)&&this.sendToMainWorker({kill:this.opts.killBehavior})}handleError(e){return e}run(e,r){try{const t=e(r.data);this.sendToMainWorker({data:t,id:r.id})}catch(e){const t=this.handleError(e);this.sendToMainWorker({error:t,id:r.id})}finally{this.lastTaskTimestamp=Date.now()}}runAsync(e,r){e(r.data).then((e=>(this.sendToMainWorker({data:e,id:r.id}),null))).catch((e=>{const t=this.handleError(e);this.sendToMainWorker({error:t,id:r.id})})).finally((()=>{this.lastTaskTimestamp=Date.now()})).catch(c)}}exports.AbstractWorker=x,exports.ClusterWorker=class extends x{constructor(e,r={}){super("worker-cluster-pool:poolifier",a.default.isPrimary,e,a.default.worker,r)}sendToMainWorker(e){this.getMainWorker().send(e)}handleError(e){return e instanceof Error?e.message:e}},exports.DynamicClusterPool=class extends w{constructor(e,r,t,s={}){super(e,t,s),this.max=r}get type(){return i.DYNAMIC}get busy(){return this.workers.length===this.max}},exports.DynamicThreadPool=class extends f{constructor(e,r,t,s={}){super(e,t,s),this.max=r}get type(){return i.DYNAMIC}get busy(){return this.workers.length===this.max}},exports.FixedClusterPool=w,exports.FixedThreadPool=f,exports.KillBehaviors=l,exports.ThreadWorker=class extends x{constructor(e,r={}){super("worker-thread-pool:poolifier",t.isMainThread,e,t.parentPort,r)}sendToMainWorker(e){this.getMainWorker().postMessage(e)}},exports.WorkerChoiceStrategies=k;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "poolifier",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "A fast, easy to use Node.js Worker Thread Pool and Cluster Pool implementation",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -16,9 +16,10 @@
|
|
|
16
16
|
"coverage": "nyc report --reporter=lcov",
|
|
17
17
|
"coverage:html": "nyc report --reporter=html",
|
|
18
18
|
"format": "prettier --loglevel silent --write .; prettierx --write .",
|
|
19
|
-
"lint": "eslint .",
|
|
20
|
-
"lint:fix": "eslint . --fix",
|
|
19
|
+
"lint": "eslint . --cache",
|
|
20
|
+
"lint:fix": "eslint . --cache --fix",
|
|
21
21
|
"typedoc": "typedoc",
|
|
22
|
+
"sonar:properties": "./updateSonarProps.sh",
|
|
22
23
|
"prepublishOnly": "npm run build:prod"
|
|
23
24
|
},
|
|
24
25
|
"repository": {
|
|
@@ -62,40 +63,43 @@
|
|
|
62
63
|
"lib"
|
|
63
64
|
],
|
|
64
65
|
"devDependencies": {
|
|
65
|
-
"@types/node": "^
|
|
66
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
67
|
-
"@typescript-eslint/parser": "^
|
|
66
|
+
"@types/node": "^18.8.3",
|
|
67
|
+
"@typescript-eslint/eslint-plugin": "^5.39.0",
|
|
68
|
+
"@typescript-eslint/parser": "^5.39.0",
|
|
68
69
|
"benchmark": "^2.1.4",
|
|
69
|
-
"eslint": "^
|
|
70
|
-
"eslint-config-standard": "^
|
|
71
|
-
"eslint-define-config": "^1.0
|
|
72
|
-
"eslint-
|
|
73
|
-
"eslint-plugin-
|
|
70
|
+
"eslint": "^8.25.0",
|
|
71
|
+
"eslint-config-standard": "^17.0.0",
|
|
72
|
+
"eslint-define-config": "^1.7.0",
|
|
73
|
+
"eslint-import-resolver-typescript": "^3.5.1",
|
|
74
|
+
"eslint-plugin-import": "^2.26.0",
|
|
75
|
+
"eslint-plugin-jsdoc": "^39.3.6",
|
|
76
|
+
"eslint-plugin-n": "^15.3.0",
|
|
74
77
|
"eslint-plugin-node": "^11.1.0",
|
|
75
78
|
"eslint-plugin-prettierx": "^0.18.0",
|
|
76
|
-
"eslint-plugin-promise": "^
|
|
77
|
-
"eslint-plugin-spellcheck": "0.0.19",
|
|
78
|
-
"expect": "^
|
|
79
|
-
"microtime": "^3.
|
|
80
|
-
"mocha": "^
|
|
81
|
-
"mochawesome": "^
|
|
79
|
+
"eslint-plugin-promise": "^6.0.1",
|
|
80
|
+
"eslint-plugin-spellcheck": "^0.0.19",
|
|
81
|
+
"expect": "^29.1.2",
|
|
82
|
+
"microtime": "^3.1.1",
|
|
83
|
+
"mocha": "^10.0.0",
|
|
84
|
+
"mochawesome": "^7.1.3",
|
|
82
85
|
"nyc": "^15.1.0",
|
|
83
|
-
"prettier": "^2.
|
|
84
|
-
"prettier-plugin-organize-imports": "^
|
|
85
|
-
"prettierx": "^0.
|
|
86
|
-
"rollup": "^2.
|
|
86
|
+
"prettier": "^2.7.1",
|
|
87
|
+
"prettier-plugin-organize-imports": "^3.1.1",
|
|
88
|
+
"prettierx": "^0.18.3",
|
|
89
|
+
"rollup": "^2.79.1",
|
|
87
90
|
"rollup-plugin-analyzer": "^4.0.0",
|
|
88
91
|
"rollup-plugin-command": "^1.1.3",
|
|
89
92
|
"rollup-plugin-delete": "^2.0.0",
|
|
90
93
|
"rollup-plugin-istanbul": "^3.0.0",
|
|
91
94
|
"rollup-plugin-terser": "^7.0.2",
|
|
92
|
-
"rollup-plugin-
|
|
93
|
-
"
|
|
94
|
-
"
|
|
95
|
-
"
|
|
95
|
+
"rollup-plugin-ts": "^3.0.2",
|
|
96
|
+
"sinon": "^14.0.1",
|
|
97
|
+
"source-map-support": "^0.5.21",
|
|
98
|
+
"typedoc": "^0.23.15",
|
|
99
|
+
"typescript": "^4.8.4"
|
|
96
100
|
},
|
|
97
101
|
"engines": {
|
|
98
|
-
"node": ">=
|
|
99
|
-
"npm": ">=
|
|
102
|
+
"node": ">=16.0.0",
|
|
103
|
+
"npm": ">=8.0.0"
|
|
100
104
|
}
|
|
101
105
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
|
|
5
|
-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
-
|
|
8
|
-
## [2.1.0] - 2021-29-08
|
|
9
|
-
|
|
10
|
-
### Added
|
|
11
|
-
|
|
12
|
-
- Add an optional pool option `messageHandler` to `PoolOptions<Worker>` for registering a message handler callback on each worker.
|
|
13
|
-
|
|
14
|
-
### Breaking Changes
|
|
15
|
-
|
|
16
|
-
- `AbstractWorker` class `maxInactiveTime`, `killBehavior` and `async` attributes have been removed in favour of the same ones in the worker options `opts` public attribute.
|
|
17
|
-
- `AbstractWorker` class `lastTask` attribute have been renamed to `lastTaskTimestamp`.
|
|
18
|
-
- `AbstractWorker` class `interval` attribute have been renamed to `aliveInterval`.
|
|
19
|
-
- `AbstractWorker` class cannot be instantiated without specifying the `mainWorker` argument referencing the main worker.
|
|
20
|
-
|
|
21
|
-
## [2.0.2] - 2021-12-05
|
|
22
|
-
|
|
23
|
-
### Bug fixes
|
|
24
|
-
|
|
25
|
-
- Fix `busy` event emission on fixed pool type
|
|
26
|
-
|
|
27
|
-
## [2.0.1] - 2021-16-03
|
|
28
|
-
|
|
29
|
-
### Bug fixes
|
|
30
|
-
|
|
31
|
-
- Check if pool options are properly set.
|
|
32
|
-
- `busy` event is emitted on all pool types.
|
|
33
|
-
|
|
34
|
-
## [2.0.0] - 2021-01-03
|
|
35
|
-
|
|
36
|
-
### Bug fixes
|
|
37
|
-
|
|
38
|
-
- Now a thread/process by default is not deleted when the task submitted take more time than maxInactiveTime configured (issue #70).
|
|
39
|
-
|
|
40
|
-
### Breaking Changes
|
|
41
|
-
|
|
42
|
-
- `FullPool` event is now renamed to `busy`.
|
|
43
|
-
- `maxInactiveTime` on `ThreadWorker` default behavior is now changed, if you want to keep the old behavior set `killBehavior` to `KillBehaviors.HARD`.
|
|
44
|
-
_Find more details on our JSDoc._
|
|
45
|
-
|
|
46
|
-
- `maxTasks` option on `FixedThreadPool` and `DynamicThreadPool` is now removed since is no more needed.
|
|
47
|
-
|
|
48
|
-
- We changed some internal structures, but you shouldn't be too affected by them as these are internal changes.
|
|
49
|
-
|
|
50
|
-
### Pool options types declaration merge
|
|
51
|
-
|
|
52
|
-
`FixedThreadPoolOptions` and `DynamicThreadPoolOptions` type declarations have been merged to `PoolOptions<Worker>`.
|
|
53
|
-
|
|
54
|
-
#### New `export` strategy
|
|
55
|
-
|
|
56
|
-
```js
|
|
57
|
-
// Before
|
|
58
|
-
const DynamicThreadPool = require('poolifier/lib/dynamic')
|
|
59
|
-
// After
|
|
60
|
-
const { DynamicThreadPool } = require('poolifier/lib/dynamic')
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
But you should always prefer just using
|
|
64
|
-
|
|
65
|
-
```js
|
|
66
|
-
const { DynamicThreadPool } = require('poolifier')
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
#### New type definitions for input data and response
|
|
70
|
-
|
|
71
|
-
For cluster worker and worker-thread pools, you can now only send and receive serializable data.
|
|
72
|
-
_This is not a limitation by poolifier but NodeJS._
|
|
73
|
-
|
|
74
|
-
#### Public property replacements
|
|
75
|
-
|
|
76
|
-
`numWorkers` property is now `numberOfWorkers`
|
|
77
|
-
|
|
78
|
-
#### Internal (protected) properties and methods renaming
|
|
79
|
-
|
|
80
|
-
These properties are not intended for end users
|
|
81
|
-
|
|
82
|
-
- `id` => `nextMessageId`
|
|
83
|
-
|
|
84
|
-
These methods are not intended for end users
|
|
85
|
-
|
|
86
|
-
- `_chooseWorker` => `chooseWorker`
|
|
87
|
-
- `_newWorker` => `createWorker`
|
|
88
|
-
- `_execute` => `internalExecute`
|
|
89
|
-
- `_chooseWorker` => `chooseWorker`
|
|
90
|
-
- `_checkAlive` => `checkAlive`
|
|
91
|
-
- `_run` => `run`
|
|
92
|
-
- `_runAsync` => `runAsync`
|
|
93
|
-
|
|
94
|
-
## [1.1.0] - 2020-21-05
|
|
95
|
-
|
|
96
|
-
### Added
|
|
97
|
-
|
|
98
|
-
- ThreadWorker support async functions as option
|
|
99
|
-
- Various external library patches
|
|
100
|
-
|
|
101
|
-
## [1.0.0] - 2020-24-01
|
|
102
|
-
|
|
103
|
-
### Added
|
|
104
|
-
|
|
105
|
-
- FixedThreadPool implementation
|
|
106
|
-
- DynamicThreadPool implementation
|
|
107
|
-
- WorkerThread implementation to improve developer experience
|
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
import type { MessageValue, PromiseWorkerResponseWrapper } from '../utility-types';
|
|
2
|
-
import type { IPoolInternal } from './pool-internal';
|
|
3
|
-
import { PoolEmitter, PoolType } from './pool-internal';
|
|
4
|
-
import type { WorkerChoiceStrategy } from './selection-strategies';
|
|
5
|
-
import { WorkerChoiceStrategyContext } from './selection-strategies';
|
|
6
|
-
/**
|
|
7
|
-
* Callback invoked if the worker has received a message.
|
|
8
|
-
*/
|
|
9
|
-
export declare type MessageHandler<Worker> = (this: Worker, m: unknown) => void;
|
|
10
|
-
/**
|
|
11
|
-
* Callback invoked if the worker raised an error.
|
|
12
|
-
*/
|
|
13
|
-
export declare type ErrorHandler<Worker> = (this: Worker, e: Error) => void;
|
|
14
|
-
/**
|
|
15
|
-
* Callback invoked when the worker has started successfully.
|
|
16
|
-
*/
|
|
17
|
-
export declare type OnlineHandler<Worker> = (this: Worker) => void;
|
|
18
|
-
/**
|
|
19
|
-
* Callback invoked when the worker exits successfully.
|
|
20
|
-
*/
|
|
21
|
-
export declare type ExitHandler<Worker> = (this: Worker, code: number) => void;
|
|
22
|
-
/**
|
|
23
|
-
* Basic interface that describes the minimum required implementation of listener events for a pool-worker.
|
|
24
|
-
*/
|
|
25
|
-
export interface IWorker {
|
|
26
|
-
/**
|
|
27
|
-
* Register a listener to the message event.
|
|
28
|
-
*
|
|
29
|
-
* @param event `'message'`.
|
|
30
|
-
* @param handler The message handler.
|
|
31
|
-
*/
|
|
32
|
-
on(event: 'message', handler: MessageHandler<this>): void;
|
|
33
|
-
/**
|
|
34
|
-
* Register a listener to the error event.
|
|
35
|
-
*
|
|
36
|
-
* @param event `'error'`.
|
|
37
|
-
* @param handler The error handler.
|
|
38
|
-
*/
|
|
39
|
-
on(event: 'error', handler: ErrorHandler<this>): void;
|
|
40
|
-
/**
|
|
41
|
-
* Register a listener to the online event.
|
|
42
|
-
*
|
|
43
|
-
* @param event `'online'`.
|
|
44
|
-
* @param handler The online handler.
|
|
45
|
-
*/
|
|
46
|
-
on(event: 'online', handler: OnlineHandler<this>): void;
|
|
47
|
-
/**
|
|
48
|
-
* Register a listener to the exit event.
|
|
49
|
-
*
|
|
50
|
-
* @param event `'exit'`.
|
|
51
|
-
* @param handler The exit handler.
|
|
52
|
-
*/
|
|
53
|
-
on(event: 'exit', handler: ExitHandler<this>): void;
|
|
54
|
-
/**
|
|
55
|
-
* Register a listener to the exit event that will only performed once.
|
|
56
|
-
*
|
|
57
|
-
* @param event `'exit'`.
|
|
58
|
-
* @param handler The exit handler.
|
|
59
|
-
*/
|
|
60
|
-
once(event: 'exit', handler: ExitHandler<this>): void;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Options for a poolifier pool.
|
|
64
|
-
*/
|
|
65
|
-
export interface PoolOptions<Worker> {
|
|
66
|
-
/**
|
|
67
|
-
* A function that will listen for message event on each worker.
|
|
68
|
-
*/
|
|
69
|
-
messageHandler?: MessageHandler<Worker>;
|
|
70
|
-
/**
|
|
71
|
-
* A function that will listen for error event on each worker.
|
|
72
|
-
*/
|
|
73
|
-
errorHandler?: ErrorHandler<Worker>;
|
|
74
|
-
/**
|
|
75
|
-
* A function that will listen for online event on each worker.
|
|
76
|
-
*/
|
|
77
|
-
onlineHandler?: OnlineHandler<Worker>;
|
|
78
|
-
/**
|
|
79
|
-
* A function that will listen for exit event on each worker.
|
|
80
|
-
*/
|
|
81
|
-
exitHandler?: ExitHandler<Worker>;
|
|
82
|
-
/**
|
|
83
|
-
* The work choice strategy to use in this pool.
|
|
84
|
-
*/
|
|
85
|
-
workerChoiceStrategy?: WorkerChoiceStrategy;
|
|
86
|
-
/**
|
|
87
|
-
* Pool events emission.
|
|
88
|
-
*
|
|
89
|
-
* @default true
|
|
90
|
-
*/
|
|
91
|
-
enableEvents?: boolean;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Base class containing some shared logic for all poolifier pools.
|
|
95
|
-
*
|
|
96
|
-
* @template Worker Type of worker which manages this pool.
|
|
97
|
-
* @template Data Type of data sent to the worker. This can only be serializable data.
|
|
98
|
-
* @template Response Type of response of execution. This can only be serializable data.
|
|
99
|
-
*/
|
|
100
|
-
export declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Response = unknown> implements IPoolInternal<Worker, Data, Response> {
|
|
101
|
-
readonly numberOfWorkers: number;
|
|
102
|
-
readonly filePath: string;
|
|
103
|
-
readonly opts: PoolOptions<Worker>;
|
|
104
|
-
/** @inheritdoc */
|
|
105
|
-
readonly workers: Worker[];
|
|
106
|
-
/** @inheritdoc */
|
|
107
|
-
readonly tasks: Map<Worker, number>;
|
|
108
|
-
/** @inheritdoc */
|
|
109
|
-
readonly emitter?: PoolEmitter;
|
|
110
|
-
/** @inheritdoc */
|
|
111
|
-
readonly max?: number;
|
|
112
|
-
/**
|
|
113
|
-
* The promise map.
|
|
114
|
-
*
|
|
115
|
-
* - `key`: This is the message Id of each submitted task.
|
|
116
|
-
* - `value`: An object that contains the worker, the resolve function and the reject function.
|
|
117
|
-
*
|
|
118
|
-
* When we receive a message from the worker we get a map entry and resolve/reject the promise based on the message.
|
|
119
|
-
*/
|
|
120
|
-
protected promiseMap: Map<number, PromiseWorkerResponseWrapper<Worker, Response>>;
|
|
121
|
-
/**
|
|
122
|
-
* Id of the next message.
|
|
123
|
-
*/
|
|
124
|
-
protected nextMessageId: number;
|
|
125
|
-
/**
|
|
126
|
-
* Worker choice strategy instance implementing the worker choice algorithm.
|
|
127
|
-
*
|
|
128
|
-
* Default to a strategy implementing a round robin algorithm.
|
|
129
|
-
*/
|
|
130
|
-
protected workerChoiceStrategyContext: WorkerChoiceStrategyContext<Worker, Data, Response>;
|
|
131
|
-
/**
|
|
132
|
-
* Constructs a new poolifier pool.
|
|
133
|
-
*
|
|
134
|
-
* @param numberOfWorkers Number of workers that this pool should manage.
|
|
135
|
-
* @param filePath Path to the worker-file.
|
|
136
|
-
* @param opts Options for the pool.
|
|
137
|
-
*/
|
|
138
|
-
constructor(numberOfWorkers: number, filePath: string, opts: PoolOptions<Worker>);
|
|
139
|
-
private checkFilePath;
|
|
140
|
-
private checkNumberOfWorkers;
|
|
141
|
-
private checkPoolOptions;
|
|
142
|
-
/** @inheritdoc */
|
|
143
|
-
abstract get type(): PoolType;
|
|
144
|
-
/** @inheritdoc */
|
|
145
|
-
get numberOfRunningTasks(): number;
|
|
146
|
-
/** @inheritdoc */
|
|
147
|
-
setWorkerChoiceStrategy(workerChoiceStrategy: WorkerChoiceStrategy): void;
|
|
148
|
-
/** @inheritdoc */
|
|
149
|
-
abstract get busy(): boolean;
|
|
150
|
-
protected internalGetBusyStatus(): boolean;
|
|
151
|
-
/** @inheritdoc */
|
|
152
|
-
findFreeTasksMapEntry(): [Worker, number] | false;
|
|
153
|
-
/** @inheritdoc */
|
|
154
|
-
execute(data: Data): Promise<Response>;
|
|
155
|
-
/** @inheritdoc */
|
|
156
|
-
destroy(): Promise<void>;
|
|
157
|
-
/**
|
|
158
|
-
* Shut down given worker.
|
|
159
|
-
*
|
|
160
|
-
* @param worker A worker within `workers`.
|
|
161
|
-
*/
|
|
162
|
-
protected abstract destroyWorker(worker: Worker): void | Promise<void>;
|
|
163
|
-
/**
|
|
164
|
-
* Setup hook that can be overridden by a Poolifier pool implementation
|
|
165
|
-
* to run code before workers are created in the abstract constructor.
|
|
166
|
-
*/
|
|
167
|
-
protected setupHook(): void;
|
|
168
|
-
/**
|
|
169
|
-
* Should return whether the worker is the main worker or not.
|
|
170
|
-
*/
|
|
171
|
-
protected abstract isMain(): boolean;
|
|
172
|
-
/**
|
|
173
|
-
* Increase the number of tasks that the given worker has applied.
|
|
174
|
-
*
|
|
175
|
-
* @param worker Worker whose tasks are increased.
|
|
176
|
-
*/
|
|
177
|
-
protected increaseWorkersTask(worker: Worker): void;
|
|
178
|
-
/**
|
|
179
|
-
* Decrease the number of tasks that the given worker has applied.
|
|
180
|
-
*
|
|
181
|
-
* @param worker Worker whose tasks are decreased.
|
|
182
|
-
*/
|
|
183
|
-
protected decreaseWorkersTasks(worker: Worker): void;
|
|
184
|
-
/**
|
|
185
|
-
* Step the number of tasks that the given worker has applied.
|
|
186
|
-
*
|
|
187
|
-
* @param worker Worker whose tasks are set.
|
|
188
|
-
* @param step Worker number of tasks step.
|
|
189
|
-
*/
|
|
190
|
-
private stepWorkerNumberOfTasks;
|
|
191
|
-
/**
|
|
192
|
-
* Removes the given worker from the pool.
|
|
193
|
-
*
|
|
194
|
-
* @param worker Worker that will be removed.
|
|
195
|
-
*/
|
|
196
|
-
protected removeWorker(worker: Worker): void;
|
|
197
|
-
/**
|
|
198
|
-
* Choose a worker for the next task.
|
|
199
|
-
*
|
|
200
|
-
* The default implementation uses a round robin algorithm to distribute the load.
|
|
201
|
-
*
|
|
202
|
-
* @returns Worker.
|
|
203
|
-
*/
|
|
204
|
-
protected chooseWorker(): Worker;
|
|
205
|
-
/**
|
|
206
|
-
* Send a message to the given worker.
|
|
207
|
-
*
|
|
208
|
-
* @param worker The worker which should receive the message.
|
|
209
|
-
* @param message The message.
|
|
210
|
-
*/
|
|
211
|
-
protected abstract sendToWorker(worker: Worker, message: MessageValue<Data>): void;
|
|
212
|
-
/**
|
|
213
|
-
* Register a listener callback on a given worker.
|
|
214
|
-
*
|
|
215
|
-
* @param worker A worker.
|
|
216
|
-
* @param listener A message listener callback.
|
|
217
|
-
*/
|
|
218
|
-
protected abstract registerWorkerMessageListener<Message extends Data | Response>(worker: Worker, listener: (message: MessageValue<Message>) => void): void;
|
|
219
|
-
protected internalExecute(worker: Worker, messageId: number): Promise<Response>;
|
|
220
|
-
/**
|
|
221
|
-
* Returns a newly created worker.
|
|
222
|
-
*/
|
|
223
|
-
protected abstract createWorker(): Worker;
|
|
224
|
-
/**
|
|
225
|
-
* Function that can be hooked up when a worker has been newly created and moved to the workers registry.
|
|
226
|
-
*
|
|
227
|
-
* Can be used to update the `maxListeners` or binding the `main-worker`<->`worker` connection if not bind by default.
|
|
228
|
-
*
|
|
229
|
-
* @param worker The newly created worker.
|
|
230
|
-
*/
|
|
231
|
-
protected abstract afterWorkerSetup(worker: Worker): void;
|
|
232
|
-
/**
|
|
233
|
-
* Creates a new worker for this pool and sets it up completely.
|
|
234
|
-
*
|
|
235
|
-
* @returns New, completely set up worker.
|
|
236
|
-
*/
|
|
237
|
-
protected createAndSetupWorker(): Worker;
|
|
238
|
-
/**
|
|
239
|
-
* This function is the listener registered for each worker.
|
|
240
|
-
*
|
|
241
|
-
* @returns The listener function to execute when a message is sent from a worker.
|
|
242
|
-
*/
|
|
243
|
-
protected workerListener(): (message: MessageValue<Response>) => void;
|
|
244
|
-
private checkAndEmitBusy;
|
|
245
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { PoolType } from '../pool-internal';
|
|
2
|
-
import type { ClusterPoolOptions } from './fixed';
|
|
3
|
-
import { FixedClusterPool } from './fixed';
|
|
4
|
-
/**
|
|
5
|
-
* A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
|
|
6
|
-
*
|
|
7
|
-
* This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
|
|
8
|
-
* When the maximum number of workers is reached, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
|
|
9
|
-
*
|
|
10
|
-
* @template DataType of data sent to the worker. This can only be serializable data.
|
|
11
|
-
* @template ResponseType of response of execution. This can only be serializable data.
|
|
12
|
-
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
|
|
13
|
-
* @since 2.0.0
|
|
14
|
-
*/
|
|
15
|
-
export declare class DynamicClusterPool<Data = unknown, Response = unknown> extends FixedClusterPool<Data, Response> {
|
|
16
|
-
readonly max: number;
|
|
17
|
-
/**
|
|
18
|
-
* Constructs a new poolifier dynamic cluster pool.
|
|
19
|
-
*
|
|
20
|
-
* @param min Minimum number of workers which are always active.
|
|
21
|
-
* @param max Maximum number of workers that can be created by this pool.
|
|
22
|
-
* @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
|
|
23
|
-
* @param [opts={}] Options for this dynamic cluster pool.
|
|
24
|
-
*/
|
|
25
|
-
constructor(min: number, max: number, filePath: string, opts?: ClusterPoolOptions);
|
|
26
|
-
/** @inheritdoc */
|
|
27
|
-
get type(): PoolType;
|
|
28
|
-
/** @inheritdoc */
|
|
29
|
-
get busy(): boolean;
|
|
30
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { Worker } from 'cluster';
|
|
3
|
-
import type { MessageValue } from '../../utility-types';
|
|
4
|
-
import type { PoolOptions } from '../abstract-pool';
|
|
5
|
-
import { AbstractPool } from '../abstract-pool';
|
|
6
|
-
import { PoolType } from '../pool-internal';
|
|
7
|
-
/**
|
|
8
|
-
* Options for a poolifier cluster pool.
|
|
9
|
-
*/
|
|
10
|
-
export interface ClusterPoolOptions extends PoolOptions<Worker> {
|
|
11
|
-
/**
|
|
12
|
-
* Key/value pairs to add to worker process environment.
|
|
13
|
-
*
|
|
14
|
-
* @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
|
|
15
|
-
*/
|
|
16
|
-
env?: any;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* A cluster pool with a fixed number of workers.
|
|
20
|
-
*
|
|
21
|
-
* It is possible to perform tasks in sync or asynchronous mode as you prefer.
|
|
22
|
-
*
|
|
23
|
-
* This pool selects the workers in a round robin fashion.
|
|
24
|
-
*
|
|
25
|
-
* @template DataType of data sent to the worker. This can only be serializable data.
|
|
26
|
-
* @template ResponseType of response of execution. This can only be serializable data.
|
|
27
|
-
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
|
|
28
|
-
* @since 2.0.0
|
|
29
|
-
*/
|
|
30
|
-
export declare class FixedClusterPool<Data = unknown, Response = unknown> extends AbstractPool<Worker, Data, Response> {
|
|
31
|
-
readonly opts: ClusterPoolOptions;
|
|
32
|
-
/**
|
|
33
|
-
* Constructs a new poolifier fixed cluster pool.
|
|
34
|
-
*
|
|
35
|
-
* @param numberOfWorkers Number of workers for this pool.
|
|
36
|
-
* @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
|
|
37
|
-
* @param [opts={}] Options for this fixed cluster pool.
|
|
38
|
-
*/
|
|
39
|
-
constructor(numberOfWorkers: number, filePath: string, opts?: ClusterPoolOptions);
|
|
40
|
-
/** @inheritdoc */
|
|
41
|
-
protected setupHook(): void;
|
|
42
|
-
/** @inheritdoc */
|
|
43
|
-
protected isMain(): boolean;
|
|
44
|
-
/** @inheritdoc */
|
|
45
|
-
destroyWorker(worker: Worker): void;
|
|
46
|
-
/** @inheritdoc */
|
|
47
|
-
protected sendToWorker(worker: Worker, message: MessageValue<Data>): void;
|
|
48
|
-
/** @inheritdoc */
|
|
49
|
-
registerWorkerMessageListener<Message extends Data | Response>(worker: Worker, listener: (message: MessageValue<Message>) => void): void;
|
|
50
|
-
/** @inheritdoc */
|
|
51
|
-
protected createWorker(): Worker;
|
|
52
|
-
/** @inheritdoc */
|
|
53
|
-
protected afterWorkerSetup(worker: Worker): void;
|
|
54
|
-
/** @inheritdoc */
|
|
55
|
-
get type(): PoolType;
|
|
56
|
-
/** @inheritdoc */
|
|
57
|
-
get busy(): boolean;
|
|
58
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import EventEmitter from 'events';
|
|
3
|
-
import type { IWorker } from './abstract-pool';
|
|
4
|
-
import type { IPool } from './pool';
|
|
5
|
-
/**
|
|
6
|
-
* Pool types.
|
|
7
|
-
*/
|
|
8
|
-
export declare enum PoolType {
|
|
9
|
-
FIXED = "fixed",
|
|
10
|
-
DYNAMIC = "dynamic"
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Internal poolifier pool emitter.
|
|
14
|
-
*/
|
|
15
|
-
export declare class PoolEmitter extends EventEmitter {
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Internal contract definition for a poolifier pool.
|
|
19
|
-
*
|
|
20
|
-
* @template Worker Type of worker which manages this pool.
|
|
21
|
-
* @template Data Type of data sent to the worker.
|
|
22
|
-
* @template Response Type of response of execution.
|
|
23
|
-
*/
|
|
24
|
-
export interface IPoolInternal<Worker extends IWorker, Data = unknown, Response = unknown> extends IPool<Data, Response> {
|
|
25
|
-
/**
|
|
26
|
-
* List of currently available workers.
|
|
27
|
-
*/
|
|
28
|
-
readonly workers: Worker[];
|
|
29
|
-
/**
|
|
30
|
-
* The tasks map.
|
|
31
|
-
*
|
|
32
|
-
* - `key`: The `Worker`
|
|
33
|
-
* - `value`: Number of tasks currently in progress on the worker.
|
|
34
|
-
*/
|
|
35
|
-
readonly tasks: Map<Worker, number>;
|
|
36
|
-
/**
|
|
37
|
-
* Emitter on which events can be listened to.
|
|
38
|
-
*
|
|
39
|
-
* Events that can currently be listened to:
|
|
40
|
-
*
|
|
41
|
-
* - `'busy'`
|
|
42
|
-
*/
|
|
43
|
-
readonly emitter?: PoolEmitter;
|
|
44
|
-
/**
|
|
45
|
-
* Pool type.
|
|
46
|
-
*
|
|
47
|
-
* If it is `'dynamic'`, it provides the `max` property.
|
|
48
|
-
*/
|
|
49
|
-
readonly type: PoolType;
|
|
50
|
-
/**
|
|
51
|
-
* Maximum number of workers that can be created by this pool.
|
|
52
|
-
*/
|
|
53
|
-
readonly max?: number;
|
|
54
|
-
/**
|
|
55
|
-
* Whether the pool is busy or not.
|
|
56
|
-
*
|
|
57
|
-
* The pool busyness boolean status.
|
|
58
|
-
*/
|
|
59
|
-
readonly busy: boolean;
|
|
60
|
-
/**
|
|
61
|
-
* Number of tasks currently concurrently running.
|
|
62
|
-
*/
|
|
63
|
-
readonly numberOfRunningTasks: number;
|
|
64
|
-
/**
|
|
65
|
-
* Find a tasks map entry with a free worker based on the number of tasks the worker has applied.
|
|
66
|
-
*
|
|
67
|
-
* If an entry is found with a worker that has `0` tasks, it is detected as free.
|
|
68
|
-
*
|
|
69
|
-
* If no tasks map entry with a free worker was found, `false` will be returned.
|
|
70
|
-
*
|
|
71
|
-
* @returns A tasks map entry with a free worker if there was one, otherwise `false`.
|
|
72
|
-
*/
|
|
73
|
-
findFreeTasksMapEntry(): [Worker, number] | false;
|
|
74
|
-
}
|