TCP连接超时异常数量过高
小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《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 服务器用来处理客户端请求的代码:
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 客户端和 tcp 服务器都具有延迟关闭其单个连接的代码可以吗?
我似乎记得在循环内调用
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
}
本篇关于《TCP连接超时异常数量过高》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
遇到问题,在为 GO 创建 Docker 时产生了错误
- 上一篇
- 遇到问题,在为 GO 创建 Docker 时产生了错误
- 下一篇
- Go语言中如何将字符串转换为数组?
-
- Golang · Go问答 | 5小时前 |
- Go cgo.Handle 管理 Go 值跨语言传递
- 403浏览 收藏
-
- Golang · Go问答 | 5小时前 | CGO · 内存管理 · Go问答 · Go CGO unsafe.Pointer cgo.Handle runtime.Pinner
- Go cgo 指针规则导致 panic 的边界定位
- 223浏览 收藏
-
- Golang · Go问答 | 6小时前 | go ·
- Go GOMAXPROCS 变化对并发吞吐的影响
- 417浏览 收藏
-
- Golang · Go问答 | 7小时前 | go · pprof · Go alloc_space inuse_space heap profile
- Go heap profile inuse_space 与 alloc_space 的选择
- 350浏览 收藏
-
- Golang · Go问答 | 7小时前 | go · 性能排查 ·
- Go runtime/trace 观察阻塞区域的阅读方式
- 283浏览 收藏
-
- Golang · Go问答 | 8小时前 | Go问答 · Go pprof 性能剖析 goroutine泄漏 并发排查
- Go pprof goroutine 泄漏剖析的采样方法
- 104浏览 收藏
-
- Golang · Go问答 | 8小时前 | 垃圾回收 · 资源管理 · Go问答 · Go 资源释放 AddCleanup close SetFinalizer
- Go SetFinalizer 不适合资源关闭的替代方案
- 439浏览 收藏
-
- Golang · Go问答 | 8小时前 |
- Go runtime.KeepAlive 防止句柄过早回收
- 489浏览 收藏
-
- Golang · Go问答 | 8小时前 |
- Go Signal Stop 与退出流程的竞态处理
- 168浏览 收藏
-
- Golang · Go问答 | 2天前 |
- Go ColumnTypes 推断动态查询字段的安全用法
- 319浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 291次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 344次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 347次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 309次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 131次使用
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go tls.GetCertificate 为什么收不到空 ServerName 请求
- 2026-09-27 501浏览
-
- Go sql.Tx提交成功前读取结果导致事务边界混乱的修复方法
- 2026-09-20 501浏览
-
- Go select 用 time.After 做超时有什么资源代价
- 2026-09-10 501浏览
