Skip to content

Commit

Permalink
discovery/azure: more robust handling of go routines (#5106)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Pasquier <[email protected]>
  • Loading branch information
simonpasquier authored Jan 18, 2019
1 parent a1f34be commit 68e4c21
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions discovery/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net"
"net/http"
"strings"
"sync"
"time"

"github.com/Azure/azure-sdk-for-go/arm/compute"
Expand Down Expand Up @@ -324,9 +325,12 @@ func (d *Discovery) refresh() (tg *targetgroup.Group, err error) {
err error
}

var wg sync.WaitGroup
wg.Add(len(machines))
ch := make(chan target, len(machines))
for i, vm := range machines {
go func(i int, vm virtualMachine) {
defer wg.Done()
r, err := newAzureResourceFromID(vm.ID, d.logger)
if err != nil {
ch <- target{labelSet: nil, err: err}
Expand Down Expand Up @@ -374,7 +378,6 @@ func (d *Discovery) refresh() (tg *targetgroup.Group, err error) {
// is a cheap and easy way to determine if a machine is allocated or not.
if networkInterface.Properties.Primary == nil {
level.Debug(d.logger).Log("msg", "Skipping deallocated virtual machine", "machine", vm.Name)
ch <- target{}
return
}

Expand All @@ -395,15 +398,13 @@ func (d *Discovery) refresh() (tg *targetgroup.Group, err error) {
}
}
}

// If we get here we haven't sent anything to the channel.
// We need to send it something to release it.
ch <- target{}
}(i, vm)
}

for range machines {
tgt := <-ch
wg.Wait()
close(ch)

for tgt := range ch {
if tgt.err != nil {
return nil, fmt.Errorf("unable to complete Azure service discovery: %s", err)
}
Expand Down

0 comments on commit 68e4c21

Please sign in to comment.