golang http 服务器不接受 post 大数据

golang http server does not accept post large data

目前尝试使用 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: An existing connection was forcibly closed by the remote host. (730054)."

可能会出现什么问题?

您没有读取请求正文,因此一旦所有缓冲区都已填满,每个请求都会被阻止。你总是需要完整地读取请求或者强制断开客户端,以避免请求挂起并消耗资源。

至少,你可以

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