feat: add backoff package for retry logic and implement example usage in new backoff-test script
This commit is contained in:
58
scripts/backoff-test/main.go
Normal file
58
scripts/backoff-test/main.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/cenkalti/backoff/v5"
|
||||
)
|
||||
|
||||
func ExampleRetry() {
|
||||
// Define an operation function that returns a value and an error.
|
||||
// The value can be any type.
|
||||
// We'll pass this operation to Retry function.
|
||||
operation := func() (string, error) {
|
||||
// An example request that may fail.
|
||||
resp, err := http.Get("http://httpbin.org/get")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// If we are being rate limited, return a RetryAfter to specify how long to wait.
|
||||
// This will also reset the backoff policy.
|
||||
if resp.StatusCode == 429 {
|
||||
seconds, err := strconv.ParseInt(resp.Header.Get("Retry-After"), 10, 64)
|
||||
if err == nil {
|
||||
return "", backoff.RetryAfter(int(seconds))
|
||||
}
|
||||
}
|
||||
|
||||
// In case of non-retriable error, return Permanent error to stop retrying.
|
||||
// For this HTTP example, client errors are non-retriable.
|
||||
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
|
||||
return "", backoff.Permanent(errors.New("bad request"))
|
||||
}
|
||||
|
||||
// Return successful response.
|
||||
return "hello", nil
|
||||
}
|
||||
|
||||
result, err := backoff.Retry(context.TODO(), operation, backoff.WithBackOff(backoff.NewExponentialBackOff()))
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Operation is successful after retries.
|
||||
|
||||
fmt.Println(result)
|
||||
// Output: hello
|
||||
}
|
||||
|
||||
func main() {
|
||||
ExampleRetry()
|
||||
}
|
||||
Reference in New Issue
Block a user