| 1 | # Node Pool :arrow_double_up: :on: |
| 2 | [![JavaScript Style Guide](https://cdn.rawgit.com/standard/standard/master/badge.svg)](https://github.com/standard/standard) |
| 3 | [![Dependabot](https://badgen.net/dependabot/dependabot/dependabot-core/?icon=dependabot)](https://badgen.net/dependabot/dependabot/dependabot-core/?icon=dependabot) |
| 4 | [![Actions Status](https://github.com/pioardi/node-pool/workflows/NodeCI/badge.svg)](https://github.com/pioardi/node-pool/actions) |
| 5 | |
| 6 | Node pool contains two <a href="https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads">worker-threads </a> pool implementations , you don' t have to deal with worker-threads complexity. <br> |
| 7 | The first implementation is a static thread pool , with a defined number of threads that are started at creation time .<br> |
| 8 | The second implementation is a dynamic thread pool with a number of threads started at creation time and other threads created when the load will increase ( with an upper limit ). <br> |
| 9 | |
| 10 | ## Installation |
| 11 | ``` |
| 12 | npm install node-pool --save |
| 13 | ``` |
| 14 | # Usage |
| 15 | |
| 16 | You can implement a worker in a simple way , extending the class ThreadWorker : |
| 17 | |
| 18 | ```js |
| 19 | 'use strict' |
| 20 | const { ThreadWorker } = require('node-pool') |
| 21 | |
| 22 | class MyWorker extends ThreadWorker { |
| 23 | constructor () { |
| 24 | super((data) => { |
| 25 | // this will be executed in the worker thread, the data will be received by using the execute method |
| 26 | return { ok: 1 } |
| 27 | }) |
| 28 | } |
| 29 | } |
| 30 | module.exports = new MyWorker() |
| 31 | ``` |
| 32 | |
| 33 | Instantiate your pool based on your needed : |
| 34 | |
| 35 | ```js |
| 36 | 'use strict' |
| 37 | const { FixedThreadPool } = require('node-pool') |
| 38 | |
| 39 | // a fixed thread pool |
| 40 | const pool = new FixedThreadPool(15, |
| 41 | './yourWorker.js') |
| 42 | |
| 43 | // or a dynamic thread pool |
| 44 | const pool = new DynamicThreadPool(10, 100, |
| 45 | './yourWorker.js') |
| 46 | pool.emitter.on('FullPool', () => console.log('Pool is full')) |
| 47 | |
| 48 | // the execute method signature is the same for both implementations, |
| 49 | // so you can easy switch from one to another |
| 50 | pool.execute({}).then(res => { |
| 51 | console.log(res) |
| 52 | }).catch .... |
| 53 | |
| 54 | ``` |
| 55 | |
| 56 | <strong> See examples folder for more details.</strong> |
| 57 | |
| 58 | ## Node versions |
| 59 | You can use node version 10.x with --experimental-worker flag, or you can use 12.x version <br> |
| 60 | |
| 61 | ## API |
| 62 | |
| 63 | TODO |
| 64 | |
| 65 | ## License |
| 66 | |
| 67 | [MIT](https://github.com/pioardi/node-pool/blob/master/LICENSE) |
| 68 | |