In Golang, Is http.HandleFunc block?
Asked Answered
E

1

8

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("%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...")
}

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**

Etymon answered 20/7, 2013 at 3:28 Comment(7)
my Go version is :go version go1.1.1 linux/386Etymon
What do you mean by blocking? What is the behavior you see that was unexpected?Deviant
i send two request in the web browser same time. the second request was handle after 10 sec later. server start working... 1374289669 path /query 1374289679 path /query I want the two request handle in the same time, like 1374289669 path /query 1374289669 path /queryEtymon
FWIW, your fmt.Printf("server stop...") will never get called because log.Fatal will kill the process: sourceBeacham
Go’s http package can serve thousands of requests concurrently. You don’t block your program or stop receiving new requests when serving one.Orthogonal
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)Beacham
i use the google chrome in the same computer. i will try curl to test.Etymon
O
20

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

Ogre answered 20/7, 2013 at 6:25 Comment(3)
thanks a lot, i think here is the chrome browser limit reason. 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.Etymon
I think you mean concurrently, not in parallel. Serving thousands of requests in parallel would require thousands of CPU cores.Bedspring
@rightfold For this particular program it's the same thing, but of course you are right.Wader

© 2022 - 2024 — McMap. All rights reserved.