-
Notifications
You must be signed in to change notification settings - Fork 525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IOLocal
propagation for unsafe access
#3636
Changes from 1 commit
0b88c01
db743e2
716ef32
0a69caf
2775064
270764f
d55489d
2cf72a5
cb3859d
7dce01c
5e171ac
c2f312d
638930d
9174c6a
1987e3a
02a43a6
a7bf748
145fc0e
fa99a5c
6cad03c
bb5d4b1
7517755
8d8e004
3589db4
522677e
6cc4d38
ac88480
49e5c30
925f504
d63a6ff
d4549fb
2502045
535fc8a
d854799
f070552
2cf1d8a
0eec9dd
af84973
1adf368
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -250,6 +250,12 @@ private final class IOFiber[A]( | |
pushTracingEvent(cur.event) | ||
} | ||
|
||
var locals: IOLocals = null | ||
if (dumpLocals) { | ||
locals = new IOLocals(localState) | ||
IOLocals.threadLocal.set(locals) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did this just for |
||
|
||
var error: Throwable = null | ||
val r = | ||
try cur.thunk() | ||
|
@@ -260,6 +266,11 @@ private final class IOFiber[A]( | |
onFatalFailure(t) | ||
} | ||
|
||
if (dumpLocals) { | ||
localState = locals.getState() | ||
IOLocals.threadLocal.set(null) | ||
} | ||
|
||
val next = | ||
if (error == null) succeeded(r, 0) | ||
else failed(error, 0) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cats.effect | ||
package unsafe | ||
|
||
// TODO handle defaults and lenses. all do-able, just needs refactoring ... | ||
final class IOLocals private[effect] (private[this] var state: IOLocalState) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, so I was extremely lazy with implementing this thing. But the main idea of this wrapper API is that it should only give the user access to |
||
|
||
private[effect] def getState(): IOLocalState = state | ||
|
||
def get[A](iol: IOLocal[A]): A = state(iol).asInstanceOf[A] | ||
|
||
def set[A](iol: IOLocal[A], value: A): Unit = state += (iol -> value) | ||
|
||
def reset[A](iol: IOLocal[A]): Unit = state -= iol | ||
|
||
// TODO other ops from IOLocal | ||
|
||
} | ||
|
||
object IOLocals { | ||
val threadLocal = ThreadLocal.withInitial[IOLocals](() => null) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bikesheddable configuration for opting-in. So the rest of us don't have to pay the penalty 😇
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this specifically "tracing", even if that's the most obvious use case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh woops, this was a very lazy copy-pasta. I copied it from the system properties we use to configure fiber tracing. We should rename it anyway,
dumpLocals
is not quite right I think 😅There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
cats.effect.localContextPropagation
similar to Monix'smonix.environment.localContextPropagation
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I liked that! I went with
cats.effect.ioLocalPropagation
.