Function retry::retry
[−]
[src]
pub fn retry<F, G, R>(tries: usize, wait: u32, value_fn: F, condition_fn: G) -> Result<R, RetryError> where F: FnMut() -> R, G: FnMut(&R) -> bool
Invokes a function until a condition is satisfied.
value_fn
is a closure that will be executed to produce a value. condition_fn
is a closure
that takes the value produced by value_fn
and returns a boolean indicating whether or not
some desired condition is true. retry
will invoke value_fn
up to tries
times, returning
the value as soon as condition_fn
is satisfied, or returning an error when the last try was
unsuccessful. It will wait wait
milliseconds after each unsuccessful try.
Failures
Will fail when:
value_fn
has been been invokedtries
times and has still not satisfiedcondition_fn
.tries
is zero. It must be at least 1.
Examples
Imagine an HTTP API with an endpoint that returns 204 No Content while a job is processing, and eventually 200 OK when the job has completed. Retrying until the job is finished would be written:
match retry(10, 500, || api_client.query_job_status(), |response| response.code == 200) { Ok(response) => println!("Job completed with result: {}", response.body), Err(error) => println!("Job completion could not be verified: {}", error), }
This retries the API call up to 10 times, waiting 500 milliseconds after each unsuccesful attempt.