2525import org .apache .dubbo .rpc .Invoker ;
2626import org .apache .dubbo .rpc .Result ;
2727import org .apache .dubbo .rpc .RpcException ;
28+ import org .apache .dubbo .rpc .RpcInvocation ;
2829
2930import java .util .Arrays ;
3031
@@ -36,21 +37,38 @@ public class TimeoutFilter implements Filter {
3637
3738 private static final Logger logger = LoggerFactory .getLogger (TimeoutFilter .class );
3839
40+ private static final String TIMEOUT_FILTER_START_TIME = "timeout_filter_start_time" ;
41+
3942 @ Override
4043 public Result invoke (Invoker <?> invoker , Invocation invocation ) throws RpcException {
41- long start = System .currentTimeMillis ();
42- Result result = invoker .invoke (invocation );
43- long elapsed = System .currentTimeMillis () - start ;
44- if (invoker .getUrl () != null
45- && elapsed > invoker .getUrl ().getMethodParameter (invocation .getMethodName (),
46- "timeout" , Integer .MAX_VALUE )) {
47- if (logger .isWarnEnabled ()) {
48- logger .warn ("invoke time out. method: " + invocation .getMethodName ()
49- + " arguments: " + Arrays .toString (invocation .getArguments ()) + " , url is "
50- + invoker .getUrl () + ", invoke elapsed " + elapsed + " ms." );
44+ if (invocation .getAttachments () != null ) {
45+ long start = System .currentTimeMillis ();
46+ invocation .getAttachments ().put (TIMEOUT_FILTER_START_TIME , String .valueOf (start ));
47+ } else {
48+ if (invocation instanceof RpcInvocation ) {
49+ RpcInvocation invc = (RpcInvocation ) invocation ;
50+ long start = System .currentTimeMillis ();
51+ invc .setAttachment (TIMEOUT_FILTER_START_TIME , String .valueOf (start ));
5152 }
5253 }
53- return result ;
54+ return invoker . invoke ( invocation ) ;
5455 }
5556
57+ @ Override
58+ public Result onResponse (Result result , Invoker <?> invoker , Invocation invocation ) {
59+ String startAttach = invocation .getAttachment (TIMEOUT_FILTER_START_TIME );
60+ if (startAttach != null ) {
61+ long elapsed = System .currentTimeMillis () - Long .valueOf (startAttach );
62+ if (invoker .getUrl () != null
63+ && elapsed > invoker .getUrl ().getMethodParameter (invocation .getMethodName (),
64+ "timeout" , Integer .MAX_VALUE )) {
65+ if (logger .isWarnEnabled ()) {
66+ logger .warn ("invoke time out. method: " + invocation .getMethodName ()
67+ + " arguments: " + Arrays .toString (invocation .getArguments ()) + " , url is "
68+ + invoker .getUrl () + ", invoke elapsed " + elapsed + " ms." );
69+ }
70+ }
71+ }
72+ return result ;
73+ }
5674}
0 commit comments