+ All Categories
Home > Technology > Using Common Node to build StartHQ

Using Common Node to build StartHQ

Date post: 13-Jan-2015
Category:
Upload: oleg-podsechin
View: 608 times
Download: 1 times
Share this document with a friend
Description:
A HelsinkiJS talk by Alex from https://starthq.com The video of this talk is available at http://youtu.be/pmyDJnEza6A
15
Server-Side JS @ StartHQ Node, Fibers, Common Node etc.
Transcript
Page 1: Using Common Node to build StartHQ

Server-Side JS @ StartHQNode, Fibers, Common Node etc.

Page 2: Using Common Node to build StartHQ

Fibers

● lightweight threads

● co-operative multitasking○ still using one event loop

● individual stack, shared heap○ require a bit of additional memory for stacks

● similar to generators (in ES6)○ but no need to mark each yielding method

Page 3: Using Common Node to build StartHQ

Fibers usage example

function sleep(ms) { var fiber = Fiber.current; setTimeout(function() { fiber.run(); }, ms); Fiber.yield();}

Page 4: Using Common Node to build StartHQ

Sleep: Async. vs. Sync.

console.log('before');setTimeout(function() { console.log('after 1s');}, 1000);

console.log('before');sleep(1000);console.log('after 1s');

Page 5: Using Common Node to build StartHQ

CommonJS

● Set of specifications○ Modules, Packages, Unit Testing

○ Binary, IO, Filesystem, JSGI, etc.

● Implemented by RingoJS and others○ Ringo runs on top of the JVM

● Node supports the first 3, but not others

because they assume sync. I/O

Page 6: Using Common Node to build StartHQ

Common Node

● Implements CommonJS specs using Fibers

on top of Node

● Allows for code reuse with RingoJS

● Simplifies business logic & debugging○ supports for, exceptions etc. for flow control

● Performance: 80%-120% of Node○ Uses a bit more memory e.g. 28MB vs. 32MB

Page 7: Using Common Node to build StartHQ

Benchmarks

Page 8: Using Common Node to build StartHQ

Node Streams

var body = [];stream.on('data', function (chunk) { body.push(chunk);})stream.on('end', function () { callback(body);});

Page 9: Using Common Node to build StartHQ

Node Streams2

var body = [];stream.on('readable', function () { body.push(stream.read());})stream.on('end', function () { callback(body);});

Page 10: Using Common Node to build StartHQ

Common Node Streams

var body = [], chunk;while(chunk = stream.read(null)) { body.push(chunk);}return body;

OR

return stream.read();

Page 11: Using Common Node to build StartHQ

Ecosystem

● Ringo modules

● Common Utils

● Mongo Sync

● Stick

● Wrapping Node packages

Page 12: Using Common Node to build StartHQ

Common Node at StartHQ

● Use it for everything○ API, static resources

○ background processing

○ command line tools

● Extractors API

● Sentry stack traces

Page 13: Using Common Node to build StartHQ

Pitfalls

● Trying to yield or resume outside fiber

● Using async libs with fibers, such as Express

● Writing to global variables

● Separate fiber instance per dependency

● Unreliable third party packages○ Throws into event loop & crashed program

Page 14: Using Common Node to build StartHQ

GitHub Links

● laverdet/node-fibers

● olegp/common-node

● olegp/notes

● starthq/extractor

Page 15: Using Common Node to build StartHQ

Thank you!@alexlamsl


Recommended