`
leonzhx
  • 浏览: 766737 次
  • 性别: 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.')
      }
    }
  })
}

 

 

 

 

分享到:
评论

相关推荐

    iron-clad.java.building.secure.web.applications

    Proven Methods for Building Secure Java-Based Web Applications Develop, deploy, and maintain secure Java applications using the expert techniques and open source libraries described in this Oracle ...

    Java 9 Building Robust Modular Applications_Code 源码

    Java 9 Building Robust Modular Applications_Code 源码 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    Bioinformatics.Data.Skills.Reproducible.and.Robust.Research.pdf

    Chapter 3. Remedial Unix Shell Chapter 4. Working with Remote Machines Chapter 5. Git for Scientists Chapter 6. Bioinformatics Data Part III. Practice: Bioinformatics Data Skills Chapter 7. Unix Data...

    Java 9_Building Robust Modular Applications-Packt Publishing(2018)

    Java 9 and its new features add to the richness of the language--one of the most-used languages to build robust software applications. Java 9 comes with a special emphasis on modularity. Some of the ...

    Robust Optimization - Princeton University Press

    C.3 Chapter 3 513 C.4 Chapter 4 513 C.5 Chapter 5 516 C.6 Chapter 6 519 C.7 Chapter 7 520 C.8 Chapter 8 521 C.9 Chapter 9 523 C.10 Chapter 12 525 C.11 Chapter ...

    Pro Node.js for Developers

    Building upon your existing JavaScript skills you’ll be shown how to use Node.js to build both Web- and Network-based applications, to deal with data sources, capture events and deal with child ...

    [removed] Moving to ES2015 (AZW3格式)

    About This Book This course offers an expert's eye on the latest ES6 features and how these advanced tasks fit together in JavaScript as a whole ... Securing And Scaling Node.Js Applications

    PHP.Web.Services.APIs.for.the.Modern.Web.2nd.Edition

    Whether you’re sharing data... real-world examples, and advice based on her extensive experience to guide you through the process—from the underlying theory to methods for making your service robust. ...

    [Node.js] Node.js the Right Way (英文版)

    Get to the forefront of server-side JavaScript programming by writing compact, robust, fast, networked Node applications that scale. Ready to take JavaScript beyond the browser, explore dynamic ...

    Twisted Network Programming Essentials 2nd Edition

    Chapter 3. Writing Asynchronous Code with Deferreds Chapter 4. Web Servers Chapter 5. Web Clients Part II: Building Production-Grade Twisted Services Chapter 6. Deploying Twisted Applications ...

    Web.Development.with.Go.Building.Scalable.Web.Apps.and.RESTful.Services

    Go, the open-source programming language originally developed at Google, makes it easy to build simple, reliable, and efficient software....Chapter 11: Building Go Web Applications on Google Cloud

    AWS.System.Administration

    Chapter 3. Access Management and Security Groups Chapter 4. Configuration Management Chapter . An Example Application Stack Chapter 6. Auto Scaling and Elastic Load Balancing Chapter 7. Deployment ...

    HTML5.and.CSS3.Building.Responsive.Websites.pdf

    Chapter 3: Constructing a Simple Responsive Blog with Responsive.gs Chapter 4: Enhancing the Blog Appearance Chapter 5: Developing a Portfolio Website with Bootstrap Chapter 6: Polishing the ...

    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 ...

    Big.Data.in.Complex.Systems.Challenges.and.Opportunities.3319

    Chapter 3. Towards Robust Performance Guarantees forModels Learned from High-Dimensional Data Chapter 4. Stream Clustering Algorithms:A Primer Chapter 5. Cross Language Duplicate Record Detectionin ...

    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 ...

    Learning.Docker.1784397938

    Chapter 3. Building Images Chapter 4. Publishing Images Chapter 5. Running Your Private Docker Infrastructure Chapter 6. Running Services in a Container Chapter 7. Sharing Data with Containers Chapter...

    Benning.Game.Programming.Patterns

    Chapter 3. Flyweight Chapter 4. Observer Chapter 5. Prototype Chapter 6. Singleton Chapter 7. State Part 3. Sequencing Patterns Chapter 8. Double Buffer Chapter 9. Game Loop Chapter 10. Update Method...

    Developing.an.Angular.2.Edge.epub

    Chapter 3. Introducing Our App Chapter 4. Modules Chapter 5. Components Chapter 6. Directives Chapter 7. Services Chapter 8. Pipes Chapter 9. Routing Chapter 10. Forms Chapter 11. Change Detection ...

Global site tag (gtag.js) - Google Analytics