-
-
Notifications
You must be signed in to change notification settings - Fork 4
Conversation
@@ -39,6 +38,18 @@ func rssHandler(rw http.ResponseWriter, r *http.Request) { | |||
fmt.Fprintln(rw, result) | |||
} | |||
|
|||
func fetchFeeds(master *feeds.Feed) { | |||
var wg sync.WaitGroup | |||
wg.Add(len(sourceFeeds)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though technically correct, its more common to call wg.Add(1)
inside the for loop, just before the go func(...)
call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aight.
Did you run this with |
Is -race another thing we can add to CI somewhere? |
Totally right. I get @bernerdschaefer pointed out that I should only be doing the fetching, not the merging into |
And I'll be sure to add -race to the circle.yml
|
for _, feed := range sourceFeeds { | ||
fetch(feed, master) | ||
} | ||
fetchFeeds(master) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the number of feeds is defined waitgroup may not be needed. Could just loop and grab from channel?
itemChan := make(chan *feeds.Item)
for _, feed := range sourceFeeds {
go fetch(feed, itemChan)
}
for i := 1; i < len(sourceFeeds); i++ {
master.Add(<-itemChan)
}
And then thread the itemChan
through the fetch instead of master
func fetch(feed sourceFeed, itemChan chan *feeds.Item) {
itemChan <- &feeds.Item{
Title: stripPodcastEpisodePrefix(items[i].Title),
Link: &feeds.Link{Href: items[i].Links[0].Href},
Description: items[i].Description,
Author: &feeds.Author{Name: sourceName},
Created: published,
}
A few runs of the benchmark using `go test -bench=.`: BenchmarkFetchFeeds-8 1 2439212195 ns/op ok github.com/thoughtbot/rss 2.451s BenchmarkFetchFeeds-8 1 1697722256 ns/op ok github.com/thoughtbot/rss 1.709s BenchmarkFetchFeeds-8 1 1883837937 ns/op ok github.com/thoughtbot/rss 1.895s BenchmarkFetchFeeds-8 1 1837185103 ns/op ok github.com/thoughtbot/rss 1.848s
A few more benchmark results (still using `go test -bench=.`): BenchmarkFetchFeeds-8 1 1030948675 ns/op ok github.com/thoughtbot/rss 1.044s BenchmarkFetchFeeds-8 2 699606858 ns/op ok github.com/thoughtbot/rss 2.330s BenchmarkFetchFeeds-8 2 689556591 ns/op ok github.com/thoughtbot/rss 2.248s BenchmarkFetchFeeds-8 1 1477364497 ns/op ok github.com/thoughtbot/rss 1.489s BenchmarkFetchFeeds-8 1 1017372924 ns/op ok github.com/thoughtbot/rss 1.029s BenchmarkFetchFeeds-8 2 931727308 ns/op ok github.com/thoughtbot/rss 2.622s
When I check out this branch and view it in browser, I don't see any feeds. |
Benchmarks for before and after the code change are available in individual
commits. This change results in an average time of 802604696.33ns for warm
requests, decreased from 1806248432ns. This is a 55.6% improvement.