From 54283095491c764781617f57145863deaa9f97d7 Mon Sep 17 00:00:00 2001 From: Alex Ferrari Date: Mon, 24 Oct 2022 09:40:46 +0200 Subject: [PATCH] Fix error with negative wg counter --- pkg/gohn/doc.go | 2 +- pkg/gohn/items.go | 17 +++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/pkg/gohn/doc.go b/pkg/gohn/doc.go index 0a110aa..90dc384 100644 --- a/pkg/gohn/doc.go +++ b/pkg/gohn/doc.go @@ -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" ) diff --git a/pkg/gohn/items.go b/pkg/gohn/items.go index 36ff727..7cb40d3 100644 --- a/pkg/gohn/items.go +++ b/pkg/gohn/items.go @@ -133,7 +133,7 @@ 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 @@ -141,12 +141,13 @@ L: 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() } } @@ -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 }