This is an automated email from the ASF dual-hosted git repository.

jefflv pushed a commit to branch 3.x-dev
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/3.x-dev by this push:
     new 8734320  Shorten the life cycle of Timeout to get a better gc effect 
(#4081)
8734320 is described below

commit 873432056b9a93d51bd678b501739d234e3869a6
Author: ken.lj <[email protected]>
AuthorDate: Fri May 17 14:12:13 2019 +0800

    Shorten the life cycle of Timeout to get a better gc effect (#4081)
---
 .../dubbo/remoting/exchange/support/DefaultFuture.java   | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git 
a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java
 
b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java
index 3324ad9..e13f651 100644
--- 
a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java
+++ 
b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java
@@ -48,6 +48,8 @@ public class DefaultFuture extends CompletableFuture<Object> {
 
     private static final Map<Long, DefaultFuture> FUTURES = new 
ConcurrentHashMap<>();
 
+    private static final Map<Long, Timeout> PENDING_TASKS = new 
ConcurrentHashMap<>();
+
     public static final Timer TIME_OUT_TIMER = new HashedWheelTimer(
             new NamedThreadFactory("dubbo-future-timeout", true),
             30,
@@ -131,6 +133,11 @@ public class DefaultFuture extends 
CompletableFuture<Object> {
             DefaultFuture future = FUTURES.remove(response.getId());
             if (future != null) {
                 future.doReceived(response);
+                Timeout t = PENDING_TASKS.remove(future.getId());
+                if (t != null) {
+                    // decrease Time
+                    t.cancel();
+                }
             } else {
                 logger.warn("The timeout response finally returned at "
                         + (new SimpleDateFormat("yyyy-MM-dd 
HH:mm:ss.SSS").format(new Date()))
@@ -201,9 +208,9 @@ public class DefaultFuture extends 
CompletableFuture<Object> {
      */
     private static void timeoutCheck(DefaultFuture future) {
         TimeoutCheckTask task = new TimeoutCheckTask(future);
-        TIME_OUT_TIMER.newTimeout(task, future.getTimeout(), 
TimeUnit.MILLISECONDS);
+        Timeout t = TIME_OUT_TIMER.newTimeout(task, future.getTimeout(), 
TimeUnit.MILLISECONDS);
+        PENDING_TASKS.put(future.getId(), t);
     }
-
     private String getTimeoutMessage(boolean scan) {
         long nowTimestamp = System.currentTimeMillis();
         return (sent > 0 ? "Waiting server-side response timeout" : "Sending 
request timeout in client-side")
@@ -227,7 +234,10 @@ public class DefaultFuture extends 
CompletableFuture<Object> {
 
         @Override
         public void run(Timeout timeout) {
-            if (future == null || future.isDone()) {
+            // remove from pending task
+            PENDING_TASKS.remove(future.getId());
+
+            if (future.isDone()) {
                 return;
             }
             // create exception response.

Reply via email to