Skip to content

Prevent the RpcClient cleanup thread from cleaning up the client object that is about to be used #76

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static void scanUnusedClient() {
} finally {
logger.warn("RpcClient in clusterName={}, naming={}, remove rpc client{}, due to unused time > {} ms",
bConfig.getName(), bConfig.getNamingOptions().getServiceNaming(),
e.getProtocolConfig().toSimpleString(), e.getProtocolConfig().getIdleTimeout());
e.getProtocolConfig().toSimpleString(), bConfig.getIdleTimeout());
}
}));
}
Expand All @@ -148,10 +148,14 @@ private static boolean isIdleTimeout(BackendConfig bConfig, RpcClientProxy clien
public static RpcClient getOrCreateClient(BackendConfig bConfig, ProtocolConfig pConfig) {
Preconditions.checkNotNull(bConfig, "backendConfig can't not be null");
Map<String, RpcClientProxy> map = CLUSTER_MAP.computeIfAbsent(bConfig, k -> new ConcurrentHashMap<>());
RpcClientProxy rpcClientProxy = map.computeIfAbsent(pConfig.toUniqId(),
uniqId -> createRpcClientProxy(pConfig));
rpcClientProxy.updateLastUsedNanos();
return rpcClientProxy;
return map.compute(pConfig.toUniqId(),
(uniqId, client) -> {
if (client == null) {
client = createRpcClientProxy(pConfig);
}
client.updateLastUsedNanos();
return client;
});
}

private static RpcClientProxy createRpcClientProxy(ProtocolConfig protocolConfig) {
Expand Down Expand Up @@ -252,7 +256,7 @@ private static class RpcClientProxy implements RpcClient {

private RpcClient delegate;

private long lastUsedNanos = System.nanoTime();
private volatile long lastUsedNanos = System.nanoTime();

RpcClientProxy(RpcClient delegate) {
this.delegate = delegate;
Expand Down
Loading