GitHub - codeGROOVE-dev/retry: Simple battle-hardened retry library for Go. (original) (raw)

retry: Production Retry Logic for Go

Release Software License Go Report Card Go Reference

Modern fork of avast/retry-go/v4 focused on correctness, reliability and efficiency. 100% API-compatible drop-in replacement.

Production guarantees:

Quick Start

Installation

go get github.com/codeGROOVE-dev/retry

Simple Retry

// Retry a flaky operation up to 10 times (default) err := retry.Do(func() error { return doSomethingFlaky() })

Retry with Custom Attempts

// Retry up to 5 times with exponential backoff err := retry.Do( func() error { resp, err := http.Get("https://api.example.com/data") if err != nil { return err } defer resp.Body.Close() return nil }, retry.Attempts(5), )

Overly-complicated production configuration

// Overly-complex production pattern: bounded retries with circuit breaking err := retry.Do( func() error { return processPayment(ctx, req) }, retry.Attempts(3), // Hard limit retry.Context(ctx), // Respect cancellation retry.MaxDelay(10*time.Second), // Cap backoff retry.AttemptsForError(0, ErrRateLimit), // Stop on rate limit retry.OnRetry(func(n uint, err error) { log.Printf("retry attempt %d: %v", n, err) }), retry.RetryIf(func(err error) bool { // Only retry on network errors var netErr net.Error return errors.As(err, &netErr) && netErr.Temporary() }), )

Preventing Cascading Failures

// Stop retry storms with Unrecoverable if errors.Is(err, context.DeadlineExceeded) { return retry.Unrecoverable(err) // Don't retry timeouts }

// Per-error type limits prevent thundering herd retry.AttemptsForError(0, ErrCircuitOpen) // Fail fast on circuit breaker retry.AttemptsForError(1, sql.ErrTxDone) // One retry for tx errors retry.AttemptsForError(5, ErrServiceUnavailable) // More retries for 503s

Changes from avast/retry-go/v4

This fork will always be a 100% compatible drop-in replacement. There are some minor tweaks that have been made though:

New APIs added:

Safety improvements:

Documentation