Process parallel http request in GO

1,666 views
Skip to first unread message

Fersca

unread,
Jun 25, 2012, 5:42:04 PM6/25/12
to golang-nuts
Hi,

I was playing with the HTTP package. I wanted to process request in
parallel as I do in java. But I couldn't.

I created a simple web server, put a sleep in the middle and realized
that go process one request per time, so if I did a refresh on my
browser, the process of the first request has to finish until the
second request start processing, here is the code:

func main(){

//Process the http commands
fmt.Printf("Starting http Server ... ")
http.Handle("/", http.HandlerFunc(sayHello))
err := http.ListenAndServe("0.0.0.0:8080", nil)
if err != nil {
fmt.Printf("ListenAndServe Error",err)
}
}

func sayHello(c http.ResponseWriter, req *http.Request) {
fmt.Printf("New Request\n")
processRequest(c, req)
}

func processRequest(w http.ResponseWriter, req *http.Request){
time.Sleep(time.Second*3)
w.Write([]byte("Go Say’s Hello(Via http)"))
fmt.Println("End")
}

As I wanted to process both request in parallel I added the "go"
command before "processRequest(c, req)" in "sayHello" function. But...
it doesn't work.... I don't know why. I know that both request are
processed because I see the printed line at the console but the
browser keep waiting for information..... and don't show my response.

So... my questions,

Does each request create a new http.ResponseWriter? or it's use the
same?
Do you know how to indicate the web server to process each request
with different threads?

Any help is welcomed....

Fersca




Max

unread,
Jun 25, 2012, 6:20:07 PM6/25/12
to golan...@googlegroups.com
I think you are wrong.

Was are you using 1 browser or 2 different browsers?

May be your browser limits number of connections to server.
If you will block with Chrome then try to open your site with FireFox

I developing server that blocks for long time some connections and it works fine.

Brad Fitzpatrick

unread,
Jun 25, 2012, 6:31:06 PM6/25/12
to Fersca, golang-nuts
Each incoming HTTP request already starts a new goroutine.  When your handler function returns, the HTTP request is considered over.

Jesse McNelis

unread,
Jun 25, 2012, 6:47:09 PM6/25/12
to Fersca, golang-nuts
On Tue, Jun 26, 2012 at 7:42 AM, Fersca <fer...@gmail.com> wrote:
> Do you know how to indicate the web server to process each request
> with different threads?
>

The http server by default runs each request in it's own goroutine.
(so you don't need to add the 'go' keyword anywhere in a request
handler.)
But, by default Go only uses a single thread to schedule all goroutines.
You need to set runtime.GOMAXPROCS to increase the number of threads
it will use.
http://golang.org/pkg/runtime/

In this case you don't actually need multiple threads. Since the call
to time.Sleep() will yield to the scheduler which will schedule a
different goroutine to run while that one is sleeping.



--
=====================
http://jessta.id.au

Fernando Scasserra

unread,
Jun 25, 2012, 6:33:08 PM6/25/12
to Brad Fitzpatrick, golang-nuts
Thanks! it explains why I see the print line on my console but I don't see the response on the browser, because the the http request finish before I send the response to the client.

Fersca
Reply all
Reply to author
Forward
0 new messages