http - 如果在 http.Server 中超时,为什么请求会在 Firefox 中无限重复?
问题描述
我在 golang 中设置了一个带有超时的简单服务器。当运行一个比超时时间更长的处理程序时,如果我用 Firefox 请求它,请求会无限期地重复。但是,如果我使用 Postman 或 curl,则不会重复请求。我想防止浏览器中的重复循环。
我试图手动关闭请求正文或检查上下文是否被取消,但是这些方法都不起作用。
package main
import (
"fmt"
"net/http"
"time"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
fmt.Printf("Hello, you've requested: %s\n", r.URL.Path)
time.Sleep(time.Second * 2)
fmt.Fprintf(w, "Hello, you've requested: %s\n", r.URL.Path)
})
s := http.Server{
Addr: ":8080",
Handler: http.DefaultServeMux,
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
}
s.ListenAndServe()
}
我希望处理程序退出而不是重复。
解决方案
据我了解,您面临的问题是服务器超时突然关闭底层 tcp conn 而没有编写正确的 http 响应,同时,当 firefox 检测到 conn 突然关闭时,它似乎决定重试 N 次,可能是因为它假设它遇到连接问题。
我相信解决方案是使用http.Handler来控制处理程序处理持续时间并在超时到期时返回正确的 HTTP 响应。
服务器超时应该更长,用于防止异常客户端行为,而不是处理程序缓慢。
标准 HTTP 包为此提供了TimeoutHandler函数。
package main
import (
"fmt"
"net/http"
"time"
)
func main() {
slowHandler := func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
fmt.Printf("Hello, you've requested: %s\n", r.URL.Path)
time.Sleep(time.Second * 2)
fmt.Fprintf(w, "Hello, you've requested: %s\n", r.URL.Path)
}
http.HandleFunc("/", slowHandler)
var handler http.Handler = http.DefaultServeMux
handler = http.TimeoutHandler(handler, time.Second, "processing timeout")
s := http.Server{
Addr: ":8080",
Handler: handler,
// ReadTimeout: 1 * time.Second,
// WriteTimeout: 1 * time.Second,
}
s.ListenAndServe()
}
推荐阅读
- azure-data-lake - 如何用空值连接字符串
- delphi - Delphi中如何正确使用IFileOperation删除文件夹中的文件
- javascript - 根据用户复选框创建 api 输入参数
- flutter - Dart HttpClient 未读取完整响应
- c# - 来自 .Net Core 应用程序的 SAP HANA 连接
- java - 如何在 Spring 和 RabbitMQ 的多线程环境中使用 Spark SQL?
- clojure - 伊阿佩托斯推!推送网关引发 IOException,但推送网关返回状态 200
- json - Groovy 中的连接列表
- prometheus - 使用正则表达式解析警报“expr”时出错
- parsing - java中的Saxon XSD或Schema解析器