为了账号安全,请及时绑定邮箱和手机立即绑定

http 重试请求超时 (408)

http 重试请求超时 (408)

Go
长风秋雁 2022-09-19 10:39:46
使用哈希科普库 (https://github.com/hashicorp/go-retryablehttpgo-retryablehttp)它会自动对所有代码重试:5xx可重试http 在某些条件下执行自动重试。主要是,如果客户端返回错误(连接错误等),或者如果收到500范围的响应代码(501除外),则在等待一段时间后调用重试。否则,将返回响应并留给调用方进行解释。这是否可能重试,例如在http状态代码上只是ootb?或者我应该构建一些自定义包装器?Request Timeout408
查看完整描述

2 回答

?
largeQ

TA贡献2039条经验 获得超7个赞

您可以实现自己的重试策略,并将其传递到“客户端”字段。

文档参考:

代码参考:

代码可能类似于

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

    }

}


查看完整回答
反对 回复 2022-09-19
?
HUWWW

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)


查看完整回答
反对 回复 2022-09-19
  • 2 回答
  • 0 关注
  • 133 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信