@paybase/pool
Advanced tools
Comparing version 1.0.0 to 1.1.0
23
index.js
const { channel, put, take, drain } = require('@paybase/csp'); | ||
const isFunction = x => typeof x === 'function'; | ||
const createPool = ({ | ||
poolSize = 5, | ||
createProcess = () => {}, | ||
createProcess, | ||
createAsyncProcess, | ||
handler = (_, x) => Promise.resolve(x), | ||
} = {}) => { | ||
const pool = Array(poolSize).fill(0); | ||
const processPool = channel(); | ||
Array(poolSize).fill(0) | ||
.forEach(() => put(processPool, createProcess())); | ||
if (!isFunction(createProcess) && !isFunction(createAsyncProcess)) | ||
throw new Error(`Please provide a process creator`); | ||
if (isFunction(createProcess) && isFunction(createAsyncProcess)) | ||
throw new Error(`Unable to create both a sync pool and an async pool, please choose one!`); | ||
const run = (value) => | ||
@@ -36,3 +43,11 @@ new Promise(async (resolve, reject) => { | ||
return { run, close }; | ||
if (createProcess) { | ||
pool.forEach(() => put(processPool, createProcess())); | ||
return { run, close }; | ||
} else { | ||
return Promise.all(pool.map(() => createAsyncProcess())) | ||
.then(ps => ps.forEach(p => put(processPool, p))) | ||
.then(() => ({ run, close })); | ||
} | ||
}; | ||
@@ -39,0 +54,0 @@ |
{ | ||
"name": "@paybase/pool", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "a pooling mechanism built on top of csp", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# @paybase/pool | ||
`async/await` pooling mechanism built on top of Communicating Sequential Processes. | ||
A highly flexible process pooling library for Node.js. Built with [@paybase/csp](https://github.com/paybase/csp). | ||
### Installation | ||
[![npm version](https://badge.fury.io/js/%40paybase%2Fpool.svg)](https://badge.fury.io/js/%40paybase%2Fpool) | ||
## Installation | ||
``` | ||
@@ -17,10 +19,28 @@ $ npm install --save @paybase/pool | ||
### Example Usage | ||
## API | ||
Pooling can simplify many complex problems, for example: | ||
This library exposes a single factory method for creating pools. | ||
#### Network I/O parallelisation | ||
### `createPool({ poolSize = 5, createProcess, createAsyncProcess, handler })` -> `Pool|Promise<Pool>` | ||
By defining a process as something which is akin to a mutex lock, we can limit request parallelisation to the size of the pool. | ||
The pool factory takes an options object containing 3 of 4 properties: | ||
- `poolSize` - defaults to 5, determines the size of the pool | ||
- `createProcess` - defines a `process` factory function which can return anything | ||
- `createAsyncProcess` - defines an async `process` factory which can return anything, useful if your process requires time to become active. | ||
- `handler(process, input)` -> `Promise` - defines a function which handles a unit of work. The handler must return a `Promise` and receives a `process` (as defined by the `process` factory) and the `input` from a call to `run` on the pool | ||
You must supply only one of `createProcess` or `createAsyncProcess`! If you supply `createAsyncProcess` the return value of the `createPool` factory will be a `Promise<Pool>`. | ||
A returned `Pool` exposes 2 methods: | ||
- `Pool.run(input)` -> `Promise` - The interface defined to run against the pool of processes, supplied input can be of any type as the handler supplied at `pool` creation defines how the input interacts which the underlying process | ||
- `Pool.close` -> `Promise` - A mechanism for destroying the `pool` when it is no longer needed | ||
## Example Usage | ||
### Network I/O parallelisation | ||
By defining our process as a plain `Symbol`, or `true` for that matter, we can limit request parallelisation to the size of the pool. | ||
```javascript | ||
@@ -33,3 +53,3 @@ const assert = require('assert'); | ||
poolSize: 2, | ||
createProcess: () => Symbol('ticket'), | ||
createProcess: () => Symbol('process'), | ||
handler: (_, query) => { | ||
@@ -53,5 +73,5 @@ console.log(`🚀 running request with query: ${query}`); | ||
#### Child process pooling | ||
### Child process pooling | ||
For spawning multiple child processes and spreading usage between the pool. | ||
For spawning multiple child processes and spreading work across processes in the pool. | ||
@@ -92,21 +112,4 @@ ```javascript | ||
### API | ||
## Contributions | ||
This library exposes a single factory method for creating pools. | ||
##### `createPool({ poolSize = 5, createProcess, handler })` -> `Pool` | ||
The pool factory takes an options object containing 3 properties: | ||
- `poolSize` - defaults to 5, determines the size of the pool | ||
- `createProcess` - defines a `process` factory function which can return anything | ||
- `handler(process, input)` -> `Promise` - defines a function which handles a unit of work. The handler must return a `Promise` and receives a `process` (as defined by the `process` factory) and the `input` from a call to `run` on the pool | ||
A returned `Pool` exposes 2 methods: | ||
- `Pool.run(input)` -> `Promise` - The interface defined to run against the pool of processes, supplied input can be of any type as the handler supplied at `pool` creation defines how the input interacts which the underlying process | ||
- `Pool.close` -> `Promise` - A mechanism for destroying the `pool` when it is no longer needed | ||
### Contributions | ||
Contributions are welcomed and appreciated! | ||
@@ -120,4 +123,4 @@ | ||
### License | ||
## License | ||
Please see the `LICENSE` file for more information. |
Sorry, the diff of this file is not supported yet
91067
46
122
7