node.js: asynchronous i/o for fun and pro tgotocon.com/dl/qcon-london-2011/slides/stefantilko... ·...
TRANSCRIPT
![Page 1: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/1.jpg)
Node.js:Asynchronous I/O for
Fun and Pro!tStefan Tilkov @ QCon London 2011
Friday, March 11, 2011
![Page 3: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/3.jpg)
Concurrent Request Processing
Friday, March 11, 2011
![Page 4: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/4.jpg)
Friday, March 11, 2011
![Page 5: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/5.jpg)
Friday, March 11, 2011
![Page 6: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/6.jpg)
Friday, March 11, 2011
![Page 7: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/7.jpg)
Friday, March 11, 2011
![Page 8: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/8.jpg)
Friday, March 11, 2011
![Page 9: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/9.jpg)
Friday, March 11, 2011
![Page 10: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/10.jpg)
read request
Friday, March 11, 2011
![Page 11: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/11.jpg)
read requestparse request
Friday, March 11, 2011
![Page 12: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/12.jpg)
read requestparse request
process
Friday, March 11, 2011
![Page 13: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/13.jpg)
read requestparse request
processsend backend request
Friday, March 11, 2011
![Page 14: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/14.jpg)
read requestparse request
processsend backend requestread backend answer
Friday, March 11, 2011
![Page 15: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/15.jpg)
read requestparse request
processsend backend requestread backend answer
process
Friday, March 11, 2011
![Page 16: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/16.jpg)
read requestparse request
processsend backend requestread backend answer
processformat response
Friday, March 11, 2011
![Page 17: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/17.jpg)
read requestparse request
processsend backend requestread backend answer
processformat responsesend response
Friday, March 11, 2011
![Page 18: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/18.jpg)
read requestparse request
processsend backend requestread backend answer
processformat responsesend response
Friday, March 11, 2011
![Page 19: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/19.jpg)
Blocking I/O Problems
Friday, March 11, 2011
![Page 20: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/20.jpg)
Blocking I/O Problems
Thread starvation
Friday, March 11, 2011
![Page 21: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/21.jpg)
Blocking I/O Problems
Thread starvation
Memory utilization
Friday, March 11, 2011
![Page 22: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/22.jpg)
Blocking I/O Problems
Thread starvation
Memory utilization
External dependencies
Friday, March 11, 2011
![Page 23: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/23.jpg)
Blocking I/O Problems
Thread starvation
Memory utilization
External dependencies
Cascading problems
Friday, March 11, 2011
![Page 24: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/24.jpg)
Blocking I/O Problems
Thread starvation
Memory utilization
External dependencies
Cascading problems
Non-streaming approach
Friday, March 11, 2011
![Page 25: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/25.jpg)
Kernel
User Space
Friday, March 11, 2011
![Page 26: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/26.jpg)
Event Loop
while (true) ready_channels = select(io_channels) for (channel in ready_channels) performIO(channel)
Friday, March 11, 2011
![Page 27: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/27.jpg)
Async I/O Characteristics
Friday, March 11, 2011
![Page 28: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/28.jpg)
Async I/O Characteristics
Program always running
Friday, March 11, 2011
![Page 29: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/29.jpg)
Async I/O Characteristics
Program always running
I/O-bound calls never block
Friday, March 11, 2011
![Page 30: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/30.jpg)
Async I/O Characteristics
Program always running
I/O-bound calls never block
Kernel handles I/O
Friday, March 11, 2011
![Page 31: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/31.jpg)
Async I/O Characteristics
Program always running
I/O-bound calls never block
Kernel handles I/O
Noti!cation via events
Friday, March 11, 2011
![Page 32: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/32.jpg)
Async I/O Characteristics
Program always running
I/O-bound calls never block
Kernel handles I/O
Noti!cation via events
Used for timers, !le I/O, net I/O, ...
Friday, March 11, 2011
![Page 33: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/33.jpg)
requests/second
http://blog.webfaction.com/a-little-holiday-present
Friday, March 11, 2011
![Page 34: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/34.jpg)
memory
http://blog.webfaction.com/a-little-holiday-present
Friday, March 11, 2011
![Page 35: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/35.jpg)
select()
poll()
epoll()
kqueue()
/dev/poll
aio_*()
Friday, March 11, 2011
![Page 36: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/36.jpg)
java.nio
.NET I/O Completion Ports
Friday, March 11, 2011
![Page 37: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/37.jpg)
Async I/O Perception
Friday, March 11, 2011
![Page 38: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/38.jpg)
Async I/O Perception
Not widely known
Friday, March 11, 2011
![Page 39: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/39.jpg)
Async I/O Perception
Not widely known
Low level
Friday, March 11, 2011
![Page 40: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/40.jpg)
Async I/O Perception
Not widely known
Low level
Hard to use
Friday, March 11, 2011
![Page 41: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/41.jpg)
Async I/O Perception
Not widely known
Low level
Hard to use
Exception rather than rule
Friday, March 11, 2011
![Page 42: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/42.jpg)
JavaScript
Friday, March 11, 2011
![Page 43: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/43.jpg)
JavaScript Perception
Friday, March 11, 2011
![Page 44: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/44.jpg)
JavaScript Perception
“Toy language”
Friday, March 11, 2011
![Page 45: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/45.jpg)
JavaScript Perception
“Toy language”
Incompatible
Friday, March 11, 2011
![Page 46: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/46.jpg)
JavaScript Perception
“Toy language”
Incompatible
Inherent design problems
Friday, March 11, 2011
![Page 47: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/47.jpg)
JavaScript Perception
“Toy language”
Incompatible
Inherent design problems
Low Performance
Friday, March 11, 2011
![Page 48: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/48.jpg)
http://commons.wikimedia.org/wiki/File:Audi_S5_V8_FSI_engine.jpg
Friday, March 11, 2011
![Page 49: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/49.jpg)
Friday, March 11, 2011
![Page 50: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/50.jpg)
http://commons.wikimedia.org/wiki/File:Ateles_paniscus_-Brazil-8.jpg
Friday, March 11, 2011
![Page 51: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/51.jpg)
Friday, March 11, 2011
![Page 52: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/52.jpg)
The JavaScript Arms Race
Friday, March 11, 2011
![Page 53: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/53.jpg)
Friday, March 11, 2011
![Page 54: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/54.jpg)
Friday, March 11, 2011
![Page 55: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/55.jpg)
http://oreilly.com/catalog/9780596517748
Friday, March 11, 2011
![Page 56: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/56.jpg)
Friday, March 11, 2011
![Page 57: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/57.jpg)
JavaScript Today
Friday, March 11, 2011
![Page 58: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/58.jpg)
JavaScript Today
Popular & widely used
Friday, March 11, 2011
![Page 59: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/59.jpg)
JavaScript Today
Popular & widely used
O"en mandatory
Friday, March 11, 2011
![Page 60: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/60.jpg)
JavaScript Today
Popular & widely used
O"en mandatory
Fast
Friday, March 11, 2011
![Page 61: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/61.jpg)
JavaScript Today
Popular & widely used
O"en mandatory
Fast
Compatible
Friday, March 11, 2011
![Page 62: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/62.jpg)
JavaScript Today
Popular & widely used
O"en mandatory
Fast
Compatible
Best practices
Friday, March 11, 2011
![Page 63: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/63.jpg)
Node.js
Friday, March 11, 2011
![Page 64: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/64.jpg)
libev libeiov8 http_parser c_ares
Node.js Architecture
Friday, March 11, 2011
![Page 65: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/65.jpg)
libev libeiov8 http_parser c_ares
Node.js Architecture
Network/Platform layer (C)
Friday, March 11, 2011
![Page 66: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/66.jpg)
libev libeiov8 http_parser c_ares
Node.js Architecture
Network/Platform layer (C)
API (JavaScript)
Friday, March 11, 2011
![Page 67: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/67.jpg)
High-performance network runtime, using JavaScript as
a high-level DSL
Friday, March 11, 2011
![Page 68: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/68.jpg)
echo.js
var net = require('net');
var server = net.createServer(function (socket) { socket.write("Echo server\r\n"); socket.pipe(socket);})
server.listen(8124, "127.0.0.1");
Code samples: http://github.com/stilkov/node-samples
Friday, March 11, 2011
![Page 69: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/69.jpg)
echo-upcase.js
var net = require('net');
var server = net.createServer(function (socket) { socket.write("Echo server\r\n"); socket.setEncoding('ascii'); socket.on('data', function(data) { socket.write(data.toUpperCase()); });});
server.listen(8124, "127.0.0.1");
Friday, March 11, 2011
![Page 70: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/70.jpg)
var sys = require("sys"), http = require("http"), url = require("url"), path = require("path"), fs = require("fs");
var dir = process.argv[2] || './public';var port = parseFloat(process.argv[3]) || 8080;sys.log('Serving files from ' + dir + ', port is ' + port);
http.createServer(function(request, response) { var uri = url.parse(request.url).pathname; var filename = path.join(process.cwd(), dir, uri); path.exists(filename, function(exists) { if(exists) { fs.readFile(filename, function(err, data) { response.writeHead(200); response.end(data); }); } else { sys.log('File not found: ' + filename); response.writeHead(404); response.end(); } });}).listen(port);
!le-server.jsFriday, March 11, 2011
![Page 71: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/71.jpg)
Concurrency Level: 100Time taken for tests: 6.000 secondsComplete requests: 10000Failed requests: 0Write errors: 0Keep-Alive requests: 0Total transferred: 710781 bytesHTML transferred: 150165 bytesRequests per second: 1666.72 [#/sec] (mean)Time per request: 59.998 [ms] (mean)Time per request: 0.600 [ms] (mean, across all concurrent requests)Transfer rate: 115.69 [Kbytes/sec] received
Connection Times (ms) min mean[+/-sd] median maxConnect: 0 8 8.3 5 57Processing: 1 51 44.4 40 307Waiting: 0 43 43.5 30 302Total: 1 59 44.8 50 316
Percentage of the requests served within a certain time (ms) 50% 50 66% 58 75% 68 80% 73 90% 112 95% 174 98% 206 99% 224 100% 316 (longest request)
Friday, March 11, 2011
![Page 72: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/72.jpg)
!le-server-md5.js
http.createServer(function(request, response) { var uri = url.parse(request.url).pathname; var filename = path.join(process.cwd(), dir, uri); sys.log('Serving file ' + filename); path.exists(filename, function(exists) { if(exists) { fs.readFile(filename, function(err, data) { var hash = crypto.createHash('md5'); hash.update(data); response.writeHead(200, { 'Content-Type': 'text/plain', 'Content-MD5': hash.digest('base64') } ); response.end(data); }); } else { response.writeHead(404); response.end(); } });}).listen(port);
Friday, March 11, 2011
![Page 73: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/73.jpg)
HTTP Chunking
Friday, March 11, 2011
![Page 74: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/74.jpg)
HTTP/1.0
Client Server
connect
Friday, March 11, 2011
![Page 75: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/75.jpg)
HTTP/1.0
Client Server
connectsend request
Friday, March 11, 2011
![Page 76: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/76.jpg)
HTTP/1.0
Client Server
connectsend request
send response
Friday, March 11, 2011
![Page 77: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/77.jpg)
HTTP/1.0
Client Server
connect
close connection
send requestsend response
Friday, March 11, 2011
![Page 78: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/78.jpg)
HTTP/1.0
Client Serverconnect
connect
close connection
send requestsend response
Friday, March 11, 2011
![Page 79: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/79.jpg)
HTTP/1.0
Client Serverconnectsend request
connect
close connection
send requestsend response
Friday, March 11, 2011
![Page 80: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/80.jpg)
HTTP/1.0
Client Serverconnectsend request
send response data
connect
close connection
send requestsend response
Friday, March 11, 2011
![Page 81: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/81.jpg)
HTTP/1.0
Client Serverconnectsend request
send response data...
connect
close connection
send requestsend response
Friday, March 11, 2011
![Page 82: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/82.jpg)
HTTP/1.0
Client Serverconnectsend request
send response data
send response data...
connect
close connection
send requestsend response
Friday, March 11, 2011
![Page 83: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/83.jpg)
HTTP/1.0
Client Serverconnectsend request
send response data
send response dataclose connection
...
connect
close connection
send requestsend response
Friday, March 11, 2011
![Page 84: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/84.jpg)
HTTP/1.1: Content-length
Client Server
Friday, March 11, 2011
![Page 85: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/85.jpg)
HTTP/1.1: Content-length
Client Server
connect
Friday, March 11, 2011
![Page 86: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/86.jpg)
HTTP/1.1: Content-length
Client Server
connectConnection: keep-alive
Friday, March 11, 2011
![Page 87: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/87.jpg)
HTTP/1.1: Content-length
Client Server
connect
send request
Connection: keep-alive
Friday, March 11, 2011
![Page 88: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/88.jpg)
HTTP/1.1: Content-length
Client Server
connect
send requestsend response
Connection: keep-alive
Friday, March 11, 2011
![Page 89: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/89.jpg)
Content-length: xxx
HTTP/1.1: Content-length
Client Server
connect
send requestsend response
Connection: keep-alive
Friday, March 11, 2011
![Page 90: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/90.jpg)
...Content-length: xxx
HTTP/1.1: Content-length
Client Server
connect
send requestsend response
Connection: keep-alive
Friday, March 11, 2011
![Page 91: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/91.jpg)
...Content-length: xxx
HTTP/1.1: Content-length
Client Server
connect
send requestsend response
send request
Connection: keep-alive
Friday, March 11, 2011
![Page 92: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/92.jpg)
...Content-length: xxx
HTTP/1.1: Content-length
Client Server
connect
send requestsend response
send responsesend request
Connection: keep-alive
Friday, March 11, 2011
![Page 93: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/93.jpg)
...Content-length: xxx
Content-length: yyy
HTTP/1.1: Content-length
Client Server
connect
send requestsend response
send responsesend request
Connection: keep-alive
Friday, March 11, 2011
![Page 94: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/94.jpg)
...Content-length: xxx
Content-length: yyy
HTTP/1.1: Content-length
Client Server
connect
send requestsend response
send response
close connection
send request
Connection: keep-alive
Friday, March 11, 2011
![Page 95: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/95.jpg)
HTTP/1.1: Transfer-encoding: chunked
Client Server
Friday, March 11, 2011
![Page 96: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/96.jpg)
HTTP/1.1: Transfer-encoding: chunked
Client Server
connectConnection: keep-alive
Friday, March 11, 2011
![Page 97: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/97.jpg)
HTTP/1.1: Transfer-encoding: chunked
Client Server
send request
connectConnection: keep-alive
Friday, March 11, 2011
![Page 98: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/98.jpg)
HTTP/1.1: Transfer-encoding: chunked
Client Server
send requestsend response data
connectConnection: keep-alive
Friday, March 11, 2011
![Page 99: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/99.jpg)
HTTP/1.1: Transfer-encoding: chunked
Client Server
send requestsend response data
connectConnection: keep-alive
Transfer-encoding: chunked
Friday, March 11, 2011
![Page 100: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/100.jpg)
HTTP/1.1: Transfer-encoding: chunked
xxx↵[data]Client Server
send requestsend response data
connectConnection: keep-alive
Transfer-encoding: chunked
Friday, March 11, 2011
![Page 101: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/101.jpg)
HTTP/1.1: Transfer-encoding: chunked
xxx↵[data]Client Server
send requestsend response data
connectConnection: keep-alive
xxx↵[data]send response data
Transfer-encoding: chunked
Friday, March 11, 2011
![Page 102: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/102.jpg)
HTTP/1.1: Transfer-encoding: chunked
xxx↵[data]Client Server
send requestsend response data
connectConnection: keep-alive
xxx↵[data]send response data
0↵send response data
Transfer-encoding: chunked
Friday, March 11, 2011
![Page 103: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/103.jpg)
HTTP/1.1: Transfer-encoding: chunked
xxx↵[data]Client Server
send requestsend response data
close connection
connectConnection: keep-alive
xxx↵[data]send response data
0↵send response data
Transfer-encoding: chunked
Friday, March 11, 2011
![Page 104: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/104.jpg)
stream-!le-server.js
http.createServer(function(request, response) { var uri = url.parse(request.url).pathname; var filename = path.join(process.cwd(), dir, uri); path.exists(filename, function(exists) { if(exists) { f = fs.createReadStream(filename); f.on('open', function() { response.writeHead(200); }); f.on('data', function(chunk) { response.write(chunk); });
f.on('error', function(err) { // ... }); f.on('end', function() { response.end(); }); } else { response.writeHead(404);- response.end(); } });}).listen(port);
Friday, March 11, 2011
![Page 105: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/105.jpg)
hash-!le-stream.js (see stream-!le-server-md5.js)
var hashFile = function(filename, cb) { path.exists(filename, function(exists) { if(exists) { r = fs.createReadStream(filename); var hash = crypto.createHash('md5'); r.on('data', function(data) { hash.update(data); }); r.on('end', function() { cb(hash.digest('base64')); }); } else { throw 'File ' + filename + ' does not exist or can not be read'; } });}
var filename = path.join(process.argv[2]);hashFile(filename, function(hash) { console.log(filename + ': ' + hash);});
Friday, March 11, 2011
![Page 106: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/106.jpg)
proxy.js
var options = function(request) { // ...}
http.createServer(function(request, response) { sys.log("--> " + request.url); var remoteRequest = http.request(options(request), function (remoteResponse) { response.writeHead(remoteResponse.statusCode, remoteResponse.headers); remoteResponse.on('data', function (chunk) { response.write(chunk); }); remoteResponse.on('end', function () { sys.log("<-- " + response.statusCode + " " + request.url); response.end(); }); }); request.on('data', function (chunk) { remoteRequest.write(chunk); }); request.on('end', function () { remoteRequest.end(); });}).listen(port);
Friday, March 11, 2011
![Page 107: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/107.jpg)
proxy-pump.js
http.createServer(function(request, response) { sys.log("--> " + request.url); var remoteRequest = http.request(options(request), function (remoteResponse) { response.writeHead(remoteResponse.statusCode, remoteResponse.headers); remoteResponse.on('end', function () { sys.log("<-- " + response.statusCode + " " + request.url); }); util.pump(remoteResponse, response); }); util.pump(request, remoteRequest);}).listen(port);
Friday, March 11, 2011
![Page 108: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/108.jpg)
AsynchronousProgramming
Challenges
Friday, March 11, 2011
![Page 109: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/109.jpg)
or:Why Programming
with Callbacks Sucks
Friday, March 11, 2011
![Page 110: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/110.jpg)
Friday, March 11, 2011
![Page 111: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/111.jpg)
var bold = function(text) { return text.bold();};
var capitalize = function(text) { return text.toUpperCase();};
console.log("Synchronous:");var result1 = capitalize("Hello, synchronous world.");var result2 = bold(result1);console.log("Sync result is " + result2);
async1.jsFriday, March 11, 2011
![Page 112: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/112.jpg)
var boldAsync = function(text, callback) { setTimeout(function (text) { callback(text.bold()); }, 100, text);};
var capitalizeAsync = function(text, callback) { setTimeout(function (text) { callback(text.toUpperCase()); }, 100, text);};
async1.jsFriday, March 11, 2011
![Page 113: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/113.jpg)
var boldAsync = function(text, callback) { setTimeout(function (text) { callback(text.bold()); }, 100, text);};
var capitalizeAsync = function(text, callback) { setTimeout(function (text) { callback(text.toUpperCase()); }, 100, text);};
async1.js
console.log("Asynchronous:");capitalizeAsync("Hello, asynchronous world.", function(result1) { boldAsync(result1, function(result2) { console.log("Async result is " + result2); });});
Friday, March 11, 2011
![Page 114: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/114.jpg)
async2.js
try { console.log("Synchronous:"); var result1 = capitalize(null); var result2 = bold(result1); console.log("Sync result is " + result2);} catch (exception) { console.log("Sync exception caught: " + exception);}
Friday, March 11, 2011
![Page 115: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/115.jpg)
async2.js
try { console.log("Asynchronous:"); capitalizeAsync(text, function(result1) { boldAsync(result1, function(result2) { console.log("Async result is " + result2); }); });} catch (exception) { console.log("Async exception caught: " + exception);}
Friday, March 11, 2011
![Page 116: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/116.jpg)
async2.js
try { console.log("Asynchronous:"); capitalizeAsync(text, function(result1) { boldAsync(result1, function(result2) { console.log("Async result is " + result2); }); });} catch (exception) { console.log("Async exception caught: " + exception);}
// bad, don't do this
Friday, March 11, 2011
![Page 117: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/117.jpg)
async3.js
var boldAsync = function(text, callback) { setTimeout(function (text) { try { callback(null, text.bold()); } catch (exception) { callback(exception); } }, 100, text);};
var capitalizeAsync = function(text, callback) { setTimeout(function (text) { try { callback(null, text.toUpperCase()); } catch (exception) { callback(exception); } }, 100, text);};
Friday, March 11, 2011
![Page 118: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/118.jpg)
async3.js
capitalizeAsync(text, function(err, result1) { if (!err) { boldAsync(result1, function(err, result2) { if (!err) { console.log("Async result is " + result2); } else { console.log("Handling async error: " + err); } }); } else { console.log("Handling async error: " + err); }});
Friday, March 11, 2011
![Page 119: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/119.jpg)
async3.js
var handleError = function(err, fn) { if (err) { console.log("Handling async error: " + err); } else { fn(); }}
capitalizeAsync(text, function(err, result1) { handleError(err, function () { boldAsync(result1, function(err, result2) { handleError(err, function () { console.log("Async result is " + result2); }); }); });});
Friday, March 11, 2011
![Page 120: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/120.jpg)
async3.js
var step = require("step");step( function () { capitalizeAsync(text, this); }, function (err, result) { if (err) throw err; boldAsync(result, this); }, function(err, result) { if (err) { console.log("Handling async error: " + err); } else { console.log("Async result is " + result); } });
Friday, March 11, 2011
![Page 121: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/121.jpg)
parallel1.js
var words = ['one', 'two', 'three', 'four', 'five'];var upcasedWords = [];
words.forEach(function (word) { capitalize(word, function(err, word) { upcasedWords.push(word); });});console.log('Done, upcased words: <' + upcasedWords.join(' ') + '>');
Friday, March 11, 2011
![Page 122: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/122.jpg)
parallel1.js
var words = ['one', 'two', 'three', 'four', 'five'];var upcasedWords = [];
words.forEach(function (word) { capitalize(word, function(err, word) { upcasedWords.push(word); });});console.log('Done, upcased words: <' + upcasedWords.join(' ') + '>');
// bad, don't do this
Friday, March 11, 2011
![Page 123: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/123.jpg)
parallel1.js
var count = words.length;words.forEach(function (word) { capitalize(word, function(err, word) { upcasedWords.push(word); if (--count === 0) { console.log('Done, upcased words: <' + upcasedWords.join(' ') + '>'); } });});
Friday, March 11, 2011
![Page 124: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/124.jpg)
parallel2.js
var words = ['one', 'two', 'three', 'four', 'five'];
step( function () { var i, length; for (i = 0, length = words.length; i < length; i++) { capitalize(words[i], this.parallel()); } },
function (err) { if (err) throw err; var upcasedWords = Array.prototype.slice.call(arguments); upcasedWords.shift(); console.log('Done, upcased words: <' + upcasedWords.join(' ') + '>'); });
Friday, March 11, 2011
![Page 125: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/125.jpg)
Tools & Ecosystem
Friday, March 11, 2011
![Page 126: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/126.jpg)
Friday, March 11, 2011
![Page 127: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/127.jpg)
npm node package manager
Connect Asynchronous, low-level HTTP handler framework inspired by Rack/WSGI
Express Sinatra-inspired Web framework on top of Connect
multi-node Spawns child processes sharing listeners
node-inspector Visual debugger for Node.js
>700 more modules see https://github.com/joyent/node/wiki/modules
Friday, March 11, 2011
![Page 128: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/128.jpg)
multi-!le-server.js
var multi = require("multi-node");
var server = http.createServer(function(request, response) { var uri = url.parse(request.url).pathname; var filename = path.join(process.cwd(), dir, uri); path.exists(filename, function(exists) { if(exists) { fs.readFile(filename, function(err, data) { if (err) { sys.log('Error serving file ' + filename + ' ' + err); sys.log('request: ' + uri); } response.writeHead(200, { 'X-Node-Id': process.pid }); response.end(data); }); } else { response.writeHead(404); response.end(); } });});
var nodes = multi.listen({ port: port, nodes: 10 }, server); sys.log("Server " + process.pid + " running at http://localhost:" + port);
Friday, March 11, 2011
![Page 129: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/129.jpg)
Summary
Friday, March 11, 2011
![Page 130: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/130.jpg)
Node.js popularizesthe “right way”
of network programming
Friday, March 11, 2011
![Page 131: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/131.jpg)
JavaScript doesn’t suck as much as you think
Friday, March 11, 2011
![Page 132: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/132.jpg)
There’s a smart and active community
Friday, March 11, 2011
![Page 133: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/133.jpg)
Node.js is fun to use!
Friday, March 11, 2011
![Page 134: Node.js: Asynchronous I/O for Fun and Pro tgotocon.com/dl/qcon-london-2011/slides/StefanTilko... · Node.js Friday, March 11, 2011. v8 libev libeio http_parser c_ares Node.js Architecture](https://reader034.vdocuments.site/reader034/viewer/2022050214/5f6057286d792251cd220c4a/html5/thumbnails/134.jpg)
Thank you!
Q&A
Friday, March 11, 2011