| 1 | # Node Thread Pool :arrow_double_up: :on: |
| 2 | [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) |
| 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 | <h2>Contents </h2> |
| 7 | <h3 align="center"> |
| 8 | <a href="#installation">Installation</a> |
| 9 | <span> · </span> |
| 10 | <a href="#usage">Usage</a> |
| 11 | <span> · </span> |
| 12 | <a href="#api">API</a> |
| 13 | <span> · </span> |
| 14 | <a href="#contribute">Contribute</a> |
| 15 | <span> · </span> |
| 16 | <a href="#nv">Compatibility</a> |
| 17 | <span> · </span> |
| 18 | <a href="#license">License</a> |
| 19 | </h3> |
| 20 | |
| 21 | <h2> Overview </h2> |
| 22 | 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> |
| 23 | The first implementation is a static thread pool , with a defined number of threads that are started at creation time .<br> |
| 24 | 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 ), the new created threads will be stopped after a threshold. <br> |
| 25 | You have to implement your worker extending the ThreadWorker class<br> |
| 26 | <h2 id="installation">Installation</h2> |
| 27 | |
| 28 | ``` |
| 29 | npm install node-thread-pool --save |
| 30 | ``` |
| 31 | <h2 id="usage">Usage</h2> |
| 32 | |
| 33 | You can implement a worker in a simple way , extending the class ThreadWorker : |
| 34 | |
| 35 | ```js |
| 36 | 'use strict' |
| 37 | const { ThreadWorker } = require('node-pool') |
| 38 | |
| 39 | class MyWorker extends ThreadWorker { |
| 40 | constructor () { |
| 41 | super((data) => { |
| 42 | // this will be executed in the worker thread, |
| 43 | // the data will be received by using the execute method |
| 44 | return { ok: 1 } |
| 45 | }, { maxInactiveTime: 1000 * 60}) |
| 46 | } |
| 47 | } |
| 48 | module.exports = new MyWorker() |
| 49 | ``` |
| 50 | |
| 51 | Instantiate your pool based on your needed : |
| 52 | |
| 53 | ```js |
| 54 | 'use strict' |
| 55 | const { FixedThreadPool, DynamicThreadPool } = require('node-pool') |
| 56 | |
| 57 | // a fixed thread pool |
| 58 | const pool = new FixedThreadPool(15, |
| 59 | './yourWorker.js') |
| 60 | |
| 61 | // or a dynamic thread pool |
| 62 | const pool = new DynamicThreadPool(10, 100, |
| 63 | './yourWorker.js') |
| 64 | pool.emitter.on('FullPool', () => console.log('Pool is full')) |
| 65 | |
| 66 | // the execute method signature is the same for both implementations, |
| 67 | // so you can easy switch from one to another |
| 68 | pool.execute({}).then(res => { |
| 69 | console.log(res) |
| 70 | }).catch .... |
| 71 | |
| 72 | ``` |
| 73 | |
| 74 | <strong> See examples folder for more details.</strong> |
| 75 | |
| 76 | <h2 id="nv">Node versions</h2> |
| 77 | |
| 78 | You can use node version 10.x with --experimental-worker flag, or you can use an higher version (i.e 12.x) <br> |
| 79 | |
| 80 | <h2 id="api">API</h2> |
| 81 | |
| 82 | ### `pool = new FixedThreadPool(numThreads, filePath, opts)` |
| 83 | `numThreads` (mandatory) Num of threads for this worker pool <br> |
| 84 | `filePath` (mandatory) Path to a file with a worker implementation <br> |
| 85 | `opts` (optional) An object with these properties : |
| 86 | - `errorHandler` - A function that will listen for error event on each worker thread |
| 87 | - `onlineHandler` - A function that will listen for online event on each worker thread |
| 88 | - `exitHandler` - A function that will listen for exit event on each worker thread |
| 89 | - `maxTasks` - This is just to avoid not useful warnings message, is used to set <a href="https://nodejs.org/dist/latest-v12.x/docs/api/events.html#events_emitter_setmaxlisteners_n">maxListeners</a> on event emitters ( workers are event emitters) |
| 90 | |
| 91 | ### `pool = new DynamicThreadPool(min, max, filePath, opts)` |
| 92 | `min` (mandatory) Same as FixedThreadPool numThreads , this number of threads will be always active <br> |
| 93 | `max` (mandatory) Max number of workers that this pool can contain, the new created threads will die after a threshold ( default is 1 minute , you can override it in your worker implementation). <br> |
| 94 | `filePath` (mandatory) Same as FixedThreadPool <br> |
| 95 | `opts` (optional) Same as FixedThreadPool <br> |
| 96 | |
| 97 | ### `pool.execute(data)` |
| 98 | Execute method is available on both pool implementations ( return type : Promise): <br> |
| 99 | `data` (mandatory) An object that you want to pass to your worker implementation <br> |
| 100 | |
| 101 | ### `pool.destroy()` |
| 102 | Destroy method is available on both pool implementations.<br> |
| 103 | This method will call the terminate method on each worker. |
| 104 | |
| 105 | |
| 106 | ### `class YourWorker extends ThreadWorker` |
| 107 | `fn` (mandatory) The function that you want to execute on the worker thread <br> |
| 108 | `opts` (optional) An object with these properties : |
| 109 | - `maxInactiveTime` - Max time to wait tasks to work on ( in ms) , after this period the new worker threads will die. |
| 110 | |
| 111 | <h2 id="contribute">Contribute</h2> |
| 112 | |
| 113 | See guidelines [CONTRIBUTING](./.github/CONTRIBUTING.md) |
| 114 | |
| 115 | |
| 116 | <h2 id="license">License</h2> |
| 117 | |
| 118 | [MIT](./LICENSE) |
| 119 | |