The closest thing to documentation of the implementation is the
implementation itself, but as that's scattered across a whole bunch of
source files, it's not too useful.
Here are some semi-organized notes in lieu of actual documentation:
- There are no modifications to V8 for domains. src/node.cc was modified
in Node 0.10 to have a new global exception-handler that the domain system
can hook into, which replaces process.on('uncaughtException'). Node 0.12
will have some further changes to the C++ side of Node (mostly related to
the new, still-private asyncListener API), but even still, they don't reach
into the V8 codebase. Node just consumes the V8 embedding API; with some
very rare exceptions, it doesn't touch V8's internals at all.
- The changes to Node are primarily in two categories:
1. To ensure the domain is properly propagated across asynchronous
call stacks.
2. To ensure that EventEmitters created and run inside domains are
attached to the correct domain.
- The original implementation of domains, from Node 0.8, was built atop
process.on('uncaughtException'). Trevor Norris reworked it fairly
heavily for Node 0.10, making changes all the way down to the C++ level, in
the interest of reducing the overhead imposed by domains. In particular, he
bifurcated the code paths used for the Node event loop so there are
separate versions for when domains aren't / are in use. Requiring the
domains module switches between the two paths, essentially making domains
free when you're not using them.
- Even with Trevor's changes, the vast bulk of the domain API is
implemented in pure JavaScript, in src/node.js, lib/domain.js, and
lib/events.js.
- The basic mechanism is pretty simple: take the function provided to
d.run() or d.bind(), wrap it in a closure, make calls to d.enter() and
d.exit() before and after running the function when the closure is
invoked. All the other mechanisms are there to ensure that the correct
domain is available across asynchronous boundaries in case of errors.
- The trickiest bits to understand are all of the implicit bindings that
happen around EventEmitters / Sockets / HTTP requests and responses and
domains. It's convenient in that you don't have to remember to add all
these EEs to the domain, but there's some subtle stuff going on.
- Probably the most counterintuitive part of domains' implementation is
the way nested domains interact -- if you enter a domain from inside a
domain, and then exit the containing domain, the contained domain will be
exited as well. This is similar to how try / catch / finally deals with the
call stack in the synchronous case, but it may not be what you expect.
The rest of the details can be figured out from reading the source
(although, especially in 0.10, it may take a few readthroughs before
everything starts to come together). I've suggested writing some design
documents to capture some of these details, and there was general agreement
that that would be useful, but somebody's gotta find the time to write all
this stuff down, and nobody's done it (yet).
Hope this is helpful!
F
On Sun, Mar 30, 2014 at 8:27 PM, ming <[email protected]> wrote:
> Hi,
> Do you know of any documentation(s) about how domains are implemented in
> Node.js? In particular, what changes were introduced in Node.js and the
> underlying Google V8 engine (if any) for the implementation of domains?
>
> Thanks.
>
>
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.