promise-pool 1.1.0
Install from the command line:
Learn more about npm packages
$ npm install @elite-libs/promise-pool@1.1.0
Install via package.json:
"@elite-libs/promise-pool": "1.1.0"
About this version
A background task processor focused on reliability and scalability.
- [x] Configurable.
- [x] Concurrency limit.
- [x] Retries. (Use
p-retry
for this.) - [x] Zero dependencies.
- [x] Error handling.
- [x] Singleton mode: Option to auto-reset when
.done()
. (Added.drain()
method.) - [x] Task scheduling & prioritization.
- [x] Optionally
return
hints. (To support API's like GitHub which return rate limit hints in http headers, e.g.X-RateLimit-Remaining
header.)
- [x] Optionally
PromisePool
exposes 2 methods: .add(...tasks)
and .done()
. This is to make it easy to implement & operate.
Tasks added to the pool will begin executing immediately, with a concurrency limit as configured (default of 4
).
# with npm
npm install @elite-libs/promise-pool
# or using yarn
yarn add @elite-libs/promise-pool
import PromisePool from '@elite-libs/promise-pool';
// 1/3: Either use the default instance or create a new one.
const pool = new PromisePool();
// 2/3: Add task(s) to the pool as needed.
// PromisePool will execute them in parallel as soon as possible.
pool.add(() => saveToS3(data));
pool.add(() => expensiveBackgroundWork(data));
// 3/3: REQUIRED: in order to ensure your tasks are executed,
// you must await either `pool.drain()` or `pool.done()` at some point in your code (`done` prevents additional tasks from being added).
await pool.done();
In this example we'll use a singleton pattern to ensure we have only 1 Promise Pool
instance per process.
// `./src/services/taskPool.ts`
import PromisePool from '@elite-libs/promise-pool';
export const taskPool = new PromisePool({
maxWorkers: 6, // Optional. Default is `4`.
});
Then from inside your app (Lambda function, Express app, etc.) you can use the taskPool
instance to add tasks to the pool.
// Example Lambda function
// `./src/handlers/user.handler.ts`
import { taskPool } from './services/taskPool';
export const handler = async (event, context) => {
const data = getDataFromEvent(event);
taskPool.add(() => saveToS3(data));
taskPool.add(() => expensiveBackgroundWork(data));
await taskPool.drain();
return {
statusCode: 200,
body: JSON.stringify({
message: 'Success',
}),
}
}
Note the await taskPool.drain()
call. This is required to ensure your tasks are executed.
You could also utilize Promise Pool
in middy
middleware:
import PromisePool, { PoolConfig } from '@elite-libs/promise-pool';
const defaults = {};
const promisePoolMiddleware = (opts: Partial<PoolConfig> = {}) => {
const options = { ...defaults, ...opts };
return {
before: (request) => {
Object.assign(request.context, {
taskPool: new PromisePool(options),
});
},
after: async (request) => {
await request.context.taskPool.drain();
}
}
}
export default promisePoolMiddleware;
// Important: Import a global instance of `Promise Pool`
import { taskPool } from './services/taskPool';
const taskPoolMiddleware = () => ({
before: (request) => {
Object.assign(request.context, { taskPool });
},
after: async (request) => {
await request.context.taskPool.drain();
}
});
export default taskPoolMiddleware;
Now you can use taskPool
in your Lambda function:
request.context.taskPool.add(() => saveToS3(data));
Now the drain()
method will be called automatically after()
every Lambda function returns.
See
/examples
folder for more examples.
Details
- promise-pool
- elite-libs
- over 2 years ago
- BSD-3-Clause
- 15 dependencies
Assets
- promise-pool-1.1.0-npm.tgz
Download activity
- Total downloads 0
- Last 30 days 0
- Last week 0
- Today 0