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

标签 http go large-files

目前尝试使用 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)

关于http - golang http服务器不接受post大数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38513607/

相关文章:

http - 是否有一个网站除了吐出发送给它的所有 POST 表单参数之外什么都不做?

google-chrome - 为什么无法下载状态代码为 4XX 和 5XX 的文件

angular - "_this.http is undefined"无缘无故

json - 无法在GO中将JSON作为HTTP POST请求的主体发送总是返回400错误和5035代码?

postgresql - 如何在 golang lib/pq postgresql 驱动程序中设置应用程序名称?

java - 从 23GB 的文件中读取 2 个字节

mysql - 如何将大型 (14 GB) MySQL 转储文件导入新的 MySQL 数据库?

http - 是否有可用的 JMX - REST 桥?

go - 无法定义表间关系

java - 如何让 Java 使用 Scanner 读取非常大的文件?