Talk about dubbo’s TimeoutFilter.

  dubbo

Order

This article mainly studies dubbo’s TimeoutFilter.

ListenableFilter

dubbo-2.7.2/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/ListenableFilter.java

public abstract class ListenableFilter implements Filter {

    protected Listener listener = null;

    public Listener listener() {
        return listener;
    }
}
  • The ListenableFilter declaration implements the Filter interface, which mainly defines the listener () method

TimeoutFilter

dubbo-2.7.2/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/TimeoutFilter.java

@Activate(group = CommonConstants.PROVIDER)
public class TimeoutFilter extends ListenableFilter {

    private static final Logger logger = LoggerFactory.getLogger(TimeoutFilter.class);

    private static final String TIMEOUT_FILTER_START_TIME = "timeout_filter_start_time";

    public TimeoutFilter() {
        super.listener = new TimeoutListener();
    }

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        invocation.setAttachment(TIMEOUT_FILTER_START_TIME, String.valueOf(System.currentTimeMillis()));
        return invoker.invoke(invocation);
    }

    static class TimeoutListener implements Listener {

        @Override
        public void onResponse(Result appResponse, 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.");
                    }
                }
            }
        }

        @Override
        public void onError(Throwable t, Invoker<?> invoker, Invocation invocation) {

        }
    }
}
  • TimeoutFilter inherits ListenableFilter, and its constructor initializes the listener as TimeoutListener; ; The invoke method first records the time when execution starts, and then calls back the Listener’s onResponse or onError method at the end of execution. The onResponse of timeoutListener takes out the timeout_filt er_start_time value. If it exists, the calculation will take time to execute. When a valid timeout is set and the time is longer than the Timeout, the warn log will be printed.

Summary

TimeoutFilter inherits ListenableFilter, and its constructor initializes the listener as TimeoutListener;; The invoke method first records the time when execution starts, and then calls back the Listener’s onResponse or onError method at the end of execution. The onResponse of timeoutListener takes out the timeout_filter_start_time value. If it exists, the calculation will take time to execute. When a valid timeout is set and the time is longer than the Timeout, the warn log will be printed.

doc