npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@paybase/pool

v1.1.6

Published

a pooling mechanism built on top of csp

Downloads

49

Readme

@paybase/pool

A highly flexible process pooling library for Node.js. Built with @paybase/csp.

npm version

Installation

$ npm install --save @paybase/pool

or

$ yarn add @paybase/pool

API

This library exposes a single factory method for creating pools.

createPool({ poolSize = 5, createProcess, createAsyncProcess, handler }) -> Pool|Promise<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.

const assert = require('assert');
const fetch = require('node-fetch');
const createPool = require('@paybase/pool');

const { run, close } = createPool({
  poolSize: 2,
  createProcess: () => Symbol('process'),
  handler: (_, query) => {
    console.log(`🚀  running request with query: ${query}`);
    return fetch(`https://postman-echo.com/get?q=${query}`)
      .then(res => res.json())
      .then(res => assert.equal(res.args.q, query));
      .then(_ => console.log(`👌  request completed successfully`));
  }
});

(async () => {
  const queries = Array.from({ length: 20 }, (_, i) => run(`${++i}`));
  await Promise.all(queries);
  close();
})();

request parallelisation

Child process pooling

For spawning multiple child processes and spreading work across processes in the pool.

const assert = require('assert');
const { spawn } = require('child_process');
const createPool = require('@paybase/pool');

const { run, close } = createPool({
  poolSize: 10,
  createProcess: () => {
    const p = spawn('cat', [ '-' ]);
    p.stdin.setEncoding('utf-8');
    p.stdout.setEncoding('utf-8'); 
    return p;
  },
  handler: (p, input) =>
    new Promise(resolve => {
      p.stdout.once('data', d => {
        assert(d, input);
        console.log(`👌  received data: ${d.trim()} from pid: ${p.pid}`);
        resolve(d);
      });
      console.log(`🚀  sending data: ${input.trim()} to pid: ${p.pid}`);
      p.stdin.write(input);
    }),   
});

(async () => {
  const inputs = Array.from({ length: 100 }, (_, i) => run(`${++i}\n`));
  await Promise.all(inputs);
  close();
})();

child process pool

Contributions

Contributions are welcomed and appreciated!

  1. Fork this repository.
  2. Make your changes, documenting your new code with comments.
  3. Submit a pull request with a sane commit message.

Feel free to get in touch if you have any questions.

License

Please see the LICENSE file for more information.