Welcome to the new Golem Cloud Docs! 👋
Documentation
Rust Language Guide
Retries

Control the retry policy from Rust

Using Golem's retry mechanism

Golem applies a retry mechanism to all workers. In case of a failure, Golem will automatically recover the worker to the point before the failure and retry the operation. An exponential backoff and an upper limit on the number of retries are applied.

If the maximum number of retries is reached, the worker will be marked as failed and no further invocations will be possible on it.

This mechanism is automatic and applied to all kind of failures. To rely on it, just let the Rust code panic.

Customizing the retry policy

The retry policy which controls the maximum number of retries and the exponential backoff is a global configuration of the Golem servers, but it can be customized for each worker.

For Rust we need to use the golem-rust crate to do so. First add it to the dependencies in your Cargo.toml:

[dependencies]
golem-rust = { version = "0.3.0" }
golem-rust-macro = { version = "0.3.0" }

Then use the use_retry_policy or with_retry_policy functions to temporarily change the retry policy, either until the returned guard is dropped (the use_ variant) or for the provided closure (the with_ variant).

let _guard = use_retry_policy(RetryPolicy {
            max_attempts: max_retries as u32,
            min_delay: 1000000000, // 1s
            max_delay: 1000000000, // 1s
            multiplier: 1,
        });
// ...
 
// or
 
with_retry_policy(RetryPolicy {
            max_attempts: max_retries as u32,
            min_delay: 1000000000, // 1s
            max_delay: 1000000000, // 1s
            multiplier: 1,
        }, || {
    // ...
});

The RetryPolicy type itself is originatd from Golem's WIT definition in the following way:

/// Configures how the executor retries failures
record retry-policy {
    /// The maximum number of retries before the worker becomes permanently failed
    max-attempts: u32,
    /// The minimum delay between retries (applied to the first retry)
    min-delay: duration,
    /// The maximum delay between retries
    max-delay: duration,
    /// Multiplier applied to the delay on each retry to implement exponential backoff
    multiplier: u32
}