登录
首页 >  Golang >  Go问答

如何重试 HTTP POST 请求

来源:stackoverflow

时间:2024-03-28 08:42:29 339浏览 收藏

golang学习网今天将给大家带来《如何重试 HTTP POST 请求》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!

问题内容

我已经实现了以下代码,用于在 go 中重试 http post 请求。

在第二次重试尝试中,我总是将请求正文设置为空。我尝试过 defer req.body.close() 但它不起作用。有人可以帮我解决这个问题吗?

func httpRetry(req *http.Request, timeOut time.Duration, retryAttempts int, retryDelay time.Duration) (*http.Response, error) {

    attempts := 0

    for {
        attempts++
        fmt.Println("Attempt - ", attempts)
        statusCode := 0

        tr := &http.Transport{
            TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
        }
        var netClient = &http.Client{
            Timeout:   time.Second * timeOut,
            Transport: tr,
        }
        fmt.Println("ret 88888 ", req)
        response, err := netClient.Do(req)

        //defer req.Body.Close()

        fmt.Println(response, "  dd", err)

        if response != nil {
            fmt.Println(response.StatusCode)
            statusCode = response.StatusCode
        }

        if err != nil {
            fmt.Println(err)
            //return response, err
        }

        if err == nil && statusCode == http.StatusOK {
            return response, nil
        }

        if err == nil && response != nil {
            defer req.Body.Close()
        }

        retry := retryDecision(statusCode, attempts, retryAttempts)
        if !retry {
            return response, err
        }

        fmt.Println("Retry Attempt number", attempts)
        time.Sleep(retryDelay * time.Second)
        fmt.Println("After Delay")
    }
}

func retryDecision(responseStatusCode int, attempts int, retryAttempts int) bool {

    retry := false
    fmt.Println("Retry Decision 0 ", responseStatusCode, attempts, retryAttempts)
    errorCodeForRetry :=
        []int{http.StatusInternalServerError, http.StatusUnauthorized, http.StatusNotImplemented, http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout}

    for _, code := range errorCodeForRetry {
        if code == responseStatusCode && attempts <= retryAttempts {
            retry = true
        }
    }

    fmt.Println("Retry Decision ", retry)

    return retry

}

请在下面查找错误详细信息

尝试 - 1 ret 88888 &{post https://localhost:8080/logs/ http/1.1 1 1 地图[] {{"converstnid":"","boid":"","msgid":"","servicename": “”,“标题”:“”,“属性”:“”,“消息”:“”,“正文”:“aa”,“异常消息”:“”,“日志类型”:“”,“异常stacktrace”: “”}} 0x5ec890 170 [] 错误 地图[] 地图[] 地图[] } 尝试 - 2 ret 88888 &{post https://localhost:8080/logs/ http/1.1 1 1 地图[] {} 0x5ec890 170 [] false 地图[] 地图[] 地图[] }


解决方案


为了能够重用正文非零的请求,您首先需要确保正文已被关闭,不是由您关闭,而是由客户端的 RoundTripper 关闭。

文档的相关部分:

roundtrip 必须始终关闭主体,包括出现错误时,但是 根据实现情况,甚至可以在单独的 goroutine 中执行此操作 roundtrip 返回后。这意味着想要重用的调用者 后续请求的主体必须安排等待 close 调用 在此之前。

当重用没有正文的请求(例如 get)时,您仍然需要小心:

如果请求没有body,只要 调用者不会改变请求,直到 roundtrip 失败或 response.body 已关闭。

取自 here

因此,只要您确定 roundtripper 关闭了主体,您可以做的就是在每次迭代的顶部重置请求的主体。像这样的事情:

func httpRetry(req *http.Request, timeOut time.Duration, retryAttempts int, retryDelay time.Duration) (*http.Response, error) {

    // ...

    data, err := ioutil.ReadAll(req.Body)
    if err != nil {
        return nil, err
    }

    // close the original body, we don't need it anymore
    if err := req.Body.Close(); err != nil {
        return err
    }

    for {

        req.Body = ioutil.NopCloser(bytes.NewReader(data)) // reset the body

        // ... your code ...

    }

    // ...
}

今天关于《如何重试 HTTP POST 请求》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>