Closed puzpuzpuz closed 4 years ago
Great idea! I'll add this later today.
@puzpuzpuz Do you think that isolation across multiple fastify instances is an important feature and access to context should be key-based, or just one instance is enough?
In my opinion, majority of users are using a single Fastify instance per app, so a global request context should be fine for them. There might be cases when the app runs multiple Fastify instances (and thus listens on multiple ports), but those are quite rare. And even in that case each HTTP request will be associated with a different async context anyway, so things will be isolated even with the global request context approach.
Thanks, that makes sense!
Released in 2.1.0
🚀 Feature Proposal
Expose request context (at least the CLS part of it) as a global object instead of (or as an alternative to) asking users to access it through
req
/app
.Motivation
The whole purpose of any CLS is to avoid having to pass a context object through application modules. Exposing request context through the
req
object kinda kills the whole idea. Having it available onapp
makes things a bit better as it could provide isolation guarantees for multiple Fastify instances running in a single node process, but it looks like the underlyingasynchronous-local-storage
library has a single globalAsyncLocalStorage
instance, which means no isolation as such.Example