FunctionHandler::Options

Contents

FunctionHandler::Options#

inline FunctionHandler<F> &YR::FunctionHandler::Options(const InvokeOptions &optsInput)#

Set the options for the current invocation, including resources, etc. This interface is not effective in local mode.

  1. Stateless function retry

    // static functions
    int AddOne(int x)
    {
        return x + 1;
    }
    
    void ThrowRuntimeError()
    {
        throw std::runtime_error("123");
    }
    
    YR_INVOKE(AddOne, ThrowRuntimeError);
    
    bool RetryChecker(const YR::Exception &e) noexcept
    {
        if (e.Code() == YR::ErrorCode::ERR_USER_FUNCTION_EXCEPTION) {
             std::string msg = e.what();
             if (msg.find("123") != std::string::npos) {
                 return true;
             }
        }
        return false;
    }
    int main(void) {
        YR::Config conf;
        YR::Init(conf);
        YR::InvokeOptions opts;
    
        opts.cpu = 1000;
        opts.memory = 1000;
        auto r1 = YR::Function(AddOne).Options(opts).Invoke(5);
        YR::Get(r1);
    
        opts.retryTimes = 1;
        opts.retryChecker = RetryChecker;
        auto r2 = YR::Function(ThrowRuntimeError).Options(opts).Invoke();
        YR::Get(r2);
    
        return 0;
    }
    

  2. Stateless functions specify local execution

    int CallLocal(int x)
    {
        YR::InvokeOptions opt;
        opt.alwaysLocalMode = true;
        auto obj = YR::Function(AddOne).Options(opt).Invoke(x);
        int ret = *YR::Get(obj);
        std::cout << "CallLocal result: " << ret << std::endl;
        return ret;
    }
    
    int CallCluster(int x)
    {
        auto obj = YR::Function(AddOne).Invoke(x);
        int ret = *YR::Get(obj);
        std::cout << "CallCluster result: " << ret << std::endl;
        return ret;
    }
    
    YR_INVOKE(CallLocal, CallCluster)
    
    void HybridClusterCallLocal()
    {
        auto obj = YR::Function(CallLocal).Invoke(1);
        std::cout << *YR::Get(obj) << " == " << 2 << std::endl;
    }
    
    void HybridLocalCallCluster()
    {
        YR::InvokeOptions opt;
        opt.alwaysLocalMode = true;
        auto obj = YR::Function(CallCluster).Options(opt).Invoke(1);
        std::cout << *YR::Get(obj) << " == " << 2 << std::endl;
    }
    
    int main()
    {
        HybridClusterCallLocal();
        HybridLocalCallCluster();
        return 0;
    }
    

Template Parameters:

F – the function type.

Parameters:

optsInput – the invocation options. For detailed descriptions, refer to struct InvokeOptions.

Returns:

FunctionHandler<F>&, a reference to the function handler object, facilitating direct invocation of the Invoke interface.