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

@nicholaswmin/threadpool

v1.0.0

Published

tiny threadpool with ergonomic IPC

Downloads

10

Readme

test-url dep-url

:thread: threadpool

thread pool with ergonomic IPC

Install

npm i @nicholaswmin/threadpool

Example

messaging between the primary and 4 threads:

// primary.js

import { Threadpool } from '@nicholaswmin/threadpool'

const pool = new Threadpool('thread.js', 4)

await pool.start()

pool
  .on('pong', () => {
    console.log('🏓 pong')
    pool.emit('ping')
  })
  .emit('ping')

and:

// thread.js 

import { primary } from '@nicholaswmin/threadpool'

primary.on('ping', () => {
  console.log('ping 🏓')

  setTimeout(() => primary.emit('pong'), 100)
})

then:

node primary.js

logs:

# ping 🏓
# 🏓 pong
# ping 🏓
# 🏓 pong
# ...

API

new Threadpool(path, size, env)

Creates a pool.

| name | type | description | default | |--------------|----------|--------------------------|-----------------------| | path | String | file path of thread code | current path | | size | Number | number of threads | available cores | | env | Object | Thread env. variables | primary process.env |

await pool.start()

Starts the pool.

await pool.stop()

Shutsdown all threads, removes all listeners and stops the pool

Returns array of exit codes.

Messaging

primary-to-thread IPC:

pool.on(name, listener)

Listens for an emitted event, across all threads.

| name | type | description | |------------|------------|-------------------| | name | String | name of event | | listener | Function | callback function |

pool.once(name, listener)

Listens for an emitted event once, across all threads.
As soon as the listener fires it is removed.

pool.off(name, listener)

Removes a listener of a given event, across all threads.

pool.removeAllListeners(name)

Removes all listeners of a given event, across all threads.

pool.emit(name, data)

Sends the event to a single thread, chosen in round-robin.

pool.broadcast(name, data)

Sends the event to every thread, in fan-out

Emitted Events

'pool-error'

Emitted if an uncaught error is thrown in a thread.
The error is provided as an Error in a listener argument.

A shutdown is attempted before emitting this event.

If the shutdown fails, the Error instance will contain the shutdown error and the error.cause will contain the originating thread error.

Thread API

thread.pid

Thread's Process ID

thread.exitCode

  • null: is alive
  • 0: exited with exit-code: 0
  • 1: threw uncaught exception or killed with any signal other than SIGTERM.

Primary API

For thread-to-primary IPC, for usage in the thread code file

primary.on(name, listener)

Listen for events emitted from the primary.

primary.emit(name, data)

Emit an event to the primary.

Graceful exits

Use beforeExit to call pool.stop(), like so:

// primary.js

process.on('beforeExit', async () => {
  try {
    await pool.stop()
    process.exit(0)
  } catch (err) {
    console.error(err)
    process.exit(1)
  }
})

Timeouts

Threads which block the event loop or delay their termination are issued a SIGKILL signal, after a set timeout.

timeouts are in ms and can be set like so:

import { Threadpool } from '@nicholaswmin/threadpool'

Threadpool.spawnTimeout = 500
Threadpool.readyTimeout = 500
Threadpool.killTimeout  = 500

const pool = new Threadpool('thread.js')

// ... rest of code

Gotchas

  • Runtime exceptions trigger a shutdown/stop.
  • Cyclic pool.broadcasts can create an exponentially-increasing send rate.
  • Based on fork() so technically it's multi-processing, each "thread" being an isolated V8 instance.

Test

NODE_ENV=test node --run test

test coverage saved as: test/lcov.info

Benchmark

Run a ping/pong benchmark

node --run benchmark -- --size=4 --kibs=10

4 threads, each ping sending 10 kilobytes of event data

Contributing

Follows Semver, Github Flow & Conventional Commits.
Changes must be accompanied by 100% unit-test coverage.

Authors

@nicholaswmin

License

The MIT License