Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

throng

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

throng - npm Package Compare versions

Comparing version 2.0.1 to 3.0.0

33

lib/throng.js

@@ -1,8 +0,10 @@

var os = require('os');
var cluster = require('cluster');
var EventEmitter = require('events').EventEmitter;
var defaults = require('lodash').defaults;
'use strict';
var DEFAULT_OPTIONS = {
workers: os.cpus().length,
const cluster = require('cluster');
const EventEmitter = require('events').EventEmitter;
const defaults = require('lodash.defaults');
const cpuCount = require('os').cpus().length;
const DEFAULT_OPTIONS = {
workers: cpuCount,
lifetime: Infinity,

@@ -12,11 +14,14 @@ grace: 5000

module.exports = function(startFn, options) {
module.exports = function throng(options, startFunction) {
let startFn = typeof options === 'function' ?
options : startFunction;
if (cluster.isWorker) return startFn(cluster.worker.id);
options = defaults(options, DEFAULT_OPTIONS);
let opts = isNaN(options) ?
defaults(options, DEFAULT_OPTIONS) : defaults({ workers: options }, DEFAULT_OPTIONS);
let emitter = new EventEmitter();
let running = true;
let runUntil = Date.now() + opts.lifetime;
var emitter = new EventEmitter();
var running = true;
var runUntil = Date.now() + options.lifetime;
listen();

@@ -34,3 +39,3 @@ fork();

function fork() {
for (var i = 0; i < options.workers; i++) {
for (var i = 0; i < opts.workers; i++) {
cluster.fork();

@@ -49,3 +54,3 @@ }

}
setTimeout(forceKill, options.grace).unref();
setTimeout(forceKill, opts.grace).unref();
}

@@ -52,0 +57,0 @@

{
"name": "throng",
"version": "2.0.1",
"version": "3.0.0",
"description": "A simple worker-manager for clustered apps",

@@ -13,3 +13,6 @@ "keywords": [

"bugs": "https://github.com/hunterloftis/throng/issues",
"main": "index.js",
"main": "lib/throng.js",
"files": [
"lib"
],
"scripts": {

@@ -19,3 +22,3 @@ "test": "mocha"

"engines": {
"node": ">= 0.10.x"
"node": ">= 4.0.0"
},

@@ -25,8 +28,8 @@ "author": "Hunter Loftis <hunter@hunterloftis.com>",

"devDependencies": {
"chai": "3.5.0",
"mocha": "2.4.5"
"chai": "^3.5.0",
"mocha": "^2.4.5"
},
"dependencies": {
"lodash": "4.3.0"
"lodash.defaults": "^4.0.1"
}
}
# Throng
Dead-simple one-liner for clustered apps.
Dead-simple one-liner for clustered Node.js apps.
Runs X workers and respawns them if they go down.
Correctly handles signals from the OS.
```js
throng(start, { workers: 3 });
const throng = require('throng');
function start(id) {
throng((id) => {
console.log(`Started worker ${id}`);
});
```
process.on('SIGTERM', function() {
console.log(`Worker ${id} exiting`);
process.exit();
});
}
```
$ node example
Started worker 1
Started worker 2
Started worker 3
Started worker 4
```

@@ -24,18 +30,46 @@ ## Installation

For older versions of node (< 4.x), use throng 2.x.
## Use
```js
throng(startFunction, options);
throng(startFunction);
```
## All Options
Provide a worker count:
```js
throng(start, {
workers: 4, // Number of workers; defaults to CPU count
lifetime: 10000, // ms to keep cluster alive; defaults to Infinity
grace: 4000 // ms grace period after worker SIGTERM; defaults to 5000
throng(3, startFunction);
```
Specify more options:
```js
throng({ workers: 16, grace: 1000 }, startFunction);
```
Handle signals (for cleanup on a kill signal, for instance):
```js
throng((id) => {
console.log(`Started worker ${id}`);
process.on('SIGTERM', function() {
console.log(`Worker ${id} exiting`);
console.log('Cleanup here');
process.exit();
});
});
```
## All Options (with defaults)
```js
throng({
workers: 4, // Number of workers (cpu count)
lifetime: 10000, // ms to keep cluster alive (Infinity)
grace: 4000 // ms grace period after worker SIGTERM (5000)
}, startFn);
```
## Tests

@@ -42,0 +76,0 @@

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