How Node.js Multiprocess Load Balancing Works

As of version 0.6.0 of node, load multiple process load balancing is available for node. The concept of forking and child processes isn‘t new to me. Yet, it wasn‘t obvious to me how this was implemented at first. It‘s quite easy to use however:

var cluster = require(‘cluster‘);var http = require(‘http‘);var numCPUs = require(‘os‘).cpus().length;

if (cluster.isMaster) {  // Fork workers.  for (var i = 0; i < numCPUs; i++) {    cluster.fork();  }

cluster.on(‘death‘, function(worker) {    console.log(‘worker ‘ + worker.pid + ‘ died‘);  });} else {  // Worker processes have a http server.  http.Server(function(req, res) {    res.writeHead(200);    res.end("hello world\n");  }).listen(8000);}

This is quite a beautiful api, but it hides some clever details. For instance, how is possible for all child processes to each open up port 8000 and start listening?

The answer is that the cluster module is just a wrapper around child_process.fork and net.Server.listen is aware of the cluster module. Specifically, cluster.fork uses child_process.fork to create child processes with special variable set in their environments to indicate cluster was used. Specifically process.env.NODE_WORKER_ID is non-zero for such children.

envCopy[‘NODE_WORKER_ID‘] = id;

var worker = fork(workerFilename, workerArgs, { env: envCopy });

Then net.Server.listen checks to see if process.env.NODE_WORKER_ID is set. If so, then the current process is a child created cluster. Instead of trying to start accepting connections on this port, a file handle is requested from the parent process:

//inside net.js
require(‘cluster‘)._getServer(address, port, addressType, function(handle) {

self._handle = handle;

self._listen2(address, port, addressType);

});

//inside cluster.js
cluster._getServer = function(address, port, addressType, cb) {

assert(cluster.isWorker);

queryMaster({

cmd: "queryServer",

address: address,

port: port,

addressType: addressType

}, function(msg, handle) {

cb(handle);

});

};

Finally, this handle is listened on instead of creating a new handle. At which point you have another process that is listening on the same port. Quite clever, though I think the beauty of the api comes at the cost of some not so hideous hacks inside the api.

时间: 2024-12-15 01:43:48

How Node.js Multiprocess Load Balancing Works的相关文章

How Network Load Balancing Technology Works--reference

http://technet.microsoft.com/en-us/library/cc756878(v=ws.10).aspx In this section Network Load Balancing Terms and Definitions Network Load Balancing Architecture Network Load Balancing Protocols Application Compatibility with Network Load Balancing

【架构】How To Use HAProxy to Set Up MySQL Load Balancing

How To Use HAProxy to Set Up MySQL Load Balancing Dec  2, 2013 MySQL, Scaling, Server Optimization Ubuntu, Debian Prelude HAProxy is an open source software which can load balance HTTP and TCP servers. In the previous article on HAProxy we configured

Network Load Balancing Technical Overview--reference

http://technet.microsoft.com/en-us/library/bb742455.aspx Abstract Network Load Balancing, a clustering technology included in the Microsoft Windows 2000 Advanced Server and Datacenter Server operating systems, enhances the scalability and availabilit

[Node.js] Load balancing a Http server

Let's see how to do load balancing in Node.js. Before we start with the solution, you can do a test to see the ability concurrent requests your current machine can handle. This is our server.js: const http = require('http'); const pid = process.pid;

Node.js Tools 1.2 for Visual Studio 2015 released

https://blogs.msdn.microsoft.com/visualstudio/2016/07/28/node-js-tools-1-2-visual-studio-2015/ What time is it?! Time to announce that our next stable release of Node.js Tools 1.2 for Visual Studio (NTVS) is available for download! NTVS 1.2 supports

A chatroom for all! Part 1 - Introduction to Node.js(转发)

项目组用到了 Node.js,发现下面这篇文章不错.转发一下.原文地址:<原文>. ------------------------------------------- A chatroom for all! Part 1 - Introduction to Node.js Rami Sayar 4 Sep 2014 11:00 AM 7 This node.js tutorial series will help you build a node.js powered real-time

node.js+mongodb 爬虫

demo截图: 本demo爬瓜子二手车北京区的数据 (注:需要略懂 node.js / mongodb 不懂也没关系 因为我也不懂啊~~~) 之所以选择爬瓜子二手车网站有两点: 一.网站无需登录,少做模拟登录: 二.数据连接没有加密,直接可以用: 网上很多node.js爬虫的栗子 但大多是一个页面的栗子,很少跟数据库结合的 所以我这个栗子是糖炒的 我的基本思路是这样的 1.先在mongodb里存所有页的连接地址的集合 2.在根据这些链接地址 一个一个的把详细信息爬下来 第一步在搜索页找到翻页的规

node.js require() 源码解读

时至今日,Node.js 的模块仓库 npmjs.com ,已经存放了15万个模块,其中绝大部分都是 CommonJS 格式.这种格式的核心就是 require 语句,模块通过它加载.学习 Node.js ,必学如何使用 require 语句.本文通过源码分析,详细介绍 require 语句的内部运行机制,帮你理解 Node.js 的模块机制. 一.require() 的基本用法分析源码之前,先介绍 require 语句的内部逻辑.如果你只想了解 require 的用法,只看这一段就够了.下面的

Node.js系列基础学习----安装,实现Hello World, REPL

Node.js基础学习 1:简介 简单的说 Node.js 就是运行在服务端的 JavaScript.Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台.Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好 2:安装 à安装node.js 在官网安装自己win版本的node.js的版本,下载,安装完毕后在运行中输入node -v若是出现版本号就证明安装成功. à安装n