ssl - Golang Web 服务器在 crypto/tls.(*block).reserve 泄漏内存

标签 ssl go memory-leaks tls1.2

我有一个用 Go 编写的 Web 服务器。

tlsConfig := &tls.Config{
    PreferServerCipherSuites: true,
    MinVersion:               tls.VersionTLS12,
    CurvePreferences: []tls.CurveID{
        tls.CurveP256,
        tls.X25519,
    },
    CipherSuites: []uint16{
        tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
        tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
        tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
        tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
        tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
        tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
    },
}

s := &http.Server{
    ReadTimeout:  5 * time.Second,
    WriteTimeout: 10 * time.Second,
    IdleTimeout:  120 * time.Second,
    Handler:      r, // where r is my router
    TLSConfig:    tlsConfig,
}

// redirect http to https
redirect := &http.Server{
    ReadTimeout:  5 * time.Second,
    WriteTimeout: 10 * time.Second,
    IdleTimeout:  120 * time.Second,
    Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Connection", "close")
        url := "https://" + r.Host + r.URL.String()
        http.Redirect(w, r, url, http.StatusMovedPermanently)
    }),
}
go func() {
    log.Fatal(redirect.ListenAndServe())
}()

log.Fatal(s.ListenAndServeTLS(certFile, keyFile))

这是我的 Digital Ocean 仪表板的屏幕截图。

enter image description here

如您所见,内存不断增长。所以我开始看https://github.com/google/pprof .这是top5的输出。

Type: inuse_space
Time: Nov 7, 2018 at 10:31am (CET)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top5
Showing nodes accounting for 289.50MB, 79.70% of 363.24MB total
Dropped 90 nodes (cum <= 1.82MB)
Showing top 5 nodes out of 88
      flat  flat%   sum%        cum   cum%
  238.98MB 65.79% 65.79%   238.98MB 65.79%  crypto/tls.(*block).reserve
   20.02MB  5.51% 71.30%    20.02MB  5.51%  crypto/tls.Server
   11.50MB  3.17% 74.47%    11.50MB  3.17%  crypto/aes.newCipher
   10.50MB  2.89% 77.36%    10.50MB  2.89%  crypto/aes.(*aesCipherGCM).NewGCM

SVG 显示了由 crypto/tls.(*block).reserve 分配的大量内存。

enter image description here

这是确切的代码。

enter image description here

我在最后几天阅读了我能找到的每一篇文章、文档、博客文章、源代码和帮助文件。然而,没有任何帮助。该代码在 Docker 容器内使用 Go 1.11 的 Ubuntu 17.10 x64 机器上运行。

看起来服务器没有关闭与客户端的连接。我认为设置所有 xyzTimeout 会有所帮助,但它没有。

有什么想法吗?

2018 年 12 月 20 日编辑:

现已修复 https://github.com/golang/go/issues/28654#issuecomment-448477056

最佳答案

添加一个答案,这样它就不会一直显示在已投票和未回答的问题列表中。

看来内存泄漏与 gorilla 上下文错误 https://github.com/gorilla/sessions/commit/12bd4761fc66ac946e16fcc2a32b1e0b066f6177 有关并且与stdlib中的tls无关。

关于ssl - Golang Web 服务器在 crypto/tls.(*block).reserve 泄漏内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53189316/

相关文章:

web-services - maven-jaxb2-plugin - 忽略 ssl 错误

java - 使用 Apache James 实现客户端身份验证

ssl - 同一个 JVM 中的两个 SSLContext

戈朗 : Recursive data structures

java - 空 LibGdx 游戏循环泄漏内存

c - SSL_CONNECT 因 SSL_ERROR_SYSCALL 错误而失败

memory - Golang, fatal error : out of memory on 1 TB RAM machine

unit-testing - 如何在并行Go测试中正确使用 channel ?

ruby-on-rails - Heroku 上的 Rails 4 错误 R14(超出内存配额)

ruby-on-rails - Rails rake 任务在运行时不断消耗更多 RAM(使用 Mechanize 抓取网站)