为什么 net/http 不考虑超过 30 秒的超时持续时间?
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中文网其他相关文章!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本文解释了GO的软件包导入机制:命名imports(例如导入“ fmt”)和空白导入(例如导入_ fmt; fmt;)。 命名导入使包装内容可访问,而空白导入仅执行t

本文解释了Beego的NewFlash()函数,用于Web应用程序中的页间数据传输。 它专注于使用newflash()在控制器之间显示临时消息(成功,错误,警告),并利用会话机制。 Lima

本文详细介绍了MySQL查询结果的有效转换为GO结构切片。 它强调使用数据库/SQL的扫描方法来最佳性能,避免手动解析。 使用DB标签和Robus的结构现场映射的最佳实践

本文演示了创建模拟和存根进行单元测试。 它强调使用接口,提供模拟实现的示例,并讨论最佳实践,例如保持模拟集中并使用断言库。 文章

本文探讨了GO的仿制药自定义类型约束。 它详细介绍了界面如何定义通用功能的最低类型要求,从而改善了类型的安全性和代码可重复使用性。 本文还讨论了局限性和最佳实践

本文详细介绍了在GO中详细介绍有效的文件,将OS.WriteFile(适用于小文件)与OS.openfile和缓冲写入(最佳大型文件)进行比较。 它强调了使用延迟并检查特定错误的可靠错误处理。

本文使用跟踪工具探讨了GO应用程序执行流。 它讨论了手册和自动仪器技术,比较诸如Jaeger,Zipkin和Opentelemetry之类的工具,并突出显示有效的数据可视化
