Fix updater guard pattern (#255)

This commit is contained in:
Quentin McGaw
2020-10-01 17:56:14 -04:00
committed by GitHub
parent ecf76896a2
commit fbecbc1c82
2 changed files with 12 additions and 7 deletions

View File

@@ -77,8 +77,7 @@ func (u *updater) updatePIAOld(ctx context.Context) (err error) {
region := strings.TrimSuffix(fileName, ".ovpn")
guard <- struct{}{}
wg.Add(1)
go resolvePIAHostname(ctx, wg, region, hosts, u.lookupIP, errors, serversCh)
<-guard
go resolvePIAHostname(ctx, wg, region, hosts, u.lookupIP, errors, serversCh, guard)
}
for range contents {
select {
@@ -101,8 +100,11 @@ func (u *updater) updatePIAOld(ctx context.Context) (err error) {
func resolvePIAHostname(ctx context.Context, wg *sync.WaitGroup,
region string, hosts []string, lookupIP lookupIPFunc,
errors chan<- error, serversCh chan<- models.PIAServer) {
defer wg.Done()
errors chan<- error, serversCh chan<- models.PIAServer, guard chan<- struct{}) {
defer func() {
guard <- struct{}{}
wg.Done()
}()
var IPs []net.IP //nolint:prealloc
// usually one single host in this case
// so no need to run in goroutines the for loop below