def timeout(seconds, err_msg="xtls: function run too long."):
    """
    超时检测
    :param seconds: 函数最长运行时间
    :param err_msg: 超时提示
    """

    def decorator(function):
        def _on_timeout(signum, frame):
            raise TimeoutError(err_msg)

        @wraps(function)
        def wrapper(*args, **kwargs):
            signal.signal(signal.SIGALRM, _on_timeout)
            signal.alarm(seconds)
            try:
                result = function(*args, **kwargs)
            finally:
                signal.alarm(0)
            return result

        return wrapper

    return decorator