Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process parallel HTTP request in go programming language?

Tags:

go

I was playing with the go 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 in order to process each request in a different gorutine. 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

like image 785
Fersca Avatar asked Jun 25 '12 21:06

Fersca


People also ask

How do you make HTTP request in Golang?

Get function is called, Go will make an HTTP request using the default HTTP client to the URL provided, then return either an http. Response or an error value if the request fails. If the request fails, it will print the error and then exit your program using os. Exit with an error code of 1 .

Do Goroutines run in parallel?

If your computer has four processor cores and your program has four goroutines, all four goroutines can run simultaneously. When multiple streams of code are running at the same time on different cores like this, it's called running in parallel.

Is HTTP client thread safe Golang?

Http clients are thread safe according to the docs (https://golang.org/src/net/http/client.go): Clients are safe for concurrent use by multiple goroutines.

What is HTTP transport Golang?

58 const DefaultMaxIdleConnsPerHost = 2 59 60 // Transport is an implementation of RoundTripper that supports HTTP, 61 // HTTPS, and HTTP proxies (for either HTTP or HTTPS with CONNECT). 62 // 63 // By default, Transport caches connections for future re-use.


1 Answers

All connections are automatically handled concurrently. Each TCP connection (not request) gets its own goroutine.

In a world with http pipelining and browsers that reuse connections, this may not always work out well. Most likely your browser is reusing a connection which stalls it until the current request being processed by the goroutine finishes.

like image 98
Stephen Weinberg Avatar answered Oct 25 '22 22:10

Stephen Weinberg