Skip to content

Instantly share code, notes, and snippets.

@dbridges
Created May 13, 2019 17:33
Show Gist options
  • Save dbridges/882826576e0504f9e880d8eacdee3262 to your computer and use it in GitHub Desktop.
Save dbridges/882826576e0504f9e880d8eacdee3262 to your computer and use it in GitHub Desktop.
type feedResult struct {
feed *Feed
err error
}
func fetchFeeds(urls []string) ([]*Feed) {
// Create a channel to process the feeds
feedc := make(chan feedResult, len(urls))
// Start a goroutine for each feed url
for _, u := range urls {
go fetchFeed(u, feedc)
}
// Wait for the goroutines to write their results to the channel
feeds := []*Feed{}
for i := 0; i < len(urls); i++ {
res := <-feedc
// If the goroutine errors out, we'll just wait for others
if res.err != nil {
continue
}
feeds = append(feeds, res.feed)
}
return feeds
}
func fetchFeed(url string, feedc chan feedResult) {
// Create a client with a default timeout
net := &http.Client{
Timeout: time.Second * 10,
}
// Issue a GET request for the feed
res, err := net.Get(url)
// If there was an error write that to the channel and return immediately
if err != nil {
feedc <- feedResult{nil, err}
return
}
defer res.Body.Close()
// Read the body of the request and parse the feed
body, err := ioutil.ReadAll(res.Body)
if err != nil {
feedc <- feedResult{nil, err}
return
}
feed, err := parseFeed(body)
if err != nil {
feedc <- feedResult{nil, err}
return
}
feedc <- feedResult{feed, nil}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment