retryTransport

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorChecker

type ErrorChecker func(resp *http.Response, err error) error

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) RoundTrip

func (r *RetryTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip 方法实现了 http.RoundTripper 接口,用于发送 HTTP 请求

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL