poolifier
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -8,2 +8,10 @@ # Changelog | ||
## [1.1.0] - 2020-21-05 | ||
### Added | ||
- ThreadWorker support async functions as option | ||
- Various external library patches | ||
## [1.0.0] - 2020-24-01 | ||
@@ -15,2 +23,2 @@ | ||
- DynamicThreadPool implementation | ||
- WorkerThread implementation to improve developer experience | ||
- WorkerThread implementation to improve developer experience |
@@ -37,5 +37,5 @@ 'use strict' | ||
destroy () { | ||
async destroy () { | ||
for (const worker of this.workers) { | ||
worker.terminate() | ||
await worker.terminate() | ||
} | ||
@@ -42,0 +42,0 @@ } |
@@ -19,2 +19,3 @@ 'use strict' | ||
this.maxInactiveTime = this.opts.maxInactiveTime || (1000 * 60) | ||
this.async = !!this.opts.async | ||
this.lastTask = Date.now() | ||
@@ -31,3 +32,7 @@ if (!fn) throw new Error('Fn parameter is mandatory') | ||
// console.log('This is the main thread ' + isMainThread) | ||
this.runInAsyncScope(this._run.bind(this), this, fn, value) | ||
if (this.async) { | ||
this.runInAsyncScope(this._runAsync.bind(this), this, fn, value) | ||
} else { | ||
this.runInAsyncScope(this._run.bind(this), this, fn, value) | ||
} | ||
} else if (value.parent) { | ||
@@ -61,4 +66,14 @@ // save the port to communicate with the main thread | ||
} | ||
_runAsync (fn, value) { | ||
fn(value.data).then(res => { | ||
this.parent.postMessage({ data: res, _id: value._id }) | ||
this.lastTask = Date.now() | ||
}).catch(e => { | ||
this.parent.postMessage({ error: e, _id: value._id }) | ||
this.lastTask = Date.now() | ||
}) | ||
} | ||
} | ||
module.exports.ThreadWorker = ThreadWorker |
{ | ||
"name": "poolifier", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Library on top of node js worker threads that implement various worker pools type", | ||
@@ -43,3 +43,3 @@ "main": "index.js", | ||
"coveralls": "^3.0.9", | ||
"expect": "^25.1.0", | ||
"expect": "^26.0.0", | ||
"mocha": "^7.0.0", | ||
@@ -46,0 +46,0 @@ "mocha-lcov-reporter": "^1.3.0", |
@@ -11,2 +11,9 @@ # Node Thread Pool :arrow_double_up: :on: | ||
<h2>Why Poolifier? </h2> | ||
Poolifier is used to perform heavy CPU bound tasks on nodejs servers, it implements thread pools ( yes, more thread pool implementations, so you can choose which one fit better for you ) using <a href="https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads">worker-threads </a>.<br> | ||
With poolifier you can improve your <strong>performance</strong> and resolve problems related to the event loop.<br> | ||
Moreover you can execute your CPU tasks using an API designed to improve the <strong>developer experience</strong>. | ||
<h2>Contents </h2> | ||
@@ -53,11 +60,6 @@ <h3 align="center"> | ||
class MyWorker extends ThreadWorker { | ||
constructor () { | ||
super(yourFunction, { maxInactiveTime: 1000 * 60}) | ||
} | ||
} | ||
module.exports = new MyWorker() | ||
module.exports = new ThreadWorker(yourFunction, { maxInactiveTime: 60000, async: false }) | ||
``` | ||
Instantiate your pool based on your needed : | ||
Instantiate your pool based on your needed : | ||
@@ -92,3 +94,3 @@ ```js | ||
You can use node versions 12.x , 13.x <br> | ||
You can use node versions 12.x , 13.x, 14.x <br> | ||
@@ -125,2 +127,3 @@ <h2 id="api">API</h2> | ||
- `maxInactiveTime` - Max time to wait tasks to work on ( in ms) , after this period the new worker threads will die. | ||
- `async` - true/false , true if your function contains async pieces else false | ||
@@ -138,3 +141,4 @@ <h2 id="cyp">Choose your pool</h2> | ||
See guidelines [CONTRIBUTING](CONTRIBUTING.md) | ||
See guidelines [CONTRIBUTING](CONTRIBUTING.md) <br> | ||
Choose your task here <a href="https://github.com/pioardi/poolifier/projects/1"> 2.0.0</a>, propose an idea, a fix, an improvement. <br> | ||
@@ -141,0 +145,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
18968
220
144
0