Search code examples
httpgolarge-files

golang http server does not accept post large data


At current time try use golang http server and compile it from this code:

    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())
}

Runs it and send test data via Apache Bench

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

It's working excelent with small files but ESServer.exe has size 8Mb and I'm receiving next error "apr_socket_recv: An existing connection was forcibly closed by the remote host. (730054)."

What problem may happens?


Solution

  • You're not reading the request body, so each request is going to block once all buffers are filled. You always need to read the request in full or forcibly disconnect the client to avoid the request hanging and consuming resources.

    At a minimum, you can

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