Skip to content

Commit 5d1f16c

Browse files
author
ericzcwang
committed
1. RpcClient超时时间日志问题修复
2. RpcClientProxy的lastUsedNanos使volatile,避免多线程可见性不一致; 3. 使用ConcurrentMap的compute,在返回之前updateLastUsedNanos,保证其他线程get的时候拿到的是都是已经更新过最后使用时间的
1 parent d5ad43f commit 5d1f16c

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

trpc-core/src/main/java/com/tencent/trpc/core/cluster/RpcClusterClientManager.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static void scanUnusedClient() {
127127
} finally {
128128
logger.warn("RpcClient in clusterName={}, naming={}, remove rpc client{}, due to unused time > {} ms",
129129
bConfig.getName(), bConfig.getNamingOptions().getServiceNaming(),
130-
e.getProtocolConfig().toSimpleString(), e.getProtocolConfig().getIdleTimeout());
130+
e.getProtocolConfig().toSimpleString(), bConfig.getIdleTimeout());
131131
}
132132
}));
133133
}
@@ -148,10 +148,14 @@ private static boolean isIdleTimeout(BackendConfig bConfig, RpcClientProxy clien
148148
public static RpcClient getOrCreateClient(BackendConfig bConfig, ProtocolConfig pConfig) {
149149
Preconditions.checkNotNull(bConfig, "backendConfig can't not be null");
150150
Map<String, RpcClientProxy> map = CLUSTER_MAP.computeIfAbsent(bConfig, k -> new ConcurrentHashMap<>());
151-
RpcClientProxy rpcClientProxy = map.computeIfAbsent(pConfig.toUniqId(),
152-
uniqId -> createRpcClientProxy(pConfig));
153-
rpcClientProxy.updateLastUsedNanos();
154-
return rpcClientProxy;
151+
return map.compute(pConfig.toUniqId(),
152+
(uniqId, client) -> {
153+
if (client == null) {
154+
client = createRpcClientProxy(pConfig);
155+
}
156+
client.updateLastUsedNanos();
157+
return client;
158+
});
155159
}
156160

157161
private static RpcClientProxy createRpcClientProxy(ProtocolConfig protocolConfig) {
@@ -252,7 +256,7 @@ private static class RpcClientProxy implements RpcClient {
252256

253257
private RpcClient delegate;
254258

255-
private long lastUsedNanos = System.nanoTime();
259+
private volatile long lastUsedNanos = System.nanoTime();
256260

257261
RpcClientProxy(RpcClient delegate) {
258262
this.delegate = delegate;

0 commit comments

Comments
 (0)