Skip to content

Commit

Permalink
Fix error with negative wg counter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexferrari88 committed Oct 24, 2022
1 parent a27c448 commit 5428309
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/gohn/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ Package GoHN is a wrapper for the Hacker News API: https://github.com/HackerNews
package gohn

const (
Version = "0.7.3"
Version = "0.7.4"
)
17 changes: 7 additions & 10 deletions pkg/gohn/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,21 @@ L:
select {
// fetch the item and send it to commentsChan
case currentId := <-kidsQueue:
go func() {
go func(wg *sync.WaitGroup, currentId int) {
it, err := s.Get(ctx, currentId)
if err != nil {
// TODO: add better error handling
wg.Done()
return
}
if fn != nil {
excludeKids, err := fn(it, &wg)
excludeKids, err := fn(it, wg)
if err != nil && excludeKids {
// TODO: add better error handling
wg.Done()
if it.Kids != nil {
for range *it.Kids {
// TODO: doesn't remove kids' kids from the waitgroup
wg.Done()
}
}
Expand All @@ -161,26 +162,22 @@ L:
return
}
}
if it.Dead != nil && *it.Dead {
wg.Done()
return
}
commentsChan <- it
}()
}(&wg, currentId)
// add the item to the map and, if it has any kid,
// add their IDs to the queue so that they can be fetched
case comment := <-commentsChan:
if comment.ID != nil {
mapCommentById[*comment.ID] = comment
if comment.Kids != nil && len(*comment.Kids) > 0 {
go func() {
go func(comment *Item) {
for _, kid := range *comment.Kids {
kidsQueue <- kid
}
}()
}(comment)
}
wg.Done()
}
wg.Done()
case <-done:
break L
}
Expand Down

0 comments on commit 5428309

Please sign in to comment.