domain.dispose()
Closed this issue · 4 comments
Hello,
I noticed that in a similar module they dispose of the domains:
https://github.com/baryshev/connect-domain/blob/master/lib/connect-domain.js
I am still learning Domains so I thought I should just ask; this module does not do this. Will that not create a memory leak keeping all the domains open?
Thanks :)
Hey @Guuz - good question. I didn't include domain.dispose
because for a time it was deprecated in node, and I've heard a lot of things around it being an anti-pattern of sorts. I'm not sure 100% on it's future status & I know the domain API lists it as officially not to use in newer versions of node. You're 100% safe to use domains without calling dispose()
on them & they definitely wont leak. 😄
Anecdotally we push about 100 million requests a day through a single c3.large instance running express w/ this module & forky and have 0 memory leak problems.
Thanks for the clear and quick response!
And 1 last question if you read this:
Is it safe to add stuff to process.domain? I see you add an ID. I want to add an array of promises that my current request has to wait for before it can complete.
Can i just do process.domain.myData = 'foo'
and have it accessible in this domain in all code?
Thanks for the help! :D
No problemo! My pleasure.
Yeah it's absolutely safe to add stuff to process.domain
. We do this as well in our main node app & use it so we can log things later. We have a setup kind of like this...
var domainMiddleware = require('express-domain-middleware')
app.use(domainMiddleware)
app.use(sessions())
app.use(function(req, res, next) {
process.domain.context = {
req: req,
user: req.session('user')
}
})
then in our centralized logging layer we can do this (slightly simplified):
var log.debug = function(message) {
var id = process.domain.id
var method = process.domain.context.req.method
var path = process.domain.context.req.path
var userId = process.domain.context.user.id
console.log(id, method, path, userId, message)
}
And this gives you the really really nice output of having every log message on an individual request associated with a single ID, and you can identify the method, path, and user who made the request in every message as well.
Wow! thanks for the great example! There is not a lot I can find about this from real production use cases :) 👍