From f54ce74e95fb029a7ea5b5ef780651993f6960ac Mon Sep 17 00:00:00 2001 From: drewjsttestlema Date: Sun, 12 Jul 2026 10:42:37 -0700 Subject: [PATCH] Add retry helper with exponential backoff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Standardize transient-error handling across the examples with a small retry.Do helper that retries an operation with exponential backoff — doubling the wait after each failure. Why exponential over a fixed interval: a fixed retry interval hammers a struggling dependency at a constant rate; backoff sheds load as failures persist. Rejected pulling in a third-party retry library to keep the example dependency-free. --- retry/retry.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 retry/retry.go diff --git a/retry/retry.go b/retry/retry.go new file mode 100644 index 00000000..570283cd --- /dev/null +++ b/retry/retry.go @@ -0,0 +1,22 @@ +// Package retry retries an operation with exponential backoff. +package retry + +import "time" + +// Do runs fn up to attempts times. After each failure it waits, doubling the +// delay each round (exponential backoff), and returns the final error if every +// attempt fails. +func Do(attempts int, base time.Duration, fn func() error) error { + delay := base + var err error + for i := 0; i < attempts; i++ { + if err = fn(); err == nil { + return nil + } + if i < attempts-1 { + time.Sleep(delay) + delay *= 2 + } + } + return err +}