`
leonzhx
  • 浏览: 793541 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Chapter 3. Building Robust Node Applications

阅读更多

 

1.  On the server, instead of the limited set of events based on the user-driven interaction with the web page’s DOM, we have an infinite variety of events based on what’s happening in the server software we use. For example, the HTTP server module provides an event called “request,” emitted when a user sends the web server a request.

 

2.  Node takes the approach that all I/O activities should be non-blocking. This means that HTTP requests, database queries, file I/O, and other things that require the program to wait do not halt execution until they return data. Instead, they run independently, and then emit an event when their data is available.

 

3.  Everything that Node does is non-blocking, so the time between an event being emitted and Node being able to act on that event is very short because it’s not waiting on things such as disk I/O.

 

4.  If all (or most) of the work your server does is computation, Node might not be the ideal model.

 

5.  If we didn’t attach any events, Node.js would exit as soon as it had run the code.

 

6.  If we emit the event from within a callback, we already know our postman will go and deliver that letter before carrying on, but when events are emitted outside the currently executing piece of code, they will not be called until that piece of code has been fully evaluated to its conclusion.

 

 

7.  You should follow two strategies when writing a Node.js server:

  a)  Once setup has been completed, make all actions event-driven.

  b)  If Node.js is required to process something that will take a long time, consider delegating it to web workers.

 

8.  All I/O in Node is unordered parallel by default. This is because all I/O in Node is asynchronous and non-blocking. When we do any I/O, we simply throw the request out there and see what happens. It’s possible that all the requests will happen in the order we made them, but maybe they won’t.

 

9.  Ordered serial requests are also easy to make by nesting or referencing callbacks together so that the first callback will initiate the second I/O request, the second callback will initiate the third, and so on. Even though each request is asynchronous and doesn’t block the event loop, the requests are made in serial. This pattern of ordered requests is useful when the results of one I/O operation have to inform the details of the next I/O request. Each previous task must be completed before the next task is started. In Node, this means nesting callbacks so that the callback from each task starts the next task.

 

10.  Another approach that changes the style of code is to use declared functions instead of just anonymous or named ones. This removes the natural pyramid seen in the other approaches, which shows the order of execution, but it also breaks the code out into more manageable chunks

 

11.  error event is a special event that is fired when an error occurs. It allows a module engaging in I/O to fire an alternative event to the one the callback was listening for to deal with the error. The error event allows us to deal with any errors that might occur in any of the callbacks that happen in any modules we use.

 

12.  Node provides a module called cluster that allows you to delegate work to child processes. This means that Node creates a copy of its current program in another process (on Windows, it is actually another thread). Each child process has some special abilities, such as the ability to share a socket with other children. This allows us to write Node programs that start many other Node programs and then delegate work to them. 

 

13.  When you use cluster to share work between a number of copies of a Node program, the master process isn’t involved in every transaction. The master process manages the child processes, but when the children interact with I/O they do it directly, not through the master. This means that if you set up a web server using cluster, requests don’t go through your master process, but directly to the children. Hence, dispatching requests does not create a bottleneck in the system.

 

14. Using cluster to distribute work:

 

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');
    cluster.fork();
  });
} else {
  // Worker processes have a http server.
  http.Server(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(8000);
}

 

 

15.  The way cluster works is that each Node process becomes either a “master” or a “worker” process. When a master process calls the cluster.fork() method, it creates a child process that is identical to the master, except for two attributes that each process can check to see whether it is a master or child. In the master process, cluster.isMaster returns true, whereas cluster.isWorker returns false.

 

16.  When you listen() to a socket where cluster is in use, many processes can listen to the same socket. cluster provides a cross-platform way to invoke several processes that share a socket. And even when the children all share a connection to a port, if one of them is jammed, it doesn’t stop the other workers from getting connections.

 

17.  cluster is based on the child_process module. This gives us a number of attributes, and some of the most useful ones relate to the health of the child processes. 

 

18. Monitoring worker health and killing zombie workers using message passing:

 

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 {
  //Server
  http.Server(function(req,res) {
    //mess up 1 in 200 reqs
    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)
  //Report stats once a second
  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)
  //allow boot time
  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.')
      }
    }
  })
}

 

 

 

 

分享到:
评论

相关推荐

    [removed] Moving to ES2015 (AZW3格式)

    This also goes for those who want to explore some modern JavaScript features, techniques, and architectures in order to develop cutting-edge web applications. What You Will Learn Get a run through ...

    Mastering.TypeScript.2nd.epub

    It's simple typing syntax enables building large, robust applications using object-oriented techniques and industry standard design principles. Packed with practical, real-world examples, this book ...

    MEAN.Web.Development.2nd.Edition.epub

    The MEAN stack is a collection of the most popular modern tools for web development that helps you build fast, robust, and maintainable web applications. Starting with the MEAN core frameworks, this ...

    JavaScript Applications with Node.js, React, React Native and MongoDB

    JavaScript Applications with Node.js, React, React Native and MongoDB: Design, code, test, deploy and manage in Amazon AWS By 作者: Eric Bush ISBN-10 书号: 0997196661 ISBN-13 书号: 9780997196665 出版...

    The way to go

    Chapter 3—Editors, IDE’s and Other tools.........................................................................28 3.1 Basic requirements for a decent Go development environment......................

    [Go语言入门(含源码)] The Way to Go (with source code)

    Chapter 3—Editors, IDE’s and Other tools.........................................................................28 3.1 Basic requirements for a decent Go development environment......................

    up and running with nodejs

    6. **Chapter 3: Building Robust Node Applications**:深入探讨如何构建健壮、可维护的Node.js应用程序。 - [http://ofps.oreilly.com/titles/9781449398583/chap_3.html]...

Global site tag (gtag.js) - Google Analytics