我正在我的http.timeoutHandlerGo Web 服务器中测试,我注意到 3 秒后我的客户端调用收到“ Timeout”消息,但 2 秒后我可以在服务器上看到消息“我的func Println”。为什么TimeoutHandler没有取消我的func1?
这是我正在使用的代码:
package main
import (
"fmt"
"io"
"net/http"
"time"
)
func func1(w http.ResponseWriter, req *http.Request) {
time.Sleep(5 * time.Second)
fmt.Println("My func Println")
io.WriteString(w, "My func!\n")
}
func main() {
srv := http.Server{
Addr: ":9000",
WriteTimeout: 5 * time.Second,
Handler: http.TimeoutHandler(http.HandlerFunc(func1), 3*time.Second, "Timeout!\n"),
}
if err := srv.ListenAndServe(); err != nil {
fmt.Printf("Server failed: %s\n", err)
}
}
慕容森
慕无忌1623718
随时随地看视频慕课网APP
相关分类