golanghttp服务器不接受post大数据



当前尝试使用golang http服务器并从以下代码编译:

    package main
import (
    "io"
    "net/http"
    "time"
)
func hello(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    io.WriteString(w, "Hello world!")
}
var mux map[string]func(http.ResponseWriter, *http.Request)
func main() {
    server := http.Server{
        Addr:           ":8000",
        MaxHeaderBytes: 30000000,
        ReadTimeout:    10 * time.Second,
        WriteTimeout:   10 * time.Second,
        Handler:        &myHandler{},
    }
    mux = make(map[string]func(http.ResponseWriter, *http.Request))
    mux["/"] = hello
    server.ListenAndServe()
}
type myHandler struct{}
func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if h, ok := mux[r.URL.String()]; ok {
        h(w, r)
        return
    }
    io.WriteString(w, "My server: "+r.URL.String())
}

运行它并通过Apache Bench 发送测试数据

ab.exe -c 30 -n 1000 -p ESServer.exe -T application/octet-stream http://localhost:8000/ 

它能很好地处理小文件,但ESServer.exe的大小为8Mb,我收到下一个错误"apr_socket_recv:远程主机强制关闭了现有连接。(730054)。"

可能会发生什么问题?

您没有读取请求主体,因此一旦所有缓冲区都被填满,每个请求都将被阻塞。您总是需要完整读取请求或强制断开客户端连接,以避免请求挂起并消耗资源。

至少,你可以

io.Copy(ioutil.Discard, r.Body)

最新更新