如何重试HTTP POST请求

如何重试HTTP POST请求,http,go,retry-logic,Http,Go,Retry Logic,我已经实现了以下代码,用于在Go中重试http post请求 在第二次重试尝试中,我总是将请求主体设置为null。我已经尝试了defer req.body.close(),但它不起作用。有谁能帮我解决这个问题吗 func httpRetry(req *http.Request, timeOut time.Duration, retryAttempts int, retryDelay time.Duration) (*http.Response, error) { attempts :=

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

在第二次重试尝试中,我总是将请求主体设置为null。我已经尝试了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

}
0x5ec890 170[]假映射[]映射[]映射[]映射[]
}

尝试-2
ret 88888和{邮政编码https://localhost:8080/logs/ HTTP/1.1 1 1映射[]{}0x5ec890 170[]假映射[]映射[]映射[]
}


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

文件的相关部分:

往返必须始终关闭主体,包括错误,但 根据实现的不同,甚至可以在单独的goroutine中这样做 往返后返回。这意味着想要重用 后续请求的主体必须安排等待结束呼叫 在这样做之前

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

如果请求没有正文,只要 在往返失败或 回应,尸体关上了

摘自

因此,只要您确定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 ...

    }

    // ...
}

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

文件的相关部分:

往返必须始终关闭主体,包括错误,但 根据实现的不同,甚至可以在单独的goroutine中这样做 往返后返回。这意味着想要重用 后续请求的主体必须安排等待结束呼叫 在这样做之前

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

如果请求没有正文,只要 在往返失败或 回应,尸体关上了

摘自

因此,只要您确定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 ...

    }

    // ...
}

您的
defer req.Body.Close()
仅在函数返回时被激发,而不是在循环重新运行时被激发。您只能发送一次请求。您必须为每次尝试创建一个新的请求(或者至少重新创建主体)。您不应该为每次循环迭代创建一个新的
传输
客户端
——事实上,很可能,除非您实际需要不同的配置,否则您应该在整个应用程序中使用相同的
Transport
Client
实例。您的
defer req.Body.Close()
仅在函数返回时启动,而不是在循环重新运行时启动。您只能发送一次请求。您必须为每次尝试创建一个新的请求(或者至少重新创建主体)。您不应该为每次循环迭代创建一个新的
传输
客户端
——事实上,很可能,除非您实际需要不同的配置,否则您应该在整个应用程序中使用相同的
传输
客户端
实例。谢谢@mkopriva。它起作用了。我对处理nil body.if req.body做了以下更改!=nil{data,err=ioutil.ReadAll(req.Body)如果err!=nil{data=[]byte(“”)fmt.Println(“Input”,string(data))}//关闭原始主体,如果req.Body!=nil{if err:=req.Body.Close();err!=nil{return nil,err}}谢谢@mkopriva。它起作用了。我对处理nil body.if req.body做了以下更改!=nil{data,err=ioutil.ReadAll(req.Body)如果err!=nil{data=[]byte(“”)fmt.Println(“Input”,string(data))}//关闭原始主体,如果req.Body!=nil{if err:=req.Body.Close();err!=nil{return nil,err}
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 ...

    }

    // ...
}