Support mirroring FiberRef contents to ThreadLocal
See original GitHub issueIt would be really useful for FiberRef
contents to be mirrored to non-zio infra (outside of our control) backed by ThreadLocal
s.
For example, when using NewRelic and it’s Tokens to connect async flows to what they call a transaction (think an incoming request).
In this case, we would store the new relic token in a FiberRef
like this:
FiberRef.make(nrToken, link = _.link())
The provided link
function will be called by zio after every async boundary so any effect running in this fiber (or a fiber inheriting this ref) will run on a thread “linked” to the token.
There might also be a need for unlink
function to clear the the thread at the end.
(MDC
is a simpler example, but maybe easier to work around with libraries like zio-logging
or FiberRef.unsafeAsThreadLocal
)
I’ve created this Draft PR a while back with a naive implementation for this.
@adamgfraser, @jdegoes WDYT?
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (7 by maintainers)
Top GitHub Comments
Thanks @jdegoes !
I wouldn’t do it when creating fiber ref, but do it separately. There’s nothing necessarily connected to fiber ref in such a feature. It may be more connected with
Supervisor
. Let me think about this for a week, but I do absolutely think we can get this into ZIO (thanks for the suggestion).