私は Golang で httpserver を書いていますが、Web ブラウザからのマルチリクエスト時に http.HandleFunc がブロックされることがわかりました。サーバーに複数のリクエストを同時に処理させるにはどうすればよいですか? ありがとう。
私のコードは次のとおりです。
func DoQuery(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Printf("%d path %s\n", time.Now().Unix(), r.URL.Path)
time.Sleep(10 * time.Second)
fmt.Fprintf(w, "hello...")
//why this function block when multi request ?
}
func main() {
fmt.Printf("server start working...\n")
http.HandleFunc("/query", DoQuery)
s := &http.Server{
Addr: ":9090",
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
//MaxHeaderBytes: 1 << 20,
}
log.Fatal(s.ListenAndServe())
fmt.Printf("server stop...")
}
コードを実行したところ、すべてが期待どおりに機能しました。同時に 2 つのリクエスト (curl localhost:9090/query) を実行しましたが、両方とも 10 秒後に完了しました。多分問題は他の場所にありますか?使用したコマンドは次のとおりです: time curl -s localhost:9090/query | echo $(curl -s localhost:9090/query) – tjameson
ありがとう
それは奇妙だ。クロムから同じURLをリクエストすると、2つのリクエストが同時に処理されずに送信されますが、curテストを使用すると同時に処理できます。しかし、異なるURLを使用して2つのリクエストを送信すると、同時に処理できます。
[root@localhost httpserver]# ./httpServer
サーバーが動作を開始します...
1374301593 パス /query?form=chrome
1374301612 パス /クエリ?from=cur2
1374301614 パス /クエリ?from=cur1
1374301618 パス /query?form=chrome
1374301640 パス /query?form=chrome2
1374301643 パス /query?form=chrome1
*1374301715 パス /query?form=chrome
1374301725 パス /query?form=chrome*
**1374301761 パス /query?form=chrome1
1374301763 パス /query?form=chrome2**