Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ShouldRetryRespError = errors.New("should retry response error")
ShouldRetryRespError is returned when a response indicates the request should be retried.
Functions ¶
This section is empty.
Types ¶
type BackOffPolicy ¶
type BackOffPolicy struct {
MaxRetries uint64
}
BackOffPolicy represents the maximum number of retries for a backoff policy.
type NotifyFunc ¶
NotifyFunc represents a function that notifies about errors and durations during retries.
type RoundTripper ¶
type RoundTripper struct {
// contains filtered or unexported fields
}
RoundTripper provides a retryable HTTP transport mechanism.
func New ¶
func New(roundTripper http.RoundTripper, shouldRetryFunc ShouldRetryFunc, notifyFunc NotifyFunc, backOffPolicy *BackOffPolicy) *RoundTripper
New creates a new RoundTripper with the provided parameters. If roundTripper is nil, http.DefaultTransport is used. If backOffPolicy is nil, a default policy with MaxRetries set to 3 is used.
Example ¶
package main import ( "context" "errors" "fmt" "net/http" "syscall" "time" "github.com/linzhengen/retryabletransport" ) func main() { client := &http.Client{ Transport: retryabletransport.New( http.DefaultTransport, func(req *http.Request, resp *http.Response, err error) bool { if errors.Is(err, syscall.ECONNRESET) { return true } if resp != nil && resp.StatusCode == http.StatusTooManyRequests { return true } return false }, func(ctx context.Context, err error, duration time.Duration) { fmt.Printf("retry http request, err: %v, duration: %v", err, duration) }, &retryabletransport.BackOffPolicy{ MaxRetries: 3, }, ), Timeout: 3 * time.Second, } _, err := client.Get("http://example.com") if err != nil { fmt.Println(err) } }
Output:
Click to show internal directories.
Click to hide internal directories.