在Golang中,http.HandleFunc是阻塞的吗?

huangapple go评论90阅读模式
英文:

In Golang, Is http.HandleFunc block?

问题

我在Golang中编写了一个HTTP服务器,但是我发现当有多个请求来自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...")
    //为什么这个函数在多个请求时会被阻塞?
}

func main() {
    fmt.Printf("服务器开始工作...\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("服务器停止...")
}

我运行了你的代码,一切都按预期工作。我同时进行了两个请求(curl localhost:9090/query),它们都在10秒后一起完成。也许问题出在其他地方?这是我使用的命令:time curl -s localhost:9090/query | echo $(curl -s localhost:9090/query) – tjameson

谢谢

这很奇怪。当我从Chrome请求相同的URL时,发送两个请求不能同时处理,但是使用curl测试可以同时处理。但是当我使用不同的URL发送两个请求时,它们可以同时处理。

[root@localhost httpserver]# ./httpServer

服务器开始工作...

1374301593 path /query?form=chrome

1374301612 path /query?from=cur2

1374301614 path /query?from=cur1

1374301618 path /query?form=chrome

1374301640 path /query?form=chrome2

1374301643 path /query?form=chrome1

*1374301715 path /query?form=chrome

1374301725 path /query?form=chrome*

**1374301761 path /query?form=chrome1

1374301763 path /query?form=chrome2**

英文:

i'm write a httpserver in Golang , but i find the http.HandleFunc will be block when multi request from the web browser. how can i do make the server handle multi request in the same time ? thanks.

my code is:

func DoQuery(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	fmt.Printf(&quot;%d path %s\n&quot;, time.Now().Unix(), r.URL.Path)
	time.Sleep(10 * time.Second)
	fmt.Fprintf(w, &quot;hello...&quot;)
	//why this function block when multi request ?
}

func main() {
	fmt.Printf(&quot;server start working...\n&quot;)
	http.HandleFunc(&quot;/query&quot;, DoQuery)
	s := &amp;http.Server{
		Addr:         &quot;:9090&quot;,
		ReadTimeout:  30 * time.Second,
		WriteTimeout: 30 * time.Second,
		//MaxHeaderBytes: 1 &lt;&lt; 20,
	}
	log.Fatal(s.ListenAndServe())
	fmt.Printf(&quot;server stop...&quot;)
}

I ran your code and everything worked as expected. I did two requests at the same time (curl localhost:9090/query) and they both finished 10 seconds later, together. Maybe the problem is elsewhere? Here's the command I used: time curl -s localhost:9090/query | echo $(curl -s localhost:9090/query) – tjameson

thakns

that's strange.
when i request same url from chrome ,send two request not handle in the same time, but use cur test can handle in the same time.
but when i send two request use different url, it's can be handle in the same time.

[root@localhost httpserver]# ./httpServer

server start working...

1374301593 path /query?form=chrome

1374301612 path /query?from=cur2

1374301614 path /query?from=cur1

1374301618 path /query?form=chrome

1374301640 path /query?form=chrome2

1374301643 path /query?form=chrome1

*1374301715 path /query?form=chrome

1374301725 path /query?form=chrome*

**1374301761 path /query?form=chrome1

1374301763 path /query?form=chrome2**

答案1

得分: 20

是的,标准的HTTP服务器会为每个请求启动一个新的goroutine。根据操作系统的设置,您应该能够并行处理数千个请求。

您的浏览器可能会限制发送到一个服务器的请求数量;请确保您正在使用没有这种限制/“优化”的客户端进行测试。

可靠的Go文档解释了Http服务器为每个请求创建一个新的goroutine:http://golang.org/pkg/net/http/#Server.Serve

英文:

Yes, the standard HTTP server will start a new goroutine for each request. You should be able to do thousands of requests in parallel depending on the operating system settings.

Your browser might be limiting how many requests it will send to one server; be sure you are testing with a client that doesn't have that limitation/"optimization".

Reliably Go docs explaining Http Server creates a new gorotine for each request: http://golang.org/pkg/net/http/#Server.Serve

huangapple
  • 本文由 发表于 2013年7月20日 11:28:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/17758345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定