go - 异常大量的 TCP 连接超时错误
问题描述
我正在使用 Go TCP 客户端连接到我们的 Go TCP 服务器。
我能够连接到服务器并正确运行命令,但是在尝试连接到我们的 TCP 服务器或在连接后发送消息时,我的 TCP 客户端经常会报告异常大量的连续 TCP 连接错误:
dial tcp kubernetes_node_ip:exposed_kubernetes_port:
connectex: A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because connected
host has failed to respond.
read tcp unfamiliar_ip:unfamiliar_port->kubernetes_node_ip:exposed_kubernetes_port
wsarecv: A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because connected
host has failed to respond.
我说“异常高”是因为我假设这些错误发生的次数应该非常少(大约 5 小时内或更少)。请注意,我并没有否认这是由连接不稳定引起的,因为我还注意到可以快速连续运行多个命令而不会出现任何错误。
但是,如果我做错了什么,我仍然会发布我的代码。
下面是我的 TCP 客户端用来连接我们的服务器的代码:
serverAddress, err := net.ResolveTCPAddr("tcp", kubernetes_ip+":"+kubernetes_port)
if err != nil {
fmt.Println(err)
return
}
// Never stop asking for commands from the user.
for {
// Connect to the server.
serverConnection, err := net.DialTCP("tcp", nil, serverAddress)
if err != nil {
fmt.Println(err)
continue
}
defer serverConnection.Close()
// Added to prevent connection timeout errors, but doesn't seem to be helping
// because said errors happen within just 1 or 2 minutes.
err = serverConnection.SetDeadline(time.Now().Add(10 * time.Minute))
if err != nil {
fmt.Println(err)
continue
}
// Ask for a command from the user and convert to JSON bytes...
// Send message to server.
_, err = serverConnection.Write(clientMsgBytes)
if err != nil {
err = merry.Wrap(err)
fmt.Println(merry.Details(err))
continue
}
err = serverConnection.CloseWrite()
if err != nil {
err = merry.Wrap(err)
fmt.Println(merry.Details(err))
continue
}
// Wait for a response from the server and print...
}
下面是我们的 TCP 服务器用来接受客户端请求的代码:
// We only supply the port so the IP can be dynamically assigned:
serverAddress, err := net.ResolveTCPAddr("tcp", ":"+server_port)
if err != nil {
return err
}
tcpListener, err := net.ListenTCP("tcp", serverAddress)
if err != nil {
return err
}
defer tcpListener.Close()
// Never stop listening for client requests.
for {
clientConnection, err := tcpListener.AcceptTCP()
if err != nil {
fmt.Println(err)
continue
}
go func() {
// Add client connection to Job Queue.
// Note that `clientConnections` is a buffered channel with a size of 1500.
// Since I am the only user connecting to our server right now, I do not think
// this is a channel blocking issue.
clientConnections <- clientConnection
}()
}
下面是我们的 TCP Server 用来处理客户端请求的代码:
defer clientConnection.Close()
// Added to prevent connection timeout errors, but doesn't seem to be helping
// because said errors happen within just 1 or 2 minutes.
err := clientConnection.SetDeadline(time.Now().Add(10 * time.Minute))
if err != nil {
return err
}
// Read full TCP message.
// Does not stop until an EOF is reported by `CloseWrite()`
clientMsgBytes, err := ioutil.ReadAll(clientConnection)
if err != nil {
err = merry.Wrap(err)
return nil, err
}
// Process the message bytes...
我的问题是:
我在上面的代码中做错了什么,还是上面的代码足以满足基本的 TCP 客户端-服务器操作?
TCP Client 和 TCP Server 都有延迟关闭它们的连接的代码可以吗?
我似乎记得
defer
在循环内调用什么都不做。在开始新的连接之前,如何正确关闭客户端连接?
一些额外的信息:
- TCP 服务器不会记录上述错误,因此除了连接不稳定之外,这也可能是与 Kubernetes/Docker 相关的问题。
解决方案
这段代码似乎不像你想象的那样运行。连接关闭的 defer 语句只会在函数返回时发生,而不是在迭代结束时发生。所以据我在这里看到,您在客户端创建了很多连接,这可能是问题所在。
serverAddress, err := net.ResolveTCPAddr("tcp", kubernetes_ip+":"+kubernetes_port)
if err != nil {
fmt.Println(err)
return
}
// Never stop asking for commands from the user.
for {
// Connect to the server.
serverConnection, err := net.DialTCP("tcp", nil, serverAddress)
if err != nil {
fmt.Println(err)
continue
}
defer serverConnection.Close()
// Added to prevent connection timeout errors, but doesn't seem to be helping
// because said errors happen within just 1 or 2 minutes.
err = serverConnection.SetDeadline(time.Now().Add(10 * time.Minute))
if err != nil {
fmt.Println(err)
continue
}
// Ask for a command from the user and send to the server...
// Wait for a response from the server and print...
}
我建议这样写:
func start() {
serverAddress, err := net.ResolveTCPAddr("tcp", kubernetes_ip+":"+kubernetes_port)
if err != nil {
fmt.Println(err)
return
}
for {
if err := listen(serverAddress); err != nil {
fmt.Println(err)
}
}
}
func listen(serverAddress string) error {
// Connect to the server.
serverConnection, err := net.DialTCP("tcp", nil, serverAddress)
if err != nil {
fmt.Println(err)
continue
}
defer serverConnection.Close()
// Never stop asking for commands from the user.
for {
// Added to prevent connection timeout errors, but doesn't seem to be helping
// because said errors happen within just 1 or 2 minutes.
err = serverConnection.SetDeadline(time.Now().Add(10 * time.Minute))
if err != nil {
fmt.Println(err)
return err
}
// Ask for a command from the user and send to the server...
// Wait for a response from the server and print...
}
}
此外,您应该保持打开单个连接或连接池,而不是立即打开和关闭连接。然后,当您发送消息时,您会从池中获得连接(或单个连接),然后编写消息并等待响应,然后释放与池的连接。
像这样的东西:
res, err := c.Send([]byte(`my message`))
if err != nil {
// handle err
}
// the implementation of send
func (c *Client) Send(msg []byte) ([]byte, error) {
conn, err := c.pool.Get() // returns a connection from the pool or starts a new one
if err != nil {
return nil, err
}
// send your message and wait for response
// ...
return response, nil
}
推荐阅读
- asp.net-web-api - Web API - 无法连接到远程服务器
- angular - Angular:轻弹(半翻转)动画
- python-3.x - os.path.join 没有将 \ 放在 Users 文件夹的前面
- javascript - 如何防止手动更改查询字符串强制页面重新加载?
- node.js - Cors 将 Expressjs 中的某些域列入白名单
- node.js - 赛普拉斯通过在 cypress.json 中以某种方式将配置“pageLoadTimeout”覆盖为 1000ms 我将“pageLoadTimeout”设置为 3000m
- python - TFLite 量化模型仍然输出浮点数
- cloud-foundry - 是否有任何执行类似 CF 推送功能的 Cloud Foundry V3 API
- fastlane - 如何为 FastLane 设置 ENV['INFO_PLIST']?
- django - Django 频道和 websocket 不工作