From 50aa790189f6423c52ea72198380dbe93c025e40 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Tue, 9 Feb 2021 18:34:14 +0100 Subject: [PATCH] Clean up project (#91) --- .eslintrc.js | 2 +- .prettierignore | 1 + CONTRIBUTING.md | 22 +++---- LICENSE | 2 +- README.MD => README.md | 133 +++++++++++++++++++++++------------------ package-lock.json | 6 +- package.json | 10 ++-- 7 files changed, 98 insertions(+), 78 deletions(-) rename README.MD => README.md (51%) diff --git a/.eslintrc.js b/.eslintrc.js index 8f902bd2..6c710303 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -38,7 +38,7 @@ module.exports = { 'warn', { ignoreMemberSort: true, - memberSyntaxSortOrder: ['none', 'all', 'single', 'multiple'] + ignoreDeclarationSort: true } ] }, diff --git a/.prettierignore b/.prettierignore index 76106cb9..e40d57b1 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,3 @@ .nyc_output/ +coverage/ lib/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c57e879c..d41ad6e0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,28 +1,28 @@ -

How to contribute

+## How to contribute -[![JavaScript Style Guide](https://cdn.rawgit.com/standard/standard/master/badge.svg)](https://github.com/standard/standard)
-This repo use standard js style , please use it if you want to contribute
-Take tasks from todo list, develop a new feature or fix a bug and do a pull request.
-Another thing that you can do to contribute is to build something on top of ring-election and link ring-election to your project
+[![JavaScript Style Guide](https://cdn.rawgit.com/standard/standard/master/badge.svg)](https://github.com/standard/standard) +This repo use standard js style, please use it if you want to contribute +Take tasks from todo list, develop a new feature or fix a bug and do a pull request. +Another thing that you can do to contribute is to build something on top of ring-election and link ring-election to your project -Please ask your PR to be merged on master branch .
+Please ask your PR to be merged on **master** branch. -How to run tests
+**How to run tests** -Unit tests
+**Unit tests** ```bash npm run test ``` - How to check if your new code is standard style
+**How to check if your new code is standard style** ```bash npm run lint ``` -How to lint and format (with prettier) your code
+**How to lint and format (with prettier) your code** ```bash - npm run lint:fix + npm run format && npm run lint:fix ``` diff --git a/LICENSE b/LICENSE index 7ab0bbd0..306f35e6 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019-2020 Alessandro Pio Ardizio +Copyright (c) 2019-2021 Alessandro Pio Ardizio Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.MD b/README.md similarity index 51% rename from README.MD rename to README.md index 465ab8d9..17c46101 100644 --- a/README.MD +++ b/README.md @@ -1,53 +1,58 @@ # Node Thread Pool :arrow_double_up: :on: + [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) [![Dependabot](https://badgen.net/dependabot/dependabot/dependabot-core/?icon=dependabot)](https://badgen.net/dependabot/dependabot/dependabot-core/?icon=dependabot) [![npm w](https://img.shields.io/npm/dw/poolifier)](https://www.npmjs.com/package/poolifier) [![Actions Status](https://github.com/pioardi/node-pool/workflows/NodeCI/badge.svg)](https://github.com/pioardi/node-pool/actions) -[![Coverage Status](https://coveralls.io/repos/github/pioardi/poolifier/badge.svg?branch=master)](https://coveralls.io/github/pioardi/poolifier?branch=master)[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) -[![NODEP](https://img.shields.io/static/v1?label=dependencies&message=no%20dependencies&color=brightgreen -)](https://img.shields.io/static/v1?label=dependencies&message=no%20dependencies&color=brightgreen -) +[![Coverage Status](https://coveralls.io/repos/github/pioardi/poolifier/badge.svg?branch=master)](https://coveralls.io/github/pioardi/poolifier?branch=master) +[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) +[![NODEP](https://img.shields.io/static/v1?label=dependencies&message=no%20dependencies&color=brightgreen)](https://img.shields.io/static/v1?label=dependencies&message=no%20dependencies&color=brightgreen) [![Gitter](https://badges.gitter.im/poolifier/community.svg)](https://gitter.im/poolifier/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) -

Why Poolifier?

-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 worker-threads .
-With poolifier you can improve your performance and resolve problems related to the event loop.
-Moreover you can execute your CPU tasks using an API designed to improve the developer experience. +## Why Poolifier? +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 [worker-threads](https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads). +With poolifier you can improve your **performance** and resolve problems related to the event loop. +Moreover you can execute your CPU tasks using an API designed to improve the **developer experience**. +## Contents -

Contents

+ Overview + · Installation · Usage · + Node versions + · API · - Choose a pool + Choose your pool · Contribute · - Team - · - Compatibility + Team · License

-

Overview

-Node pool contains two worker-threads pool implementations , you don' t have to deal with worker-threads complexity.
-The first implementation is a static thread pool , with a defined number of threads that are started at creation time and will be reused.
-The second implementation is a dynamic thread pool with a number of threads started at creation time ( these threads will be always active and reused) and other threads created when the load will increase ( with an upper limit, these threads will be reused when active ), the new created threads will be stopped after a configurable period of inactivity.
-You have to implement your worker extending the ThreadWorker class
-

Installation

+## Overview -``` +Node pool contains two [worker-threads](https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads) pool implementations, you don't have to deal with worker-threads complexity. +The first implementation is a static thread pool, with a defined number of threads that are started at creation time and will be reused. +The second implementation is a dynamic thread pool with a number of threads started at creation time (these threads will be always active and reused) and other threads created when the load will increase (with an upper limit, these threads will be reused when active), the new created threads will be stopped after a configurable period of inactivity. +You have to implement your worker extending the ThreadWorker class + +## Installation + +```shell npm install poolifier --save ``` -

Usage

-You can implement a worker in a simple way , extending the class ThreadWorker : +## Usage + +You can implement a worker in a simple way, extending the class ThreadWorker: ```js 'use strict' @@ -59,7 +64,10 @@ function yourFunction (data) { return { ok: 1 } } -module.exports = new ThreadWorker(yourFunction, { maxInactiveTime: 60000, async: false }) +module.exports = new ThreadWorker(yourFunction, { + maxInactiveTime: 60000, + async: false +}) ``` Instantiate your pool based on your needed : @@ -88,72 +96,81 @@ pool.execute({}).then(res => { ``` - See examples folder for more details( in particular if you want to use a pool for [multiple functions](./examples/multiFunctionExample.js) ). -Now type script is also supported, find how to use it into the example folder +**See examples folder for more details (in particular if you want to use a pool for [multiple functions](./examples/multiFunctionExample.js)).** +**Now type script is also supported, find how to use it into the example folder** -

Node versions

+## Node versions -You can use node versions 12.x , 13.x, 14.x
+You can use node versions 12.x, 13.x, 14.x -

API

+## API ### `pool = new FixedThreadPool(numThreads, filePath, opts)` -`numThreads` (mandatory) Num of threads for this worker pool
-`filePath` (mandatory) Path to a file with a worker implementation
+ +`numThreads` (mandatory) Num of threads for this worker pool +`filePath` (mandatory) Path to a file with a worker implementation `opts` (optional) An object with these properties : + - `errorHandler` - A function that will listen for error event on each worker thread - `onlineHandler` - A function that will listen for online event on each worker thread - `exitHandler` - A function that will listen for exit event on each worker thread -- `maxTasks` - This is just to avoid not useful warnings message, is used to set maxListeners on event emitters ( workers are event emitters) +- `maxTasks` - This is just to avoid not useful warnings message, is used to set [maxListeners](https://nodejs.org/dist/latest-v12.x/docs/api/events.html#events_emitter_setmaxlisteners_n) on event emitters (workers are event emitters) ### `pool = new DynamicThreadPool(min, max, filePath, opts)` -`min` (mandatory) Same as FixedThreadPool numThreads , this number of threads will be always active
-`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).
-`filePath` (mandatory) Same as FixedThreadPool
-`opts` (optional) Same as FixedThreadPool
+ +`min` (mandatory) Same as FixedThreadPool numThreads, this number of threads will be always active +`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). +`filePath` (mandatory) Same as FixedThreadPool +`opts` (optional) Same as FixedThreadPool ### `pool.execute(data)` -Execute method is available on both pool implementations ( return type : Promise):
-`data` (mandatory) An object that you want to pass to your worker implementation
+ +Execute method is available on both pool implementations (return type : Promise): +`data` (mandatory) An object that you want to pass to your worker implementation ### `pool.destroy()` -Destroy method is available on both pool implementations.
-This method will call the terminate method on each worker. +Destroy method is available on both pool implementations. +This method will call the terminate method on each worker. ### `class YourWorker extends ThreadWorker` -`fn` (mandatory) The function that you want to execute on the worker thread
-`opts` (optional) An object with these properties : + +`fn` (mandatory) The function that you want to execute on the worker thread +`opts` (optional) An object with these properties: + - `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 +- `async` - true/false, true if your function contains async pieces else false + +## Choose your pool -

Choose your pool

-Performance is one of the main target of these thread pool implementations, we want to have a strong focus on this.
+Performance is one of the main target of these thread pool implementations, we want to have a strong focus on this. We already have a bench folder where you can find some comparisons. -To choose your pool consider that with a FixedThreadPool or a DynamicThreadPool ( in this case is important the min parameter passed to the constructor) your application memory footprint will increase .
-Increasing the memory footprint, your application will be ready to accept more CPU bound tasks, but during idle time your application will consume more memory.
-One good choose from my point of view is to profile your application using Fixed/Dynamic thread pool , and to see your application metrics when you increase/decrease the num of threads.
-For example you could keep the memory footprint low choosing a DynamicThreadPool with 5 threads, and allow to create new threads until 50/100 when needed, this is the advantage to use the DynamicThreadPool.
-But in general , always profile your application +To choose your pool consider that with a FixedThreadPool or a DynamicThreadPool (in this case is important the min parameter passed to the constructor) your application memory footprint will increase. +Increasing the memory footprint, your application will be ready to accept more CPU bound tasks, but during idle time your application will consume more memory. +One good choose from my point of view is to profile your application using Fixed/Dynamic thread pool, and to see your application metrics when you increase/decrease the num of threads. +For example you could keep the memory footprint low choosing a DynamicThreadPool with 5 threads, and allow to create new threads until 50/100 when needed, this is the advantage to use the DynamicThreadPool. +But in general, **always profile your application** -

Contribute

+## Contribute -See guidelines [CONTRIBUTING](CONTRIBUTING.md)
-Choose your task here 2.0.0, propose an idea, a fix, an improvement.
+See guidelines [CONTRIBUTING](CONTRIBUTING.md) +Choose your task here [2.0.0](https://github.com/pioardi/poolifier/projects/1), propose an idea, a fix, an improvement. +## Team -

Team

**Creator/Owner:** -* [__Alessandro Pio Ardizio__](https://github.com/pioardi) -***Contributors*** -* [__Shinigami92__](https://github.com/Shinigami92) -* [__Jérôme Benoit__](https://github.com/jerome-benoit) +- [**Alessandro Pio Ardizio**](https://github.com/pioardi) + +**_Contributors_** + +- [**Shinigami92**](https://github.com/Shinigami92) +- [**Jérôme Benoit**](https://github.com/jerome-benoit) -

License

+## License [MIT](./LICENSE) diff --git a/package-lock.json b/package-lock.json index e3e205ec..9f0e7521 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1660,9 +1660,9 @@ } }, "eslint-plugin-prettierx": { - "version": "0.17.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-prettierx/-/eslint-plugin-prettierx-0.17.0.tgz", - "integrity": "sha512-TsAyEIGvbcuJlnCwFjUqhQ2bMlPn1mX9JdmxvH8pPOVGBPQ6h4K7bVkH/6+TeVmpV41Rj/m5CXe0SU28OK46hg==", + "version": "0.17.1", + "resolved": "https://registry.npmjs.org/eslint-plugin-prettierx/-/eslint-plugin-prettierx-0.17.1.tgz", + "integrity": "sha512-UEcZtVY8+U8TwFcBFR7tKI+eBdYxU1iPbynUnCkofT/7e/msm981JY97gY0lI/GIIVG/xcIJST5PYzlTc/SwVw==", "dev": true, "requires": { "eslint-config-prettier": "~7.2.0", diff --git a/package.json b/package.json index a30ae25f..418bff36 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,10 @@ "scripts": { "build": "npm run build:clean && tsc", "build:clean": "rimraf lib", - "test": "npm run build && nyc mocha --exit --timeout 20000 tests/*test.js ", - "test:debug": "mocha --inspect-brk --exit tests/*test.js ", + "test": "npm run build && nyc mocha --exit --timeout 20000 tests/**/*.test.js", + "test:debug": "mocha --inspect-brk --exit tests/**/*.test.js", "coverage": "nyc report --reporter=text-lcov | coveralls", + "coverage:html": "nyc --reporter=html mocha --exit --timeout 20000 tests/**/*.test.js", "format": "prettier --loglevel silent --write .; prettierx --write .", "lint": "eslint .", "lint:fix": "eslint . --fix" @@ -54,7 +55,7 @@ "eslint-config-standard": "^16.0.2", "eslint-plugin-import": "^2.22.1", "eslint-plugin-node": "^11.1.0", - "eslint-plugin-prettierx": "^0.17.0", + "eslint-plugin-prettierx": "^0.17.1", "eslint-plugin-promise": "^4.2.1", "expect": "^26.6.2", "mocha": "^8.2.1", @@ -67,6 +68,7 @@ "typescript": "^4.1.3" }, "engines": { - "node": ">=12.11.0" + "node": ">=12.11.0", + "npm": ">=6.0.0 <7" } } -- 2.34.1