2016-07-21 19 views
0

在當前時間嘗試使用golang HTTP服務器,並從該代碼進行編譯: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臺發送測試數據

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

它的工作外觀極好的小文件,但ESServer.exe有大小的8Mb和我收到一個錯誤「apr_socket_recv:現有的連接被強行關閉遠程主機(730054)。」

可偏偏什麼問題?

+0

你的第一個問題是您正在使用AB,這甚至不是HTTP/1.1,因此開放爲每個請求一個新的連接,這將不利於當你用完文件描述符或臨時端口時。接下來,您的多路複用器不安全,並將在併發請求下發生混亂。 – JimB

+0

@jimb ab比'-k'更樂意做keepalive。 – hobbs

+0

@hobbs:是的,但它仍然是HTTP/1.0,這裏的例子是不使用'-k';)(加上去的http服務器可以在這些微基準做作'跑贏ab',所以它的測試'ab'爲就像它正在測試服務器一樣) – JimB

回答

1

你不讀請求體,因此每個請求是要阻止一旦所有緩衝區填滿。您始終需要全面讀取請求或強制斷開客戶端以避免請求掛起並耗費資源。

至少,你可以

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

非常感謝你...... –