Skip to content

Commit

Permalink
close client immediately when destroy unused invoker (#8756)
Browse files Browse the repository at this point in the history
  • Loading branch information
zrlw committed Sep 12, 2021
1 parent b7fa549 commit 55dec92
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
Expand Up @@ -317,7 +317,7 @@ protected void destroyAllInvokers() {
if (localUrlInvokerMap != null) {
for (Invoker<T> invoker : new ArrayList<>(localUrlInvokerMap.values())) {
try {
invoker.destroy();
invoker.destroyAll();
} catch (Throwable t) {
logger.warn("Failed to destroy service " + serviceKey + " to provider " + invoker.getUrl(), t);
}
Expand Down Expand Up @@ -359,7 +359,7 @@ private void destroyUnusedInvokers(Map<String, Invoker<T>> oldUrlInvokerMap, Map
Invoker<T> invoker = oldUrlInvokerMap.remove(addressKey);
if (invoker != null) {
try {
invoker.destroy();
invoker.destroyAll();
if (logger.isDebugEnabled()) {
logger.debug("destroy invoker[" + invoker.getUrl() + "] success. ");
}
Expand Down
Expand Up @@ -409,7 +409,7 @@ protected void destroyAllInvokers() {
if (localUrlInvokerMap != null) {
for (Invoker<T> invoker : new ArrayList<>(localUrlInvokerMap.values())) {
try {
invoker.destroy();
invoker.destroyAll();
} catch (Throwable t) {
logger.warn("Failed to destroy service " + serviceKey + " to provider " + invoker.getUrl(), t);
}
Expand Down Expand Up @@ -439,7 +439,7 @@ private void destroyUnusedInvokers(Map<URL, Invoker<T>> oldUrlInvokerMap, Map<UR
Invoker<T> invoker = oldUrlInvokerMap.get(key);
if (invoker != null) {
try {
invoker.destroy();
invoker.destroyAll();
if (logger.isDebugEnabled()) {
logger.debug("destroy invoker[" + invoker.getUrl() + "] success. ");
}
Expand Down
Expand Up @@ -25,4 +25,8 @@
*/
public interface ExchangeClient extends Client, ExchangeChannel {

default void closeAll(int timeout) {
close(timeout);
}

}
Expand Up @@ -43,4 +43,11 @@ public interface Invoker<T> extends Node {
*/
Result invoke(Invocation invocation) throws RpcException;

/**
* destroy all
*/
default void destroyAll() {
destroy();
}

}
Expand Up @@ -135,6 +135,20 @@ public boolean isAvailable() {

@Override
public void destroy() {
destroyInternal(false);
}

@Override
public void destroyAll() {
destroyInternal(true);
}

/**
* when destroy unused invoker, closeAll should be true
*
* @param closeAll
*/
private void destroyInternal(boolean closeAll) {
// in order to avoid closing a client multiple times, a counter is used in case of connection per jvm, every
// time when client.close() is called, counter counts down once, and when counter reaches zero, client will be
// closed.
Expand All @@ -153,7 +167,11 @@ public void destroy() {
}
for (ExchangeClient client : clients) {
try {
client.close(ConfigurationUtils.getServerShutdownTimeout());
if (closeAll) {
client.closeAll(ConfigurationUtils.getServerShutdownTimeout());
} else {
client.close(ConfigurationUtils.getServerShutdownTimeout());
}
} catch (Throwable t) {
logger.warn(t.getMessage(), t);
}
Expand Down
Expand Up @@ -158,7 +158,22 @@ public void close() {

@Override
public void close(int timeout) {
if (referenceCount.decrementAndGet() <= 0) {
closeInternal(timeout, false);
}

@Override
public void closeAll(int timeout) {
closeInternal(timeout, true);
}

/**
* when destroy unused invoker, closeAll should be true
*
* @param timeout
* @param closeAll
*/
private void closeInternal(int timeout, boolean closeAll) {
if (closeAll || referenceCount.decrementAndGet() <= 0) {
if (timeout == 0) {
client.close();

Expand Down

0 comments on commit 55dec92

Please sign in to comment.