Skip to content

Commit da6f7aa

Browse files
committed
refactor: 优化上下文处理
1 parent 25af44d commit da6f7aa

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

hsweb-core/src/main/java/org/hswebframework/web/context/ThreadLocalContextHolderSupport.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.hswebframework.web.context;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import reactor.util.context.Context;
45
import reactor.util.context.ContextView;
56

@@ -9,6 +10,7 @@
910
* 基于 ThreadLocal 的上下文持有器支持实现
1011
* 适用于传统平台线程环境
1112
*/
13+
@Slf4j
1214
public class ThreadLocalContextHolderSupport implements ContextHolder.ContextHolderSupport {
1315

1416
private static final ThreadLocal<Context> contextHolder = ThreadLocal.withInitial(Context::empty);
@@ -23,8 +25,16 @@ public Closeable makeCurrent(ContextView context) {
2325
Context previous = contextHolder.get();
2426
Context newContext = previous.putAll(context);
2527
contextHolder.set(newContext);
26-
27-
return () -> contextHolder.set(previous);
28+
Thread bound = Thread.currentThread();
29+
30+
return () -> {
31+
Thread current = Thread.currentThread();
32+
if (current != bound) {
33+
log.warn("Context holder is cross thread {}=>{} {}", bound, current, context);
34+
} else {
35+
contextHolder.set(previous);
36+
}
37+
};
2838
}
2939

3040
@Override

0 commit comments

Comments
 (0)