Node js


Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Node js

  1. 1. Asynchronous I/O in NodeJS- Standard and challenge of web programming? Pham Cong Dinh @ SkunkWorks @pcdinh BarCamp Saigon 2011 Follow us on Twitter: @teamskunkworks
  2. 2. Notice● It is not a comprehensive study● Things can be changed● No standard at the moment● Unix only
  3. 3. Proven control flow models● Single-threaded process model ● Prefork MPM Apache HTTPd● Multi-threaded process model ● Worker MPM Apache HTTPd ● JVM
  4. 4. Emerging control flow models● CoroutinesCoroutines are computer program components that generalize subroutines toallow multiple entry points for suspending and resuming execution at certainlocations.● FiberA lightweight thread of execution. Like threads, fibers share address space.However, fibers use co-operative multitasking while threads use pre-emptivemultitasking.● Events: non-blocking I/O
  5. 5. Events / Non-blocking I/O From
  6. 6. Request handling: Process / Threads / Events● Process: A single process is forked per request. That process is blocked until response can be produced● Threads: A process contains multiple threads. A single thread is assigned to handle an incoming request. Thread-per-request model● Events: A process consists of the processing of a series of events. At any instant in time, a single event/request is being processed. The process is not blocked on I/O request.
  7. 7. Request handling: Process / Threads / EventsThreads shares● Default share memory ● File descriptors● Filesystem context ● Signals and Signal handling
  8. 8. Request handling:Process / Threads / Events Thread creation is expensive - From
  9. 9. Request handling:Process / Threads / Events Context switching is expensive
  10. 10. Request handling:Process / Threads / Events Blocking model: Process and Threads
  11. 11. Request handling: Process / Threads / EventsNon-blocking model: Events
  12. 12. Request handling:Event-driven IO loop issue poll vs. epoll epoll: O(1) or O(n=active)
  13. 13. Request handling: Event-driven callback issuesNon-blocking IO Loop vs. Blocking callbackEvent dispatching is not blockedCallback can be blockedMixing asynchronous code and synchronous code can be bad
  14. 14. Events in NodeJS● libev for event loops● libeio for asynchonous file I/O● c-ares for asynchronous DNS requests and name resolution.● evcom (by Ryan Dahl) is a stream socket library on top of libev.
  15. 15. Asynchronous programming model in NodeJS● First citizen: High order function/callback● Most “objects” in NodeJS are Event Emitters (http server/client, etc.)● Most low level “functions” take callbacks. (posix API, DNS lookups, etc.)Blocking codevar a = db.query(SELECT A);console.log(result a:, a);Non-blocking code using callbackdb.query(SELECT A, function(result) { console.log(result a:, result);});
  16. 16. Asynchronous programming model in NodeJS● Callbacks is hard ● Divides things into stages and handle each stage in a a callback ● Do things in a specific order. ● You must keep track of what is done at a point of time ● Hard to handle failures ● Nested callbacks can be hard to read
  17. 17. Asynchronous programming model in NodeJS ● Nested callbacks can be hard to readvar transferFile = function (request, response) { var uri = url.parse(request.url).pathname; var filepath = path.join(process.cwd(), uri); // check whether the file is exist and get the result from callback path.exists(filepath, function (exists) { if (!exists) { response.writeHead(404, {"Content-Type": "text/plain"}); response.write("404 Not Foundn"); response.end(); } else { // read the file content and get the result from callback fs.readFile(filepath, "binary", function (error, data) { if (error) { response.writeHead(500, {"Content-Type": "text/plain"}); response.write(error + "n"); } else { response.writeHead(200); response.write(data, "binary"); } response.end(); }); } });}
  18. 18. Asynchronous programming model in NodeJS ● Callback is hard to debugfunction f () { throw new Error(’foo’);}setTimeout(f, 10000*Math.random());setTimeout(f, 10000*Math.random());From which line does the error arise?
  19. 19. Asynchronous programming model in NodeJS Flow Control Libraries● Steps● Flow-JS● Node-Promise
  20. 20. Asynchronous programming model in NodeJS Flow Control Libraries: Steps● Steps goal is to both remove boilerplate code and to improve readability of asynchronous code. The features are easy chaining of serial actions with optional parallel groups within each step.Step( function readSelf() { fs.readFile(__filename, this); }, function capitalize(err, text) { if (err) throw err; return text.toUpperCase(); }, function showIt(err, newText) { if (err) throw err; console.log(newText); });
  21. 21. Asynchronous programming model in NodeJS Flow Control Libraries: Flow-JS● Flow-JS provides a Javascript construct that is something like a continuation or a fiber found in other languages. Practically speaking, it can be used to eliminate so-called "pyramids" from your multi-step asynchronous logic.dbGet(userIdOf:bobvance, function(userId) { dbSet(user: + userId + :email, bobvance@potato.egg, function() { dbSet(user: + userId + :firstName, Bob, function() { dbSet(user: + userId + :lastName, Vance, function() { okWeAreDone(); }); }); });}); flow.exec( function() { dbGet(userIdOf:bobvance, this); },function(userId) { dbSet(user: + userId + :email, bobvance@potato.egg, this.MULTI()); dbSet(user: + userId + :firstName, Bob, this.MULTI()); dbSet(user: + userId + :lastName, Vance, this.MULTI()); },function() { okWeAreDone() } );
  22. 22. Asynchronous programming model in NodeJS JavaScript extension: TameJS● Tame (or "TameJs") is an extension to JavaScript, written in JavaScript, that makes event programming easier to write, read, and edit when control-flow libraries are not good enough!.●
  23. 23. Asynchronous programming model in NodeJS JavaScript extension: TameJS ● Synchronous codehandleVisit : function(angel, buffy) { var match_score = getScore(angel, buffy); var next_match = getNextMatch(angel); var visit_info = recordVisitAndGetInfo(angel, buffy); if (match_score > 0.9 && ! visit_info.last_visit) { sendVisitorEmail(angel, buffy); } doSomeFinalThings(match_score, next_match, visit_info);} ● Asynchrnous codehandleVisit : function(angel, buffy) { getScore(angel, buffy, function(match_score) { getNextMatch(angel, function(next_match) { recordVisitAndGetInfo(angel, buffy, function(visit_info) { if (match_score > 0.9 && ! visit_info.last_visit) { sendVisitorEmail(angel, buffy); } doSomeFinalThings(match_score, next_match, visit_info); }); }); });}
  24. 24. Asynchronous programming model in NodeJS JavaScript extension: TameJS ● TameJS stylehandleVisit : function(angel, buffy) { // // lets fire all 3 at once // await { getScore (angel, buffy, defer(var score)); getNextMatch (angel, buffy, defer(var next)); recordVisitAndGetInfo (angel, buffy, defer(var vinfo)); } // // theyve called back, and now we have our data // if (score > 0.9 && ! vinfo.last_visit) { sendVisitorEmail(angel, buffy); } doSomeFinalThings(score, next, vinfo);}
  25. 25. Asynchronous programming model in NodeJS JavaScripts yield● V8/NodeJS has not supported yield, so generator yet
  26. 26. The end● Q&A