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

net-cluster

v0.0.2

Published

a drop-in replacement for node's net module that works in a sane way when clustered

Downloads

9,588

Readme

net-cluster

Build Status

a drop-in replacement for node's net module that works in a sane way when clustered

listening on random port

server.listen(0)

http://nodejs.org/api/cluster.html

from the documentation:

Normally, this will case servers to listen on a random port. However, in a cluster, each worker will receive the same "random" port each time they do listen(0). In essence, the port is random the first time, but predictable thereafter. If you want to listen on a unique port, generate a port number based on the cluster worker ID.

2 problems with this functionality

  • listen(0) should always bind to a random port
  • the listen(cluster.workerId) is unreliable
    • you can't gurantee that the port cluster.workerId is available

how net-cluster operates

listen(0) always binds to a random port

usage

net-cluster strictly implements node's net module.

/**
 * uses net-cluster instead of net to create a server
 * 
 * Note: 2 servers will be created and they will be listening
 * on 2 distinct random ports. Try using node's net module instead
 * of net-cluster and see what happens.
 */
var cluster = require('cluster')
  , net = require('net-cluster')
  ;
  
if (cluster.isMaster) {
  cluster.fork();
  cluster.fork();
} else if (cluster.isWorker) {
  var server = net.createServer();
  server.listen(function() {
    var port = server.address().port;
    console.log('listening on port: ' + port);
  });
}

use cases

  • network application that needs to open multiple non-pre-determined ports
  • if you provide a module that listens on port 0
    • this is a difficult bug for people using your module. Not only will every instance of your module code be sharing the same socket, but ANY other piece of code using listen(0) will be sharing the same socket (other modules or even the user's code)

license

MIT