Golang - TLS握手错误

时间:2016-01-16 05:07:32

标签: go https

我正在运行https网络服务器。我正在使用角度网络应用程序(Chrome浏览器)对其进行测试,该应用程序对Web服务器进行ajax调用。

如果我继续不停地点击网络服务器,一切看起来都有效。但每当我让它闲置一段时间并点击Web服务器时,来自浏览器的ajax调用都没有得到响应。我几乎总是在服务器日志中看到这个日志行。

2016/01/16 04:06:47.006977 http: TLS handshake error from 42.21.139.47:51463: EOF

我可以确认IP地址是我的IP地址。

我正在启动我的https服务器:

r := mux.NewRouter()
r.HandleFunc("/status", handleStatus)
setUpLoginEndpoint(&cfg.Session, r)
setUpLogoutEndpoint(cfg.Session.CookieName, r)
setUpChangePasswordEndpoint(cfg.Session.CookieName, r)
setUpMetricSinkEndpoint(cfg.Metric.SinkApiKey, r)
setUpMetricQueryEndpoint(cfg.Session.CookieName, r)
http.ListenAndServeTLS(":443", "../cert.pem", "../keys.pem", &Server{r})

我可以确认使用defer r.Body.Close()关闭每个处理程序中的请求正文。

我正在使用1.5.2。

任何帮助都将不胜感激。

此致

沙迪亚

2 个答案:

答案 0 :(得分:4)

我启用了tcp keepalive,这个问题得到了解决。我在谷歌计算引擎中运行我的虚拟机,可能防火墙终止了空闲连接。

TCP Keep alive

Configuring tcp keep alive in linux

Golang http服务器自动选择了这个,因此我的golang代码不需要进行任何更改。

此致

沙迪亚

答案 1 :(得分:0)

我收到与 OP 相同的错误,但就我而言,我指定的 TLSHandshakeTimeout 值太低。我不确定什么是合适的值,但将它从 100ms 移到 700ms 消除了我的错误。对于遇到相同错误并在其 http.Client 设置中指定非默认 http.Transport 配置值的其他人,您可能需要确保将 TLSHandshakeTimeout 设置为足够高的值。

&http.Client{
  Transport: &http.Transport{
    TLSHandshakeTimeout:   700 * time.Millisecond, //<-- I was receiving OP's error when I had this set to 100ms
  }
}
相关问题