【问题标题】:How to retry HTTP POST requests如何重试 HTTP POST 请求
【发布时间】:2019-02-15 07:23:42
【问题描述】:

我已经实现了以下代码,用于在 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

}

请在下方查看错误详情

Attempt -  1
ret 88888  &{POST https://localhost:8080/logs/ HTTP/1.1 1 1 map[] {{"converstnId":"","boId":"","msgId":"","serviceName":"","headers":"","properties":"","message":"","body":"aa","exceptionMessage":"","logType":"","exceptionStackTrace":""}}

0x5ec890 170 [] 假地图[] 地图[] 地图[]
}

Attempt -  2
ret 88888  &{POST https://localhost:8080/logs/ HTTP/1.1 1 1 map[] {} 0x5ec890 170 [] false  map[] map[] <nil> map[]  

}

【问题讨论】:

  • 你的 defer req.Body.Close() 被触发,然后函数返回,然后你的循环重新运行。
  • 您只能发送一次请求。您必须为每次尝试创建一个新请求(或至少重新创建主体)。
  • 您不应该为每个循环迭代创建一个新的 TransportClient - 事实上,您很可能应该在整个循环中使用相同的 TransportClient 实例整个应用程序,除非您确实需要不同的配置。

标签: http go retry-logic


【解决方案1】:

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

文档的相关部分:

RoundTrip 必须始终关闭主体,包括错误时,但 取决于实现,甚至可以在单独的 goroutine 中这样做 RoundTrip 返回后。这意味着调用者想要重用 后续请求的正文必须安排等待关闭调用 在这样做之前。

当重用没有正文的请求时,比如 GET,你仍然需要小心:

如果请求没有正文,则可以重复使用,只要 调用者不会改变请求,直到 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 ...

    }

    // ...
}

【讨论】:

  • 谢谢@mkopriva。有用。我做了以下更改来处理 nil body.if req.Body != nil { data, err = ioutil.ReadAll(req.Body) if err != nil { data = []byte("") } fmt.Println( "Input", string(data)) } // 关闭原来的 body,我们不再需要它 if req.Body != nil { if err := req.Body.Close(); err != nil { return nil, err } }
猜你喜欢
  • 1970-01-01
  • 2019-08-24
  • 1970-01-01
  • 2016-06-12
  • 2023-03-22
  • 2014-05-05
  • 1970-01-01
  • 1970-01-01
  • 2016-04-19
相关资源
最近更新 更多