Skip to content

Retry a block

Re-run a block with exponential backoff until it succeeds or hits a limit.

18th August 2026

def retry_call(attempts: 5, delay: 1.0)
  current = delay
  tries = 0
  begin
    yield
  rescue StandardError
    tries += 1
    raise if tries >= attempts

    sleep current
    current *= 2
    retry
  end
end

Caveats

Not for blocks that are not idempotent.