Skip to content

Commit

Permalink
fix: wg.Done for all goroutines expected but not created
Browse files Browse the repository at this point in the history
whenever at least 1 of the n expected activities failed to scrape,
the pipeline stage of genImg would block because it created a
wg of size n.

To remediate this, a counter of the number of created go routines
is kept by the genImg pipeline stage. Once the input channel is
closed (no more graphs to turn into images), the stage wg.Done's
n minus the number of created goroutines.
  • Loading branch information
CamiloGarciaLaRotta committed Mar 30, 2019
1 parent 5dbf652 commit de7a2d5
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,37 @@ func genImg(in <-chan graph, size int) <-chan activityImage {
if err != nil {
log.Fatal(err)
}
labelColor := color.RGBA{88, 96, 105, 0xff}
valueColor := color.RGBA{149, 157, 165, 0xff}
axisColor := color.RGBA{108, 178, 103, 0xff}
polyColor := color.RGBA{123, 201, 111, 0xff}

var out = make(chan activityImage, size)
var wg sync.WaitGroup
wg.Add(size)
activeGoRoutines := 0
go func() {
for g := range in {
activeGoRoutines++
go func(g graph) {
defer wg.Done()
s := style{
LabelColor: color.RGBA{88, 96, 105, 0xff},
ValueColor: color.RGBA{149, 157, 165, 0xff},
AxisColor: color.RGBA{108, 178, 103, 0xff},
PolyColor: color.RGBA{123, 201, 111, 0xff},
MarkerRadius: 6,
LabelColor: labelColor,
ValueColor: valueColor,
AxisColor: axisColor,
PolyColor: polyColor,
LabelFont: truetype.NewFace(font, &truetype.Options{Size: 24}),
ValueFont: truetype.NewFace(font, &truetype.Options{Size: 22}),
MarkerRadius: 6,
}
out <- activityImage{img(g, s), g.Data.Year}
}(g)
}
// when input channel is closed, reduce the waitgroup counter
// by the number of goroutines that were expected but not created
for i := 0; i < size-activeGoRoutines; i++ {
wg.Done()
}
}()
go func() {
wg.Wait()
Expand Down

0 comments on commit de7a2d5

Please sign in to comment.