Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrorChecker ¶
ErrorChecker 应该被设计为无状态的, 因为它有可能被多个goroutine并发调用. 当发生error时, 无需手动关闭response.Body. 它由RetryTransport实例负责关闭.
var DefaultErrorCheck ErrorChecker = func(resp *http.Response, err error) error { if err != nil { if errors.Is(err, context.Canceled) { return nil } return tracerr.Wrap(err) } if resp.StatusCode >= http.StatusBadRequest { switch resp.StatusCode { case http.StatusTooManyRequests: return nil } return tracerr.Errorf("http status code: %d", resp.StatusCode) } return nil }
DefaultErrorCheck 当发生以下情况下不进行重试: 1. 错误为上下文取消或超时时 2. 请求状态码小于400时 3. 请求状态码为429时
type RetryTransport ¶
type RetryTransport struct {
// contains filtered or unexported fields
}
func New ¶
func New(roundTripper http.RoundTripper, retryMax int) *RetryTransport
New 创建带重试机制的Transport. roundTripper 为nil时, 使用 http.DefaultTransport. retryMax 小于等于0时, 使用 gretry.DefaultRetryMax. ErrorChecker使用 DefaultErrorCheck, 需要自定义请使用 RetryTransport.SetErrorChecker. DelayCalculator使用 gretry.DefaultDelayCalculator, 需要自定义请使用 RetryTransport.SetDelayCalculator.
func (*RetryTransport) SetDelayCalculator ¶
func (r *RetryTransport) SetDelayCalculator(calculator gretry.DelayCalculator)
SetDelayCalculator 设置 gretry.DelayCalculator, 为nil时, 使用 gretry.DefaultDelayCalculator.
func (*RetryTransport) SetErrorChecker ¶
func (r *RetryTransport) SetErrorChecker(check ErrorChecker)
SetErrorChecker 设置 ErrorChecker, 为nil时, 使用 DefaultErrorCheck.
Click to show internal directories.
Click to hide internal directories.