英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论