我有一个客户端和两个服务器(golang中的两个服务器)。所有解决方案都涉及从一台服务器向另一台服务器发送多个请求。但是我正在尝试实现这种通信:
Client Server A Server B
Request1 --> Received
Request 2 --> Received
Received <-- Request 3
Response 3 --> Received
Received <-- Response 2
Received <-- Response 1
但是,我在对请求2的响应中始终遇到EOF错误。我已经阅读了有关该主题的几篇文章,并尝试了所有可能的解决方案,但没有任何解决方案可以解决此问题。这是我编写的用于发送POST请求的代码。服务器A和服务器B都调用相同的代码。
func SendPost(url string, insecureSkipVerify bool, authHeader *CAuthHeader, contentType string, otherHeaders map[string]string, body string, v interface{}) (string, error) {
bBody := strings.NewReader(body)
req, err := http.NewRequest("POST", url, bBody)
if err != nil {
return "", utilities.GetError("01: ", err.Error())
}
req.Close = true
if authHeader != nil {
req.Header.Set(authHeader.GetHeaderStrings())
}
req.Header.Set(HEADERKEY_CONTENTTYPE, contentType)
for k, v := range otherHeaders {
req.Header.Set(k, v)
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify},
}
utilities.DebugLog("-------> SENDING POST: %s", url)
netClient := &http.Client{Timeout: 40 * time.Second, Transport: tr}
resp, err := netClient.Do(req)
if err != nil {
// GETTING ERROR LOG HERE IN REQUEST 2
utilities.DebugLog("<------- RECEIVED POST: %s, with error: %s", url, err.Error())
return "", utilities.GetError("02: ", err.Error())
}
defer resp.Body.Close()
utilities.DebugLog("<------- RECEIVED POST: %s", url)
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", utilities.GetError("03: ", err.Error())
}
utilities.DebugLog("%s", string(b))
// MAY IGNORE THIS, JUST POPULATES THE RESPONSE INTO THE interface() PASSED AS A PARAMETER TO THIS FUNCTION
if err := populateInterface(b, v); err != nil {
return string(b), err
}
utilities.DebugLog("Received body: %s", string(b))
return string(b), nil
}
我在服务器A上获得的日志是:
2019-02-21 18:33:57.579758 +0530 IST m=+80.025904414: -------> SENDING POST: http://localhost:7350/v2/rpc/refreshPublicProfile?http_key=defaultkey
2019-02-21 18:34:13.517802 +0530 IST m=+95.963470623: <------- RECEIVED POST: http://localhost:7350/v2/rpc/refreshPublicProfile?http_key=defaultkey, with error: Post http://localhost:7350/v2/rpc/refreshPublicProfile?http_key=defaultkey: EOF
服务器B上的日志为:
2019-02-21 18:33:57.586869 +0530 IST m=+74.893470343: -------> SENDING POST: http://localhost:8103/publicprofile
2019-02-21 18:34:13.513541 +0530 IST m=+90.819664927: <------- RECEIVED POST: http://localhost:8103/publicprofile
golang http的主人能否请我帮助您了解这样做的正确方法?
答案 0 :(得分:0)
问题与golang http软件包无关,而与服务器B有关,服务器B是第三方服务器,如果请求的运行时间超过10秒钟,则该服务器不会发送响应。因此,服务器A收到了上面列出的错误。但是,值得注意的学习都归因于@JimB:
最小,完整和可验证的示例非常有用,因为我最终创建了一个示例,并且在那里未观察到相同的错误。这使我相信问题与服务器B有关,后者是第三方应用程序。我已经向他们的团队提交了一个错误,他们现在正在调查该错误。
现在已经使用默认值构造了传输:
tr := http.DefaultTransport.(*http.Transport)
if tr.TLSClientConfig == nil {
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: insecureSkipVerify}
} else {
tr.TLSClientConfig.InsecureSkipVerify = insecureSkipVerify
}
事实证明,这是我浪费了几天的虚假警报。不确定这是否对任何人都有帮助,但仍然想发布有关最终解决方法的详细信息。