为了账号安全,请及时绑定邮箱和手机立即绑定

客户端关闭连接时,Go http.ResponseWriter.Write 不返回错误

客户端关闭连接时,Go http.ResponseWriter.Write 不返回错误

沧海一幻觉 2022-06-06 15:52:45
我编写了一个简单的 http HandleFunc,需要 2 秒来处理,然后我向服务器发送一个超时 1 秒的请求。我希望调用 ResponseWriter.Write 会返回一个错误,但它不会。请为我解释一下。服务器代码:import (    "log"    "net/http"    "time")func main() {    log.SetFlags(log.Ltime | log.Lshortfile)    handler := http.NewServeMux()    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)            n, err := w.Write([]byte("my slow response"))            if err != nil {                log.Println("error when ResponseWriter Write: ", err)            }            log.Printf("responded to long-task. n: %v\n", n)        })    server := &http.Server{Addr: ":8008", Handler: handler}    log.Println("listening on port ", server.Addr)    err := server.ListenAndServe()    if err != nil {        log.Fatal(err)    }}客户端代码:package mainimport (    "io/ioutil"    "log"    "net/http"    "time")func main() {    client := http.Client{Timeout: 1 * time.Second}    r, _ := http.NewRequest("GET", "http://127.0.0.1:8008/long-task", nil)    w, err := client.Do(r)    if err != nil {        log.Fatal(err)    }    defer w.Body.Close()    body, err := ioutil.ReadAll(w.Body)    if err != nil {        log.Fatal(err)    }    log.Printf("response: %s\n", body)}服务器输出:10:50:58 http_server.go:28: listening on port  :800810:51:01 http_server.go:19: handling long-task10:51:02 http_server.go:17: client closed or server responded10:51:03 http_server.go:25: responded to long-task. n: 16客户端输出:2020/05/27 10:51:02 Get "http://127.0.0.1:8008/long-task": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
查看完整描述

1 回答

?
largeQ

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



查看完整回答
反对 回复 2022-06-06
  • 1 回答
  • 0 关注
  • 240 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信