Skip to content

Commit e00a5f0

Browse files
committed
Refactor no-op scope approach
1 parent df32a37 commit e00a5f0

3 files changed

Lines changed: 40 additions & 19 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package datadog.context;
2+
3+
/** {@link ContextScope} that has no effect on execution units. */
4+
final class NoopContextScope implements ContextScope {
5+
static final NoopContextScope ROOT_SCOPE = new NoopContextScope(Context.root());
6+
7+
static NoopContextScope create(Context context) {
8+
return context == Context.root() ? ROOT_SCOPE : new NoopContextScope(context);
9+
}
10+
11+
private final Context context;
12+
13+
private NoopContextScope(Context context) {
14+
this.context = context;
15+
}
16+
17+
@Override
18+
public Context context() {
19+
return context;
20+
}
21+
22+
@Override
23+
public void close() {}
24+
}

components/context/src/main/java/datadog/context/ThreadLocalContextManager.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ ContextScope doAttach(Context context, @Nullable ContextContinuationImpl continu
3434
continuation.releaseOnScopeClose();
3535
return continuation; // acts as no-op scope, avoiding allocation
3636
}
37-
return holder; // acts as no-op scope, avoiding allocation
37+
NoopContextScope noopScope = holder.noopScope;
38+
if (context != noopScope.context()) {
39+
noopScope = holder.noopScope = NoopContextScope.create(context);
40+
}
41+
return noopScope;
3842
}
3943

4044
ContextListener[] ls = listeners;
@@ -161,15 +165,14 @@ public final Context context() {
161165

162166
@Override
163167
public void close() {
164-
if (!closed) {
165-
// check for out-of-order close to avoid corrupting the current state
166-
if (context == holder.current) {
167-
ContextListener[] ls = INSTANCE.listeners;
168-
notifyDetach(context, ls);
169-
holder.current = previous;
170-
notifyAttach(previous, ls);
171-
closed = true;
172-
}
168+
// check for out-of-order close to avoid corrupting the current state
169+
if (!closed && context == holder.current) {
170+
ContextListener[] ls = INSTANCE.listeners;
171+
notifyDetach(context, ls);
172+
holder.current = previous;
173+
holder.noopScope = NoopContextScope.ROOT_SCOPE;
174+
notifyAttach(previous, ls);
175+
closed = true;
173176
}
174177
}
175178
}
@@ -290,15 +293,9 @@ void releaseOnScopeClose() {
290293
public void close() {}
291294
}
292295

293-
private static final class ContextHolder implements ContextScope {
296+
private static final class ContextHolder {
294297
Context current = Context.root();
295298

296-
@Override
297-
public Context context() {
298-
return current;
299-
}
300-
301-
@Override
302-
public void close() {}
299+
NoopContextScope noopScope = NoopContextScope.ROOT_SCOPE;
303300
}
304301
}

components/context/src/test/java/datadog/context/ContextProvidersForkedTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Context current() {
7373

7474
@Override
7575
public ContextScope attach(Context context) {
76-
return NoopContextContinuation.ROOT_CONTINUATION;
76+
return NoopContextScope.ROOT_SCOPE;
7777
}
7878

7979
@Override

0 commit comments

Comments
 (0)