I want to build and display a tree of linked issues using the github.com/xlab/treeprint package. I have a working version, but it doesn't use go-routines, and seems like a good candidate.
The tree part may be irrelevant, though maybe if I return different values from my function, I could build it in a different way.
func main {
tree := treeprint.New()
recurseTreeFetching(fetcher, tree, *issueID)
fmt.Println(tree.String())
}
func recurseTreeFetching(fetcher Fetcher, tree treeprint.Tree, issueID string) {
issues := fetcher.fetchIssues(issueID)
if len(issues) == 0 {
return
}
for i := 0; i < len(issues); i++ {
currIssueID := issues[i].Key
currBranch := tree.AddBranch(currIssueID)
recurseTreeFetching(fetcher, currBranch, currIssueID)
}
}
This works, but it's pretty slow. I've looked at answers like this: /recursive-goroutines-what-is-the-neatest-way-to-tell-go-to-stop-reading-from-ch, but I'm struggling to get it to work.
I'm not limiting depth, nor checking for already added nodes.
I've tried "sprinkling on some concurrency." But the function deadlocks. Any guidance or fixes?
func main {
tree := treeprint.New()
var ch chan int
go recurseTreeFetching(fetcher, tree, *issueID, ch)
tocollect := 1
for n := 0; n < tocollect; n++ {
tocollect += <-ch
}
fmt.Println(tree.String())
}
func recurseTreeFetching(fetcher Fetcher, tree treeprint.Tree, issueID string, ch chan int) {
issues := fetcher.fetchIssues(issueID)
if len(issues) == 0 {
return
}
ch <- len(issues)
for i := 0; i < len(issues); i++ {
currIssueID := issues[i].Key
currBranch := tree.AddBranch(currIssueID)
go recurseTreeFetching(fetcher, currBranch, currIssueID, ch)
}
}
goroutine 1 [chan send (nil chan)]:? Include your error here.