Welcome to the new Golem Cloud Docs! 👋
Documentation
Experimental Languages
TypeScript
Retries

Control the retry policy from TypeScript

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 throw an unhandled exception.

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.

The golem-ts library exports the withRetryPolicy function to temporarily change the retry policy:

import { withRetryPolicy } from "@golemcloud/golem-ts"
 
// Durations are expected as nanoseconds
const result: string = withRetryPolicy(
  {
    maxAttempts: 3,
    minDelay: BigInt(100 * 1000 * 1000), // 100 milliseconds
    maxDelay: BigInt(2 * 1000 * 1000 * 1000), // 2 seconds
    multiplier: 1.5,
  },
  () => {
    // this closure runs with the custom retry policy
    return "hello"
  }
)

The RetryPolicy interface required by withRetryPolicy is generated from Golem's WIT definition:

/// 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: f64
}