#call #computer #retry #deadlines #time

ic-call-retry

Retries for inter-canister calls on the Internet Computer

1 unstable release

new 0.1.0 May 7, 2025

#354 in Magic Beans


Used in ic-safe-upgrades

MIT/Apache

15KB
165 lines

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)

Dependencies

~1.7–9.5MB
~89K SLoC