Skip to content

Instantly share code, notes, and snippets.

@phoorichet
Last active April 26, 2017 05:01
Show Gist options
  • Save phoorichet/c7bbd4aae318ae5a5a1930700da481f8 to your computer and use it in GitHub Desktop.
Save phoorichet/c7bbd4aae318ae5a5a1930700da481f8 to your computer and use it in GitHub Desktop.
func consumer(queue chan *http.Request, id int) {
// create http client
client := &http.Client{}
// consumer runs forever until the queue is closed
for {
select {
case req, ok := <-queue:
if !ok {
// not ok means queue is closed
// we exit the func
return
}
// fire the request
resp, err := client.Do(req)
if err != nil {
// continue the the next request for now
continue
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("error: %v", err)
continue
}
fmt.Printf("consumer id %d, resp: %s\n", id, string(body))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment