我在下面有一个web服务器示例,如果您立即(在1秒之前)拨打curl localhost:3000 -v
然后拨打^C
(取消),它将报告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
}
})))
}
根据我的调试,我已经能够得出结论,只有当整个响应写入超过8192字节(或8KB)时,才会发生这种情况.如果我的整个响应写入少于8192,则不会返回broken pipe
错误.
我的问题是where is this 8192 bytes (or 8kb) buffer limit set?这是Golang的HTTP写缓冲区的限制吗?这与被分块的响应有关吗?这只与curl
客户端或浏览器客户端有关吗?如何更改此限制,以便在连接关闭之前写入更大的缓冲区(用于调试)?
谢谢!