Skip to content

Commit 45f2774

Browse files
committed
代码优化
修复跑线程池thread local泄漏的bug
1 parent ab6a28c commit 45f2774

File tree

5 files changed

+57
-37
lines changed

5 files changed

+57
-37
lines changed

spring-cloud-gray-client-netflix/src/main/java/cn/springcloud/gray/client/netflix/connectionpoint/DefaultHystrixRibbonConnectionPoint.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@Deprecated
88
public class DefaultHystrixRibbonConnectionPoint extends DefaultRibbonConnectionPoint {
99

10-
private ThreadLocal<Boolean> hystrixRequestContextInitialized = new ThreadLocal<>();
10+
private static final ThreadLocal<Boolean> hystrixRequestContextInitialized = new ThreadLocal<>();
1111

1212
public DefaultHystrixRibbonConnectionPoint(
1313
GrayManager grayManager,
@@ -32,8 +32,12 @@ public void shutdownconnectPoint(ConnectPointContext connectPointContext) {
3232
super.shutdownconnectPoint(connectPointContext);
3333
} finally {
3434
// Boolean hystrixReqCxtInited = hystrixRequestContextInitialized.get();
35-
// if (hystrixReqCxtInited != null && hystrixReqCxtInited && HystrixRequestContext.isCurrentThreadInitialized()) {
36-
// HystrixRequestContext.getContextForCurrentThread().shutdown();
35+
// if (hystrixReqCxtInited != null) {
36+
// hystrixRequestContextInitialized.remove();
37+
// if (hystrixReqCxtInited && HystrixRequestContext.isCurrentThreadInitialized()) {
38+
// hystrixRequestContextInitialized.remove();
39+
// HystrixRequestContext.getContextForCurrentThread().shutdown();
40+
// }
3741
// }
3842
}
3943
}

spring-cloud-gray-client-netflix/src/main/java/cn/springcloud/gray/client/netflix/feign/GrayTrackFeignRequestInterceptor.java

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,44 @@ public GrayTrackFeignRequestInterceptor(RequestLocalStorage requestLocalStorage)
2626

2727
@Override
2828
public void apply(RequestTemplate template) {
29-
GrayHttpTrackInfo grayTrack = getGrayHttpTrackInfo();
30-
if (grayTrack != null) {
31-
if (StringUtils.isNotEmpty(grayTrack.getUri())) {
32-
template.header(GrayHttpTrackInfo.GRAY_TRACK_URI, grayTrack.getUri());
33-
}
34-
if (StringUtils.isNotEmpty(grayTrack.getTraceIp())) {
35-
template.header(GrayHttpTrackInfo.GRAY_TRACK_TRACE_IP, grayTrack.getTraceIp());
36-
}
37-
if (StringUtils.isNotEmpty(grayTrack.getMethod())) {
38-
template.header(GrayHttpTrackInfo.GRAY_TRACK_METHOD, grayTrack.getMethod());
39-
}
40-
if (grayTrack.getParameters() != null && !grayTrack.getParameters().isEmpty()) {
41-
grayTrack.getParameters().entrySet().forEach(entry -> {
42-
String name = new StringBuilder().append(GrayHttpTrackInfo.GRAY_TRACK_PARAMETER_PREFIX)
43-
.append(GrayTrackInfo.GRAY_TRACK_SEPARATE)
44-
.append(entry.getKey()).toString();
45-
template.header(name, entry.getValue());
46-
});
47-
}
48-
if (grayTrack.getHeaders() != null && !grayTrack.getHeaders().isEmpty()) {
49-
grayTrack.getHeaders().entrySet().forEach(entry -> {
50-
String name = new StringBuilder().append(GrayHttpTrackInfo.GRAY_TRACK_HEADER_PREFIX)
51-
.append(GrayTrackInfo.GRAY_TRACK_SEPARATE)
52-
.append(entry.getKey()).toString();
53-
template.header(name, entry.getValue());
54-
});
55-
}
56-
57-
appendGrayTrackInfoToHeader(GrayTrackInfo.GRAY_TRACK_ATTRIBUTE_PREFIX, grayTrack.getAttributes(), template);
29+
GrayHttpTrackInfo grayTrack = getGrayHttpTrackInfo(template);
30+
if (grayTrack == null) {
31+
return;
32+
}
33+
if (StringUtils.isNotEmpty(grayTrack.getUri())) {
34+
template.header(GrayHttpTrackInfo.GRAY_TRACK_URI, grayTrack.getUri());
35+
}
36+
if (StringUtils.isNotEmpty(grayTrack.getTraceIp())) {
37+
template.header(GrayHttpTrackInfo.GRAY_TRACK_TRACE_IP, grayTrack.getTraceIp());
38+
}
39+
if (StringUtils.isNotEmpty(grayTrack.getMethod())) {
40+
template.header(GrayHttpTrackInfo.GRAY_TRACK_METHOD, grayTrack.getMethod());
5841
}
42+
if (grayTrack.getParameters() != null && !grayTrack.getParameters().isEmpty()) {
43+
grayTrack.getParameters().entrySet().forEach(entry -> {
44+
String name = new StringBuilder().append(GrayHttpTrackInfo.GRAY_TRACK_PARAMETER_PREFIX)
45+
.append(GrayTrackInfo.GRAY_TRACK_SEPARATE)
46+
.append(entry.getKey()).toString();
47+
template.header(name, entry.getValue());
48+
});
49+
}
50+
if (grayTrack.getHeaders() != null && !grayTrack.getHeaders().isEmpty()) {
51+
grayTrack.getHeaders().entrySet().forEach(entry -> {
52+
String name = new StringBuilder().append(GrayHttpTrackInfo.GRAY_TRACK_HEADER_PREFIX)
53+
.append(GrayTrackInfo.GRAY_TRACK_SEPARATE)
54+
.append(entry.getKey()).toString();
55+
template.header(name, entry.getValue());
56+
});
57+
}
58+
59+
appendGrayTrackInfoToHeader(GrayTrackInfo.GRAY_TRACK_ATTRIBUTE_PREFIX, grayTrack.getAttributes(), template);
5960
}
6061

61-
private GrayHttpTrackInfo getGrayHttpTrackInfo() {
62+
private GrayHttpTrackInfo getGrayHttpTrackInfo(RequestTemplate template) {
6263
try {
6364
return (GrayHttpTrackInfo) requestLocalStorage.getGrayTrackInfo();
6465
} catch (Exception e) {
65-
log.error("从requestLocalStorage中获取GrayTrackInfo对象失败.", e);
66+
log.warn("从requestLocalStorage中获取GrayTrackInfo对象失败, url:{}", template.url(), e);
6667
return null;
6768
}
6869
}

spring-cloud-gray-client-netflix/src/main/java/cn/springcloud/gray/client/netflix/hystrix/HystrixLocalStorageCycle.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ public void initContext() {
2020
@Override
2121
public void closeContext() {
2222
Boolean hystrixReqCxtInited = hystrixRequestContextInitialized.get();
23-
if (hystrixReqCxtInited != null && hystrixReqCxtInited && HystrixRequestContext.isCurrentThreadInitialized()) {
24-
HystrixRequestContext.getContextForCurrentThread().shutdown();
23+
if (hystrixReqCxtInited != null) {
24+
hystrixRequestContextInitialized.remove();
25+
if (hystrixReqCxtInited && HystrixRequestContext.isCurrentThreadInitialized()) {
26+
HystrixRequestContext.getContextForCurrentThread().shutdown();
27+
}
28+
2529
}
2630
}
2731
}

spring-cloud-gray-client/src/main/java/cn/springcloud/gray/concurrent/GrayConcurrentHelper.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
import cn.springcloud.gray.GrayClientHolder;
44
import cn.springcloud.gray.request.GrayTrackInfo;
55
import cn.springcloud.gray.request.RequestLocalStorage;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
68

79
import java.util.Collection;
810
import java.util.concurrent.Callable;
911
import java.util.stream.Collectors;
1012

1113
public class GrayConcurrentHelper {
1214

15+
private static final Logger log = LoggerFactory.getLogger(GrayConcurrentHelper.class);
16+
1317
private GrayConcurrentHelper() {
1418
}
1519

@@ -59,7 +63,12 @@ public static <V> GrayCallableContext createGrayCallableContext(Callable<V> call
5963

6064
public static GrayTrackInfo getGrayTrackInfo() {
6165
RequestLocalStorage requestLocalStorage = GrayClientHolder.getRequestLocalStorage();
62-
return requestLocalStorage == null ? null : requestLocalStorage.getGrayTrackInfo();
66+
try {
67+
return requestLocalStorage == null ? null : requestLocalStorage.getGrayTrackInfo();
68+
} catch (Exception e) {
69+
log.warn("获取GrayTrackInfo失败, 线程名是 {}", Thread.currentThread().getName(), e);
70+
return null;
71+
}
6372
}
6473

6574
}

spring-cloud-gray-client/src/main/java/cn/springcloud/gray/concurrent/GrayRunnable.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public void run() {
2222
try {
2323
context.getTarget().run();
2424
} finally {
25+
requestLocalStorage.removeGrayTrackInfo();
26+
requestLocalStorage.removeGrayRequest();
2527
localStorageLifeCycle.closeContext();
2628
}
2729
}

0 commit comments

Comments
 (0)