Golang保持连接

huangapple go评论78阅读模式
英文:

Golang keep alive

问题

我有以下的Go代码:

package main

import (
   "net/http"
    "github.com/labstack/echo"
)
func main() {
e := echo.New()
e.POST("/getRequestData", respond)
e.Logger.Fatal(e.Start(":80"))
}

func respond(c echo.Context) error {

        resp := map[string]string{ "value": "key"}
        return c.JSON(http.StatusOK, resp)
}

我希望与该服务器的连接在10分钟的不活动时间内保持打开状态。
我发现每隔几分钟我都会从服务器收到Ack,这导致连接无限期保持打开状态。
如何在Go中关闭keep-alive?

谢谢。

英文:

I have the following go code:

package main

import (
   "net/http"
    "github.com/labstack/echo"
)
func main() {
e := echo.New()
e.POST("/getRequestData", respond)
e.Logger.Fatal(e.Start(":80"))
}

func respond(c echo.Context) error {

        resp := map[string]string{ "value": "key"}
        return c.JSON(http.StatusOK, resp)
}

I want that connections toward this server will be open up to 10 min of inactivity.
What I see that every few minutes I get Ack from the server, which causes it to remain open indefinitely.
How do I turn off keep-Alive in go?

Thanks.

答案1

得分: 2

这在2011年的问题2011中提到过(服务器端),并在提交ac213ab中得到解决。
这允许设置HTTP.Transport配置,例如将DisableKeepAlives设置为true,MaxIdleConnsPerHost设置为-1,并且这些设置将被尊重。

但是,如果您需要对TCP连接有更多控制,也可以像这里中的实现Hijacker那样选择。

英文:

This was mentioned in 2011 with issue 2011 (server side), and resolved in commit ac213ab.
That allows to set HTTP.Transport configuration like DisableKeepAlives to true, and MaxIdleConnsPerHost to -1 and have those settings respected.

But implementing an Hijacker as in here is also an option if you need more control over the TCP connection.

huangapple
  • 本文由 发表于 2021年6月1日 14:34:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/67783759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定