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

liujun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 21a608c  Merge pull request #3174, make timeout filter not work in 
async way.
21a608c is described below

commit 21a608c8ba580e6ae8aee66d733bd950243862df
Author: 时无两丶 <[email protected]>
AuthorDate: Thu Jan 24 12:50:50 2019 +0800

    Merge pull request #3174, make timeout filter not work in async way.
---
 .../org/apache/dubbo/rpc/filter/TimeoutFilter.java | 40 ++++++++++++++++------
 1 file changed, 29 insertions(+), 11 deletions(-)

diff --git 
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/TimeoutFilter.java
 
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/TimeoutFilter.java
index ad783ef..c00689f 100644
--- 
a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/TimeoutFilter.java
+++ 
b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/TimeoutFilter.java
@@ -25,6 +25,7 @@ import org.apache.dubbo.rpc.Invocation;
 import org.apache.dubbo.rpc.Invoker;
 import org.apache.dubbo.rpc.Result;
 import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.RpcInvocation;
 
 import java.util.Arrays;
 
@@ -36,21 +37,38 @@ public class TimeoutFilter implements Filter {
 
     private static final Logger logger = 
LoggerFactory.getLogger(TimeoutFilter.class);
 
+    private static final String TIMEOUT_FILTER_START_TIME = 
"timeout_filter_start_time";
+
     @Override
     public Result invoke(Invoker<?> invoker, Invocation invocation) throws 
RpcException {
-        long start = System.currentTimeMillis();
-        Result result = invoker.invoke(invocation);
-        long elapsed = System.currentTimeMillis() - start;
-        if (invoker.getUrl() != null
-                && elapsed > 
invoker.getUrl().getMethodParameter(invocation.getMethodName(),
-                "timeout", Integer.MAX_VALUE)) {
-            if (logger.isWarnEnabled()) {
-                logger.warn("invoke time out. method: " + 
invocation.getMethodName()
-                        + " arguments: " + 
Arrays.toString(invocation.getArguments()) + " , url is "
-                        + invoker.getUrl() + ", invoke elapsed " + elapsed + " 
ms.");
+        if (invocation.getAttachments() != null) {
+            long start = System.currentTimeMillis();
+            invocation.getAttachments().put(TIMEOUT_FILTER_START_TIME, 
String.valueOf(start));
+        } else {
+            if (invocation instanceof RpcInvocation) {
+                RpcInvocation invc = (RpcInvocation) invocation;
+                long start = System.currentTimeMillis();
+                invc.setAttachment(TIMEOUT_FILTER_START_TIME, 
String.valueOf(start));
             }
         }
-        return result;
+        return invoker.invoke(invocation);
     }
 
+    @Override
+    public Result onResponse(Result result, Invoker<?> invoker, Invocation 
invocation) {
+        String startAttach = 
invocation.getAttachment(TIMEOUT_FILTER_START_TIME);
+        if (startAttach != null) {
+            long elapsed = System.currentTimeMillis() - 
Long.valueOf(startAttach);
+            if (invoker.getUrl() != null
+                    && elapsed > 
invoker.getUrl().getMethodParameter(invocation.getMethodName(),
+                    "timeout", Integer.MAX_VALUE)) {
+                if (logger.isWarnEnabled()) {
+                    logger.warn("invoke time out. method: " + 
invocation.getMethodName()
+                            + " arguments: " + 
Arrays.toString(invocation.getArguments()) + " , url is "
+                            + invoker.getUrl() + ", invoke elapsed " + elapsed 
+ " ms.");
+                }
+            }
+        }
+        return result;
+    }
 }

Reply via email to