Skip to content

Instantly share code, notes, and snippets.

@listochkin
Last active December 16, 2015 03:09
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 listochkin/5368240 to your computer and use it in GitHub Desktop.
Save listochkin/5368240 to your computer and use it in GitHub Desktop.
Forrest L Norvell describes Domains in Node
02:15:46 (listochkin) how do I know if my current callback is running in a domain?
02:16:17 (Remy) listochkin: can you clarify ?
02:19:35 (listochkin) Remy: I mean, suppose I have a module A that does something potentially dangerous. I want to wrap a function in that module in a domain dA. Now suppose I require that module A from module B. And inside that module I've already created a domain dB. What I want is to use dB inside of A instead of creating an extra domain dA since my code is now protected by that external domain. Is it possible and is it a good idea?
02:25:24 (ChrisPartridge) listochkin: then don't put the domain in module A, just have it in module B
02:27:07 (listochkin) ChrisPartridge: what if A is my module and B is someone else's app?
02:33:48 (listochkin) Remy, ChrisPartridge: so, guys any ideas about my nested domains question?
02:33:51 (ChrisPartridge) listochkin: in that case I'm not too sure, I think domains should be used in the application context, not so much in every module - I could be wrong
02:34:47 (listochkin) ChrisPartridge: ok then, perhaps, a note in a docs with a domain usage sample would work fine. Thank you!
02:35:18 (ChrisPartridge) listochkin: keep in mind, I've only played with domains - maybe get one of the node guys opinions before committing to memory :)
02:37:36 (listochkin) ChrisPartridge: yeah, probably. I tried googling through NodeJS mailing list but with no luck. I'm pretty sure I'm not the first person to ask that question. But I looked at Node-MySQL by felixge and he doesn't use domains in it.
02:40:31 (s5fs) listochkin: if you're looking for examples on how to use domains, i see there's a video up on nodetuts
02:40:48 (s5fs) listochkin: i personally know nothing of domains (yet) but am happy about that video haha
02:42:10 (listochkin) s5fs: thanks, I'll take a look, I liked the example on API page
02:49:42 (s5fs) listochkin: best of luck. videos rarely work for me, I go into "tv mode" and drift away
02:57:46 (othiym23) listochkin: if you're still around and still have questions about domain nesting, I can probably answer your question(s)
02:58:21 (listochkin) othiym23: I'm here :) reading old threads on NodeJS mailing list
02:58:42 (othiym23) listochkin: for starters, you can always check process.domain or require('domain').active to see if a function is executing inside a domain
02:59:36 (listochkin) othiym23: is it still around? I saw people mentioning that but neither of these are in the current API docs
02:59:46 (othiym23) as far as nesting is concerned, when you enter a domain (via d.run(), d.enter() or calling a function that's bound to the domain using d.bind()), it gets pushed onto a domain stack
03:00:25 (othiym23) when a domain is exited, all domains nested within that domain are also exited, and the error handler exits the domain
03:00:43 (othiym23) listochkin: they're not part of the official API, but they're not going anywhere
03:01:11 (listochkin) othiym23: ah, I see
03:01:28 (othiym23) I think they're undocumented because you shouldn't need them unless you're building something on top of domains explicitly
03:03:02 (othiym23) there are few situations in which module maintainers should need to add domain support themselves
03:03:43 (othiym23) generally it's best handled by app developers, because the goal is to get enough information for you to act on and then shut down the service gracefully (which is something that's been discussed a bunch both on the mailing list and on GitHub issues)
03:04:10 thatguydan_ is now known as thatguydan
03:04:22 (othiym23) the edge cases are connection pools, where there are going to be EventEmitters that are long-lived and associated with more than one connection
03:04:47 (othiym23) and given the way felixge's MySQL driver works, it's kind of an edge case
03:05:04 zz_silvers is now known as silvers
03:05:31 (othiym23) if you were gonna mash it up with, say, generic-pool, you'd want to make sure that you incorporated domain support to your connection checkout implementation, but it maybe doesn't need to be in node-mysql itself
03:06:29 (listochkin) othiym23: thanks a lot! That clear things up for me
03:07:31 (othiym23) you're welcome!
03:08:03 (ChrisPartridge) yeah, good stuff othiym23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment