App patterns · real, tested code

Retry with Exponential Backoff in Larzscript

Retry a flaky call a few times, waiting longer between each attempt, instead of failing on the first hiccup or hammering a struggling service.

retry.call() takes max_attempts/base_delay/factor, and a sleep_fn you can swap for a no-op or a recording stub in tests so retries don't actually wait - this example injects a no-op sleep so the doubling backoff itself is provably exercised without the page waiting on real seconds.

larzscript pkg install retry

Code

import "retry" as retry

let attempts = 0
fn flaky() {
  attempts = attempts + 1
  if attempts < 3 { throw {"type": "LarzRuntimeError", "message": "not yet"} }
  return "ok on attempt " + str(attempts)
}
print(retry.call(flaky, {"max_attempts": 5, "base_delay": 0, "sleep_fn": fn(s) { return nil }}))
Output
ok on attempt 3