当响应超过8kb时,为什么golang http服务器因"断管"而失败?

eld*_*soa 8 curl http httpresponse go

下面我有在那里,如果你调用一个例子Web服务器curl localhost:3000 -v,然后^C(取消)立即(在1秒内),它会报告write tcp 127.0.0.1:3000->127.0.0.1:XXXXX: write: broken pipe.

package main

import (
    "fmt"
    "net/http"
    "time"
)

func main() {
    log.Fatal(http.ListenAndServe(":3000", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            time.Sleep(1 * time.Second)

            // Why 8061 bytes? Because the response header on my computer
            // is 132 bytes, adding up the entire response to 8193 (1 byte 
            // over 8kb)
            if _, err := w.Write(make([]byte, 8061)); err != nil {
                    fmt.Println(err)
                    return
            }   
    })))
}
Run Code Online (Sandbox Code Playgroud)

根据我的调试,我得出结论,只有当整个响应写入超过8192字节(或8kb)时才会发生这种情况.如果我的整个响应写入少于8192,broken pipe则不会返回错误.

我的问题是这个8192字节(或8kb)缓冲区限制在哪里设置?这是Golang的HTTP写缓冲区的限制吗?这与响应被分块有关吗?这只与curl客户端或浏览器客户端有关吗?如何更改此限制,以便在连接关闭之前写入更大的缓冲区(用于调试目的)?

谢谢!

Jim*_*imB 13

net/http/server.go输出缓冲区中设置为4<<10,即4KB.

您在8KB处看到错误的原因是,至少需要2次写入套接字才能检测到已关闭的远程连接.第一次写入成功,但远程主机发送RST数据包.第二次写入将是一个关闭的套接字,这是返回broken pipe错误的内容.

根据套接字写入缓冲区和连接延迟,在注册第一个RST数据包之前,可能会有更多写入成功.