登录
首页 >  Golang >  Go问答

Golang Web 服务器存在内存泄漏问题涉及到 crypto/tls.(*block).reserve

来源:stackoverflow

时间:2024-03-22 14:21:47 334浏览 收藏

在使用 Go 编写 Web 服务器时,出现了一个内存泄漏问题,涉及到 crypto/tls.(*block).reserve。调查发现,内存不断增长,分析显示大量内存分配给了 crypto/tls.(*block).reserve。尽管研究了大量文档和资源,但未能找到解决方案。代码在使用 Go 1.11 的 Docker 容器中运行,且服务器似乎无法关闭与客户端的连接。

问题内容

我有一个用 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 仪表板的屏幕截图。

正如你所看到的,内存不断增长。所以我开始查看 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 分配的相同大量内存。

这是确切的代码。

我花了最后几天阅读我能找到的每一篇文章、文档、博客文章、源代码、帮助文件。然而没有任何帮助。该代码在 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 无关。

本篇关于《Golang Web 服务器存在内存泄漏问题涉及到 crypto/tls.(*block).reserve》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>