本文介绍了http请求会自动重试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GoLang将数据推送到apache服务器.假设我的apache服务器暂时停止了.然后,我的http请求将自动重试.我正在使用这句话

I am trying to push my data to apache server using GoLang. Suppose my apache server is temporarily stopped. Then will my http request retry automatically.I am using this statement

resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return errors.Wrap(err, "http request error")
    }

我无法继续进行调查,因为我的死刑被卡在这里了.而且我反复出现此错误.

I am unable to proceed further coz what is think is my execution is stuck here. And I am repeatedly getting this error.

推荐答案

否,您将需要实现自己的重试方法,这是一个基本示例,可以使您有所了解:

No, you will need to implement your own retry method, this is a basic example that could give you an idea:

https://play.golang.org/p/_o5AgePDEXq

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

func main() {
    var (
        err      error
        response *http.Response
        retries  int = 3
    )
    for retries > 0 {
        response, err = http.Get("https://non-existent")
        // response, err = http.Get("https://google.com/robots.txt")
        if err != nil {
            log.Println(err)
            retries -= 1
        } else {
            break
        }
    }
    if response != nil {
        defer response.Body.Close()
        data, err := ioutil.ReadAll(response.Body)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("data = %s\n", data)
    }
}

这篇关于http请求会自动重试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:18