1 回答
TA贡献2039条经验 获得超7个赞
写入的数据太少,ResponseWriter 没有将缓冲的数据发送给客户端。
handler.HandleFunc("/long-task", func(w http.ResponseWriter, r *http.Request) {
go func() {
<-r.Context().Done()
log.Println("client closed or server responded")
}()
log.Println("handling long-task")
time.Sleep(2 * time.Second)
for i := 0; i < 520; i++ {
_, err := w.Write([]byte("my slow response"))
if err != nil {
log.Println("error when ResponseWriter Write: ", err, i)
}
}
// log.Printf("responded to long-task. n: %v\n", n)
})
输出:
13:53:19 01.go:30: listening on port :8008
13:53:22 01.go:18: handling long-task
13:53:23 01.go:16: client closed or server responded
13:53:24 01.go:23: error when ResponseWriter Write: write tcp 127.0.0.1:8008->127.0.0.1:57098: write: broken pipe 512
13:53:24 01.go:23: error when ResponseWriter Write: write tcp 127.0.0.1:8008->127.0.0.1:57098: write: broken pipe 513
13:53:24 01.go:23: error when ResponseWriter Write: write tcp 127.0.0.1:8008->127.0.0.1:57098: write: broken pipe 514
13:53:24 01.go:23: error when ResponseWriter Write: write tcp 127.0.0.1:8008->127.0.0.1:57098: write: broken pipe 515
13:53:24 01.go:23: error when ResponseWriter Write: write tcp 127.0.0.1:8008->127.0.0.1:57098: write: broken pipe 516
13:53:24 01.go:23: error when ResponseWriter Write: write tcp 127.0.0.1:8008->127.0.0.1:57098: write: broken pipe 517
13:53:24 01.go:23: error when ResponseWriter Write: write tcp 127.0.0.1:8008->127.0.0.1:57098: write: broken pipe 518
13:53:24 01.go:23: error when ResponseWriter Write: write tcp 127.0.0.1:8008->127.0.0.1:57098: write: broken pipe 519
添加回答
举报