0

I have a function that try to call some services, one of them ( external service) have a lot of issues, and never give any answers and I would like to kill it after certain time.

I would like to find any crate that could set a timeout calling a function. But I can't manage to find one ?

fn main() {
    let result = retry::retry(Fixed::from_millis(3000).take(5), || {
            let possibly_return = function_maybe_never_return();
        match possibly_return {
            Ok(kubeconfig) => OperationResult::Ok(kubeconfig),
            Err(err) => Err(err)
        }
    });
    match result {
            Ok(downloaded) => info!("OK !!")
            Err(e) => Error!("Unable to ... after many retries {:?}", e),
    };
}

fn function_maybe_never_return(){
    
}

I want to use retry crate on it to retrying operation.

maathor
  • 121
  • 3
  • 12
  • You cannot kill a function. – n. 1.8e9-where's-my-share m. Dec 04 '20 at 14:39
  • yes, I know, but do you have any workarround ? – maathor Dec 04 '20 at 14:41
  • You can run the process on a seperate thread [and terminate it after a timeout](https://stackoverflow.com/questions/36181719/what-is-the-correct-way-in-rust-to-create-a-timeout-for-a-thread-or-a-function) – Ibraheem Ahmed Dec 04 '20 at 15:04
  • 1
    You can use `std::process::Command` to spawn an external process (possibly the program you're running with a special command-line flag that tells it to run the function) and terminate it after a timeout. That is the only portable way of achieving your goal. – user4815162342 Dec 04 '20 at 15:18
  • You can kill a process so perhaps run one. – n. 1.8e9-where's-my-share m. Dec 04 '20 at 18:11
  • Hi @maathor. If you can change the function then it should be possible to make it async and use one of existing libraries to make your call to external service async as well. The it'll be much easier to cancel function execution. Please let me know if you are interested in that kind of solution then I can provide you more details. – MaxV Dec 05 '20 at 19:43

0 Answers0