2 回答

TA貢獻2039條經驗 獲得超8個贊
您可以實現自己的重試策略,并將其傳遞到“客戶端”字段。
文檔參考:
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 關注
- 238 瀏覽
添加回答
舉報