Skip to main content

Crate ic_call_retry

Crate ic_call_retry 

Source
Expand description

A library for retrying calls to the Internet Computer with configurable retry policies.

This library provides utilities for retrying calls to the Internet Computer with different retry policies. It supports both idempotent and non-idempotent calls, though you must decide yourself which calls are idempotent and which are not.

Note that retries are always executed immediately, there is no backoff strategy. This is because the Internet Computer doesn’t (yet?) support “pausing” a call context. Retries with backoffs would have to be implemented as background tasks.

§Features

  • Support for both idempotent and non-idempotent calls
  • Configurable retry policies with deadlines
  • Detailed error reporting

§Examples

use ic_call_retry::{call_idempotent_method_with_retry, when_out_of_time_or_stopping, Deadline};
use ic_cdk::api::time;
use ic_cdk::call::Call;

async fn example_retry_call() -> Result<(), String> {
    // Set a deadline 5 seconds in the future
    let deadline = time() + 5_000_000_000; // 5 seconds in nanoseconds
    let deadline = Deadline::TimeOrStopping(deadline);

    // Create a call to some canister
    let call = Call::bounded_wait(canister_id, "some_method")
        .with_arg(&arg);

    // Retry the call until either:
    // 1. The call succeeds
    // 2. The deadline is reached
    // 3. The caller canister enters the stopping state
    // 4. A non-retryable error occurs
    call_idempotent_method_with_retry(
        call,
        &mut when_out_of_time_or_stopping(&deadline),
    )
    .await
    .map_err(|e| format!("Call failed: {:?}", e))?;

    Ok(())
}

§Note

Retrying indefinitely is not recommended, as this can make your canister unupgradable. For example, the following are safe to use:

  • A time-based deadline (Deadline::TimeOrStopping)
  • A stopping-based deadline (Deadline::Stopping)
  • A maximum number of retries (max_retries)

Enums§

Deadline
Represents a deadline for retrying calls.
ErrorCause
Represents the cause of a retry error.
RetryError
An error type for retried calls.

Functions§

call_idempotent_method_with_retry
Makes and, in case of failure, retries an idempotent call until instructed otherwise
call_nonidempotent_method_with_retry
Makes and, in case of failure, retries a non-idempotent call until instructed otherwise
when_max_retries_reached
Returns a function that retries up to the specified number of times.
when_out_of_time_or_stopping
Returns a function that determines whether to stop retrying based on the deadline.