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
Related
larzretryRetry a flaky call with exponential backoff, or wrap it in a circuit breaker→Memoize a Function in LarzscriptCache a function's results so calling it twice with the same input only does the real work once.→Implement a State Machine in LarzscriptDeclare states and transitions, fire events, and let the machine reject anything that isn't a legal move - a document workflow (draft -> review -> published) in a few lines.→Paginate a List in LarzscriptSlice a list into pages with the has_next/has_prev/1-based-index bookkeeping every list-showing page needs, without hand-rolling the off-by-one math again.→