nodejs中cluster使用

摘自<node.js即学即用>第3章

使用NODE中cluster利用多核CPU

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// 创建工作进程
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('death', function(worker) {
console.log('worker ' + worker.pid + ' died');
cluster.fork();//重启子进程
});
} else {
// 工作进程创建http 服务器
http.Server(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
}


通过消息传递来监控工作进程状态

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
var rssWarn = (12 * 1024 * 1024)
    , heapWarn = (10 * 1024 * 1024)
if(cluster.isMaster) {
    for(var i=0; i<numCPUs; i++) {
        var worker = cluster.fork();
        worker.on('message', function(m) {
            if (m.memory) {
                console.log(m.memory.rss,rssWarn)
                if(m.memory.rss > rssWarn) {
                    console.log('Worker ' + m.process + ' using too much memory.')
                }
            }

        })
    }
} else {
// 服务器
    http.createServer(function(req,res) {
        res.writeHead(200);
        res.end('hello world\n')
    }).listen(8000)
// 每秒报告一次状态
    setInterval(function report(){
        process.send({memory: process.memoryUsage(), process: process.pid});
    }, 1000)
}

杀死僵尸进程


var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
var rssWarn = (50 * 1024 * 1024)
    , heapWarn = (50 * 1024 * 1024)
var workers = {}
if(cluster.isMaster) {
    for(var i=0; i<numCPUs; i++) {
        createWorker()
    }
    setInterval(function() {
        var time = new Date().getTime()
        for(pid in workers) {
            if(workers.hasOwnProperty(pid) &&
                workers[pid].lastCb + 5000 < time) {
                console.log('Long running worker ' + pid + ' killed')
                workers[pid].worker.kill()
                delete workers[pid]
                createWorker()
            }
        }
    }, 1000)
} else {
// 服务器
    http.Server(function(req,res) {
// 打乱200 个请求中的1 个
        if (Math.floor(Math.random() * 200) === 4) {
            console.log('Stopped ' + process.pid + ' from ever finishing')
            while(true) { continue }
        }
        res.writeHead(200);
        res.end('hello world from ' + process.pid + '\n')
    }).listen(8000)
// 每秒钟报告一次状态
    setInterval(function report(){
        process.send({cmd: "reportMem", memory: process.memoryUsage(),
            process: process.pid})
    }, 1000)
}
function createWorker() {
    var worker = cluster.fork()
    console.log('Created worker: ' + worker.pid)
// 允许开机时间
    workers[worker.pid] = {worker:worker, lastCb: new Date().getTime()-1000}
    worker.on('message', function(m) {
        if(m.cmd === "reportMem") {
            workers[m.process].lastCb = new Date().getTime()
            if(m.memory.rss > rssWarn) {
                console.log('Worker ' + m.process + ' using too much memory.')
            }
        }
    })
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值