Skip to content

Instantly share code, notes, and snippets.

@ofrobots
Created March 26, 2015 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ofrobots/d725e7c2aa5fdafa8a13 to your computer and use it in GitHub Desktop.
Save ofrobots/d725e7c2aa5fdafa8a13 to your computer and use it in GitHub Desktop.
var async_wrap = process.binding('async_wrap');
var uid = 0;
var kCallInitHook = 0;
var asyncHooksObject = {};
function asyncInit() {
this._asyncQueue = { uid: ++uid };
process._rawDebug('init ' + this._asyncQueue.uid);
}
function asyncBefore() {
process._rawDebug('before: ' + this._asyncQueue.uid);
}
function asyncAfter() {
process._rawDebug('after: ' + this._asyncQueue.uid);
}
async_wrap.setupHooks(asyncHooksObject, asyncInit, asyncBefore, asyncAfter);
asyncHooksObject[kCallInitHook] = 1;
var net = require('net');
var s = net.createServer();
s.on('connection', function onConnection(c) {
c.end('Hello World\n');
});
s.listen(8000, function() {
// Don't want to trace the req making the conntion. Only the received
// req by the server.
asyncHooksObject[kCallInitHook] = 0;
// Create a connection for logging, then exit app.
net.connect(8000, function() { });
});
@ofrobots
Copy link
Author

Here's the annotated output

init    1
before: 1       # before hook called because child (2) was created
init    2
after:  1
before: 1            # actual before for event 1
after:  1
before: 2
after:  2
before: 2
after:  2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment