duangangpin078794 2019-09-05 14:46
浏览 38

连接拒绝后,Go程序退出

I'm Sending get requests to websites and get the statuscodes inside a loop, when the loop sends get request to a non-exist website, it breaks the program.

go version go1.12.6 darwin/amd64

func getRequest(url string) int {

    http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}

    resp, err := http.Get(url)
    if err != nil {
        log.Fatalln(err)
    }

    defer resp.Body.Close()

    return resp.StatusCode
} 

func checker(info []accountinfo) {
    var request string
    var statusCode int

    for i := range info {
        request = fmt.Sprintf("https://%s/", info[i].domain)
        statusCode = getRequest(request)
        if statusCode == 200 {
            fmt.Println("That's ok")
        } else {
            fmt.Println(info[i].domain, "not found or content is not valid")
        }

    }
}

I expect it print "something.com not found or content is not valid". But it throws the error below.

2019/09/05 17:19:43 Get https://something.com/: dial tcp ip:443: connect: connection refused
exit status 1

EDIT

Using Println instead of Fatalln gives another error:

2019/09/05 17:52:18 Get https://something.com/: dial tcp ip:443: connect: connection refused
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x124eebf]

goroutine 1 [running]:
main.getRequest(0xc0000ae0e0, 0x1a, 0x0)
    /Users/ozan/monitoring-development/v2/main.go:147 +0xff
main.checker(0xc000584000, 0x8d, 0x100)
    /Users/ozan/monitoring-development/v2/main.go:110 +0xd3
main.main()
    /Users/ozan/monitoring-development/v2/main.go:67 +0x4dd
exit status 2
  • 写回答

1条回答 默认 最新

  • donglu4159 2019-09-05 14:50
    关注

    log.Fatalln, as its name implies and documentation clearly states, prints the error and exits the program. If you don't want to exit, use log.Println instead.

    Your logic for the "not found or not valid" error is based on status codes, but in order to get a status code, the request must complete. A request cannot complete to a host that doesn't exist or refuses connections. So you need to consider two types of "failure" cases:

    1. A transport-level error, like DNS failure, connection refused/timed out, etc.
    2. A protocol-level error, like HTTP status 500, etc.

    Also note that you're treating only status 200 as success; this may be what's intended, it may not, as the entire 2xx block of status codes are success codes (e.g. 204 No Content is a common success response).

    You likely want something like this:

    resp, err := http.Get(url)
    if err != nil {
        log.Println(err) // Assuming you want to see what the error actually was
        return -1    // Not perfect, but at least the caller will treat it as a "non-success" status code and give your expected result
    }
    

    A better implementation would be to return the status code and an error and handle both correctly in checker.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大