2 回答
TA贡献2039条经验 获得超7个赞
您可以实现自己的重试策略,并将其传递到“客户端”字段。
文档参考:
https://pkg.go.dev/github.com/hashicorp/go-retryablehttp?utm_source=godoc#Client
https://pkg.go.dev/github.com/hashicorp/go-retryablehttp?utm_source=godoc#DefaultRetryPolicy
代码参考:
代码可能类似于
package main
import (
"context"
"net/http"
"github.com/hashicorp/go-retryablehttp"
)
func main() {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 10
retryClient.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
ok, e := retryablehttp.DefaultRetryPolicy(ctx, resp, err)
if !ok && resp.StatusCode == http.StatusRequestTimeout {
return true, nil
// return true for a retry,
// if e is nil,
// you might want to populate that error
// to propagate it.
// see https://github.com/hashicorp/go-retryablehttp/blob/02c1586c8f14be23e7eeb522f1094afbabf45e93/client.go#L673
}
return ok, e
}
}
TA贡献1874条经验 获得超12个赞
正如源代码在文件 client.go 的第 354 行中指定的那样,您可以将该函数配置为在任何自定义方案中重试。CheckRetry
// CheckRetry specifies the policy for handling retries, and is called
// after each request. The default policy is DefaultRetryPolicy.
CheckRetry CheckRetry
您只需要在下面的类型中编写一个函数,并使用该自定义实现进行配置。retryablehttp.Client.CheckRetry
type CheckRetry func(ctx context.Context, resp *http.Response, err error) (bool, error)
- 2 回答
- 0 关注
- 185 浏览
添加回答
举报