php小编香蕉在探讨网络请求中的超时持续时间时发现,为什么net/http在设计中没有考虑超过30秒的超时时间限制?超时时间是指在发送请求后,如果在指定时间内没有收到响应,就会认为请求失败。在网络请求中,超时时间的设置是非常重要的,过短可能导致请求失败,过长则会浪费资源。通过分析,主要原因是在设计时考虑到了性能和资源的平衡,以及网络环境的不确定性。接下来,我们将详细解答这个问题。
问题内容
使用 golang 1.20.1。
在golang的net/http和context包中,我无法设置超过三十秒的超时。设置较短的超时效果很好,例如
代码:
log.infof("elasticsearch url is %v", elasticsearchurl) client := &http.client{timeout: time.duration(time.second * 60)} req, err := http.newrequest("get", listbackupsurl, nil) if err != nil { internalerror(w, fmt.sprintf("error creating request: %v", err)) return } req.setbasicauth(username, password) resp, err := client.do(req) if err != nil { // handle error internalerror(w, fmt.sprintf("error accessing elasticsearch: %v", err)) return }
日志:
i0503 23:01:55.973821 1 somecode.go:85] url is http://elasticsearch.example.ingest:9200 e0503 23:02:25.976345 1 caller_handler.go:63] 500 internal server error: error accessing elasticsearch: get "http://elasticsearch.example.ingest:9200/_cat/snapshots/object-store-repo?v&s=id": dial tcp 1.2.3.4:9200: i/o timeout
超时时间是三十秒,而不是六十秒。
如果我使用 http.newrequestwithcontext(...)
并使用设置相同超时的上下文,我会得到相同的行为:
代码:
log.infof("elasticsearch url is %v", elasticsearchurl) ctx, cancel := context.withtimeout(context.background(), time.duration(time.second * 60)) defer cancel() req, err := http.newrequestwithcontext(ctx, "get", listbackupsurl, nil) if err != nil { internalerror(w, fmt.sprintf("error creating request: %v", err)) return } req.setbasicauth(username, password) resp, err := client.do(req) if err != nil { // handle error internalerror(w, fmt.sprintf("error accessing elasticsearch: %v", err)) return }
日志:
i0503 23:31:10.941169 1 somecode.go:85] elasticsearch url is http://elasticsearch.example.ingest:9200 e0503 23:31:40.941642 1 caller_handler.go:63] 500 internal server error: error accessing elasticsearch: get "http://elasticsearch.example.ingest:9200/_cat/snapshots/object-store-repo?v&s=id": dial tcp 1.2.3.4:9200: i/o timeout
但是,如果我在任一方法中将超时更改为三秒 (time.duration(time.second * 3))
,它将按预期工作:
I0503 23:44:17.622121 1 somecode.go:85] Elasticsearch URL is http://elasticsearch.example.ingest:9200 E0503 23:44:20.624795 1 caller_handler.go:63] 500 Internal Server Error: error accessing elasticsearch: Get "http://elasticsearch.example.ingest:9200/_cat/snapshots/object-store-repo?v&s=id": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
解决方法
我会隔离问题以消除可能性,从而帮助缩小导致问题发生的原因。如果可能,请使用一小段代码,这样您就可以只处理您想要的内容。
要测试您的基础设施,httpstat 将帮助您模拟远程超时。示例:
func main() { client := &http.Client{Timeout: time.Duration(time.Second * 200)} req, err := http.NewRequest("GET", "https://www.php.cn/link/1a59ef90d1ea801448e1567d0896a99f/504?sleep=120000", nil) if err != nil { log.Fatal(err) return } resp, err := client.Do(req) if err != nil { log.Fatal(err) return } fmt.Println(resp) }
如果您此时收到 dial tcp ip:port: i/o timeout
timeout,那么您需要检查您的操作系统和防火墙。无论您在 go 中设置什么超时,都应该覆盖操作系统默认值,如果您以这种方式超时,则可能是防火墙(本地或远程)导致了这种情况。
或者,如果您能够从外部连接,则 es 可能会超时,尽管根据文档,您应该预期会直接来自 es 的信息错误。您可以直接在 url 中设置 es 的超时 并对其进行测试。
希望这有帮助。
以上是为什么 net/http 不考虑超过 30 秒的超时持续时间?的详细内容。更多信息请关注PHP中文网其他相关文章!

Golang在实际应用中表现出色,以简洁、高效和并发性着称。 1)通过Goroutines和Channels实现并发编程,2)利用接口和多态编写灵活代码,3)使用net/http包简化网络编程,4)构建高效并发爬虫,5)通过工具和最佳实践进行调试和优化。

Go语言的核心特性包括垃圾回收、静态链接和并发支持。1.Go语言的并发模型通过goroutine和channel实现高效并发编程。2.接口和多态性通过实现接口方法,使得不同类型可以统一处理。3.基本用法展示了函数定义和调用的高效性。4.高级用法中,切片提供了动态调整大小的强大功能。5.常见错误如竞态条件可以通过gotest-race检测并解决。6.性能优化通过sync.Pool重用对象,减少垃圾回收压力。

Go语言在构建高效且可扩展的系统中表现出色,其优势包括:1.高性能:编译成机器码,运行速度快;2.并发编程:通过goroutines和channels简化多任务处理;3.简洁性:语法简洁,降低学习和维护成本;4.跨平台:支持跨平台编译,方便部署。

关于SQL查询结果排序的疑惑学习SQL的过程中,常常会遇到一些令人困惑的问题。最近,笔者在阅读《MICK-SQL基础�...

golang ...

Go语言中如何对比并处理三个结构体在Go语言编程中,有时需要对比两个结构体的差异,并将这些差异应用到第�...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版
中文版,非常好用