New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@gamedevsam/throng

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@gamedevsam/throng

A simple worker-manager for clustered apps, updated with TypeScript

  • 6.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

Throng

npm package Dependency Status devDependency Status Build Status

Dead-simple one-liner for clustered Node.js apps.

Forks N workers and creates new ones if they go down. Correctly handles signals from the OS.

// CJS
const throng = require('throng');

throng((id) => console.log(`Started worker ${id}`));

// ESM
import { throng } from 'throng';

throng((id) => console.log(`Started worker ${id}`));
$ node examples/basic
Started worker 1
Started worker 2
Started worker 3
Started worker 4

Installation

$ npm install --save throng

Use

Fork 1 worker per CPU:

throng(workerStartFunction);

Specify the number of workers:

throng({ worker: workerStartFunction, count: 3 });

More options:

throng({
  primary: primaryStartFunction,
  worker: workerStartFunction,
  count: 16,
  grace: 1000
});

Handling signals:

(for cleaning up before disconnecting a worker on a SIGTERM, for instance)

throng({ worker })

function worker(id, disconnect) {
  console.log(`Started worker ${id}`)

  process.on('SIGTERM', () => {
    console.log(`Worker ${id} exiting (cleanup here)`)
    disconnect()
  })
})

All options (with defaults)

throng({
  primary: () => {},              // Fn to call in master process (can be async)
  worker: yourWorkerFunc,         // Fn to call in cluster workers (can be async)
  workerCreated: undefined        // Fn to call when a worker is created
  count: os.cpus().length,        // Number of workers
  lifetime: Infinity,             // Min time to keep cluster alive (ms)
  grace: 5000,                    // Grace period between signal and hard shutdown (ms)
  signals: ['SIGTERM', 'SIGINT']  // Signals that trigger a shutdown (proxied to workers)
})

A complex example

const throng = require('throng');

throng({ primary, worker, count: 4 });

// This will only be called once
function primary() {
  console.log('Started primary');

  process.on('beforeExit', () => {
    console.log('Primary cleanup.');
  });
}

// This will be called four times
function worker(id, disconnect) {
  let exited = false;

  console.log(`Started worker ${id}`);
  process.on('SIGTERM', shutdown);
  process.on('SIGINT', shutdown);

  async function shutdown() {
    if (exited) return;
    exited = true;

    await new Promise((r) => setTimeout(r, 300)); // simulate async cleanup work
    console.log(`Worker ${id} cleanup done.`);
    disconnect();
  }
}
$ node examples/complex
Started primary
Started worker 1
Started worker 3
Started worker 2
Started worker 4
^C
Worker 1 cleanup done.
Worker 3 cleanup done.
Worker 2 cleanup done.
Worker 4 cleanup done.
Primary cleanup.

Staying alive

Throng forks replacements for workers that crash so your cluster can continue working through failures.

$ node examples/crashy
-1--2--3--4--2--1--3--4--crash!--1--3--4--crash!--5--3--4--6--5--3--4--crash!--6--crash!--crash!--7--6--8--9--7--6--8--9--crash!--7--6--9--10--7--6--9--10--crash!--7--9--10--11--7--crash!--9--crash!--7--12--9--13--crash!--12--9--crash!--crash!--crash!--14--crash!--12--15--crash!--14--18--15--19--14--18--15--crash!--19--14--crash!--15--20--14--21--15--20--14--21--15--20--14--21--15--20--14--21--15-

Test

$ docker-compose run --rm dev

node@docker:/home/app$ npm test

Keywords

FAQs

Package last updated on 10 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc