How do I retry requests with exponential backoff in Go?

In Go, you can implement retry logic with exponential backoff for making requests to an external service. This approach helps to manage transient errors and avoid overwhelming a service with rapid retry attempts.

The basic idea is to delay subsequent retry attempts exponentially, which means you wait longer after each failure. Here's a simple example of how to implement this strategy in Go:

package main import ( "fmt" "net/http" "time" "math/rand" ) func main() { url := "http://example.com/api" maxRetries := 5 backoffFactor := 2.0 for i := 0; i < maxRetries; i++ { response, err := http.Get(url) if err != nil { // Log error (for demonstration purpose, we just print) fmt.Printf("Attempt %d failed: %v\n", i+1, err) // Calculate backoff duration waitTime := time.Duration(rand.Intn(int(math.Pow(backoffFactor, float64(i)) * 1000))) fmt.Printf("Waiting for %v before retrying...\n", waitTime) time.Sleep(waitTime * time.Millisecond) continue } // If request succeeds, break the loop defer response.Body.Close() fmt.Printf("Success on attempt %d!\n", i+1) break } }

Go exponential backoff retry requests error handling http requests programming.