question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support mirroring FiberRef contents to ThreadLocal

See original GitHub issue

It would be really useful for FiberRef contents to be mirrored to non-zio infra (outside of our control) backed by ThreadLocals. 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:closed
  • Created 3 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dkarlinskycommented, Feb 12, 2021

Thanks @jdegoes !

1reaction
jdegoescommented, Feb 11, 2021

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).

Read more comments on GitHub >

github_iconTop Results From Across the Web

FiberRef - ZIO
FiberRef is the fiber version of ThreadLocal with significant improvements in its semantics. A ThreadLocal only has a mutable state in which each...
Read more >
An Introduction to ThreadLocal in Java - Baeldung
A quick and practical guide to using ThreadLocal for storing thread-specific data in Java.
Read more >
Java ThreadLocal Example - DigitalOcean
While we believe that this content benefits our community, we have not yet ... Java ThreadLocal is used to create thread local variables....
Read more >
ThreadLocal<T> Class (System.Threading) - Microsoft Learn
Value // One usage of ThreadLocal(T) static void Main() { // Thread-Local variable that yields a name for a thread ThreadLocal<string> ThreadName =...
Read more >
ThreadLocal (Java SE 11 & JDK 11 ) - Oracle Help Center
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found