http2:服务器发送GOAWAY并关闭连接;LastStreamID=1999

Gra*_*der 6 http go

我有 for 循环,我在其中调用从 osrm 服务器获取响应的函数,一段时间后 ioutil.ReadAll(resp.Body) 返回打印的 err http2: server sent GOAWAY and closed the connection; LastStreamID=1999, ErrCode=NO_ERROR, debug=""

func RequestGET(req string) []byte {

    reqst, err := http.NewRequest("GET", req, nil)
    client := &http.Client{}
    resp, err := client.Do(reqst)
    if err != nil {
        panic(err)
    }
    resp_data, err := ioutil.ReadAll(resp.Body)
    resp.Body.Close()
    if err != nil {
        fmt.Println(err)
    }
    return resp_data
}
Run Code Online (Sandbox Code Playgroud)

不是 resp.Body.Close() 关闭连接吗?我希望每次都能得到一个新的。

Jim*_*imB 6

服务器由于某种原因正在关闭连接;后端关闭、超时等。除了重试,您无能为力,就像您遇到任何其他连接错误一样。在 arelated issue ( https://golang.org/issue/18639 ) 中有一些关于自动重试的讨论,但通常看起来客户端正在按预期工作。

Response.Body.Close不关闭连接,否则会破坏 http1.1 和 http2 使用持久连接的目的。读取和关闭响应正文是允许 http 客户端重用连接的方式。