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)
8 <a href="#installation">Installation</a>
10 <a href="#usage">Usage</a>
12 <a href="#api">API</a>
14 <a href="#contribute">Contribute</a>
16 <a href="#nv">Compatibility</a>
18 <a href="#license">License</a>
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>
29 npm install node-thread-pool --save
31 <h2 id="usage">Usage</h2>
33 You can implement a worker in a simple way , extending the class ThreadWorker :
37 const { ThreadWorker } = require('node-pool')
39 class MyWorker extends ThreadWorker {
42 // this will be executed in the worker thread,
43 // the data will be received by using the execute method
45 }, { maxInactiveTime: 1000 * 60})
48 module.exports = new MyWorker()
51 Instantiate your pool based on your needed :
55 const { FixedThreadPool, DynamicThreadPool } = require('node-pool')
57 // a fixed thread pool
58 const pool = new FixedThreadPool(15,
61 // or a dynamic thread pool
62 const pool = new DynamicThreadPool(10, 100,
64 pool.emitter.on('FullPool', () => console.log('Pool is full'))
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 => {
74 <strong> See examples folder for more details.</strong>
76 <h2 id="nv">Node versions</h2>
78 You can use node version 10.x with --experimental-worker flag, or you can use an higher version (i.e 12.x) <br>
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)
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>
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>
102 Destroy method is available on both pool implementations.<br>
103 This method will call the terminate method on each worker.
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.
111 <h2 id="contribute">Contribute</h2>
113 See guidelines [CONTRIBUTING](./.github/CONTRIBUTING.md)
116 <h2 id="license">License</h2>