首页 > 解决方案 > HTTP 客户端返回结果为 null

问题描述

我们在负载均衡器和其他东西下在生产中运行核心 go 服务器。

现在,当 goroutine 计数(带有 pprof 跟踪)超过 2500 时,我们的 HTTP 客户端将返回 nil 作为结果。

PPROD 跟踪

这是我的 HTTP 客户端代码。

client := &http.Client{
Timeout:   time.Second * 130,
Transport: netTransport,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
    return http.ErrUseLastResponse
},
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
go func() {
    select {
    case <-ctx.Done():
        log.Error(ctx.Err()) // prints "context deadline exceeded"
        return
    }
}()
vars := mux.Vars(r)
validateTokenURL = authURL + "/" + vars["resource"] + "/authorized" + "?url=" + path
req, _ := http.NewRequest("GET", validateTokenURL, nil)
req.Header = r.Header
req = req.WithContext(ctx)
res, error := client.Do(req)

if error != nil {
    log.Error("The group's number increased tremendously!")
    return nil, nil, false
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
    log.Error(err.Error())
}
json.Unmarshal(body, &responseBody)
res.Body.Close()
if _, ok := responseBody["data"]; ok && strings.Compare(reflect.TypeOf(responseBody["data"]).String(), "string") == 0 && strings.Compare(responseBody["data"].(string), "ok") == 0 {
    return responseBody, res, true
}
return responseBody, res, false
}

我已将 ulimit 设置为大约 8 GB,另外还有 16 GB 作为 RAM。内存使用率低于 20%。

谁能帮助我们弄清楚我们缺少什么?

标签: gohttpclient

解决方案


推荐阅读