• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

How to stop writing spaghetti code

on

  • 14,826 views

 

Statistics

Views

Total Views
14,826
Views on SlideShare
14,523
Embed Views
303

Actions

Likes
17
Downloads
227
Comments
1

8 Embeds 303

http://speakerrate.com 159
http://code.danyork.com 113
http://lanyrd.com 24
http://115.112.206.131 2
http://www.advogato.org 2
http://alikindoy.wordpress.com 1
http://www.techgig.com 1
http://www.linkedin.com 1
More...

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • nice work...some more reference to understand event driven I/O with metaphors at http://code.danyork.com/2011/01/25/node-js-doctors-offices-and-fast-food-restaurants-understanding-event-driven-programming/
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

How to stop writing spaghetti code How to stop writing spaghetti code Presentation Transcript

  • Ya ho o! In c
  • What is the event loop?
  • Multi-tasking, one thing at a time.
  • var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/');
  • var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 1. Evaluate 'Main'
  • var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 1. variables: http -> http module server -> http server listeners: server.request -> function
  • var http = require('http'); server = http.createServer(); * server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 2. Event Loop
  • var http = require('http'); server = http.createServer(); * server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 2. Do we have active listeners? listeners: server.request -> function Yes! Wait for listeners.
  • var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 3. Event Calls
  • var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 3. 'request' is called. Since listeners: server.request -> function Call function
  • var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 3. Loop! (go to Step 2.)
  • Breaking the event loop
  • EE = require('events').EventEmitter; ee = new EE(); die = false; ee.on('die', function() { die = true; }); setTimeout(function() { ee.emit('die'); }, 100); while(!die) { } console.log('done');
  • Why non-blocking matters
  • var result = db.query("select * from T"); // use result
  • What are we waiting for?
  • "Blocking" is as bad as stopping
  • Event Loop vs. Threads
  • Apache vs NGINX concurrency × reqs/sec http://blog.webfaction.com/a-little-holiday-present
  • Apache vs NGINX concurrency × memory http://blog.webfaction.com/a-little-holiday-present
  • The less you block the more I/O Node can do
  • Programming Style
  • What kinds of applications do we build?
  • Applications • Command line applications • Web servers • Streaming servers • Peer-to-peer
  • Generalising • 'Main' scripts • Request/Response driven • Streaming
  • 'Main' Scripts • Simple JavaScript • Doesn't use anything blocking • Doesn't use callbacks • Almost certainly a simple CLI script
  • Request/Response Driven • Discrete request/response cycle • Encapsulated requests/responses
  • Streaming • Arbitrary IN/OUT data • Stream data may not be encapsulated
  • var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'
  • http.createServer(function (req, res) { })
  • //Server CommonJS Module Skeleton var sys = require('sys'), EE = require('events').EventEmitter; var Server = exports.Server = function() { //Server Setup }; sys.inherits(Server, EE); Server.prototype.Request = function() { //Request Setup }; sys.inherits(Sever.prototype.Request, EE); Server.prototype._createRequest = function(req, res) { //Request Factory return new this.Request(); }; exports.createServer = function() { //Server Factory return new Server(); };
  • var server = Server.createServer(); server.on('request', server._createReq);
  • Step 1. Create new Server
  • Step 2. Listen for requests
  • Step 3. On request create new Request
  • Step 4. Encapsulate request state in request object
  • Step 5. Request life-cycle
  • Closure Encapsulation DB File Query dbResponse Access Init fileReady Server Request Init Request Respond Process Processed
  • Summary • Event Loop does 1 thing at a time • Until a loop loops events aren't fired • Emitted events run immediately • Define a "server" structure • Use factory methods to encapsulate with named objects • Use events to maximize the event loop