Last active
April 26, 2017 05:01
-
-
Save phoorichet/c7bbd4aae318ae5a5a1930700da481f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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