OSP logo
os://protocol

Retry

Retry mechanisms for failed operations

Overview

The retry system provides configurable retry behavior for failed operations. It supports multiple backoff strategies, conditional retries, and callbacks for monitoring retry attempts.

Backoff Strategies

StrategyDescription
noneNo delay increase between retries
linearDelay increases linearly (delay * attempt)
exponentialDelay doubles each attempt (delay * 2^attempt)

TypeScript API

import type { Retry, Backoff } from '@osprotocol/schema/runs/retry'

Backoff

Available backoff strategies for retry delays.

type Backoff = 'none' | 'linear' | 'exponential'

Retry

Retry configuration for workflow runs.

interface Retry {
  /** Maximum number of retry attempts */
  attempts: number
  /** Initial delay between retries in milliseconds */
  delayMs: number
  /** Backoff strategy (default: 'none') */
  backoff?: Backoff
  /** Maximum delay when using backoff (milliseconds) */
  maxDelayMs?: number
  /** Callback on each retry attempt */
  onRetry?: (error: Error, attempt: number) => void
  /** Optional predicate to determine if error is retryable */
  shouldRetry?: (error: Error) => boolean
}

Usage Examples

Simple Retry

const retry: Retry = {
  attempts: 3,
  delayMs: 1000
}
// Retries up to 3 times with 1 second between each attempt

Exponential Backoff

const retry: Retry = {
  attempts: 5,
  delayMs: 100,
  backoff: 'exponential',
  maxDelayMs: 10000,
  onRetry: (error, attempt) => {
    console.log(`Retry ${attempt}: ${error.message}`)
  }
}
// Delays: 100ms, 200ms, 400ms, 800ms, 1600ms (capped at 10000ms)

Conditional Retry

const retry: Retry = {
  attempts: 3,
  delayMs: 500,
  shouldRetry: (error) => {
    // Only retry network errors
    return error.name === 'NetworkError'
  }
}

Delay Calculation

StrategyAttempt 1Attempt 2Attempt 3Attempt 4
nonedelayMsdelayMsdelayMsdelayMs
lineardelayMs2 * delayMs3 * delayMs4 * delayMs
exponentialdelayMs2 * delayMs4 * delayMs8 * delayMs

Integration

Retry integrates with:

  • RunOptions: Configure retry behavior for runs
  • Timeout: Retries respect timeout constraints
  • Cancel: Pending retries can be cancelled