Skip to content

Commit

Permalink
Get rid of WaitGroup, make cancel called with sync.One
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Aug 5, 2019
1 parent ab8f702 commit 0b23177
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions pkg/setup/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ func (sn *Node) createRoute(expireAt time.Time, route routing.Route, rport, lpor
// indicate errors occurred during rules setup
rulesSetupErrs := make(chan error, len(r))

var rulesSetupDone sync.WaitGroup
rulesSetupDone.Add(len(r))
// context to cancel rule setup in case of errors
ctx, cancel := context.WithCancel(context.Background())
for idx := len(r) - 1; idx >= 0; idx-- {
Expand All @@ -209,12 +207,12 @@ func (sn *Node) createRoute(expireAt time.Time, route routing.Route, rport, lpor
}

go func(ctx context.Context, hop *Hop, rule routing.Rule) {
defer rulesSetupDone.Done()

routeID, err := sn.setupRule(ctx, hop.To, rule)
if err != nil {
// filter out context cancellation errors
if err != context.Canceled {
if err == context.Canceled {
rulesSetupErrs <- err
} else {
rulesSetupErrs <- fmt.Errorf("rule setup: %s", err)
}
return
Expand All @@ -227,23 +225,22 @@ func (sn *Node) createRoute(expireAt time.Time, route routing.Route, rport, lpor
}(ctx, hop, rule)
}

var err error
var rulesSetupErr error
var cancelOnce sync.Once
// check for any errors occurred so far
for range r {
if err = <-rulesSetupErrs; err != nil {
// filter out context cancellation errors
if err := <-rulesSetupErrs; err != nil && err != context.Canceled {
// rules setup failed, cancel further setup
cancel()

// wait for setup to complete
rulesSetupDone.Wait()
break
cancelOnce.Do(cancel)
rulesSetupErr = err
}
}

// close chan to avoid leaks
close(rulesSetupErrs)
if err != nil {
return 0, err
if rulesSetupErr != nil {
return 0, rulesSetupErr
}

rule := routing.ForwardRule(expireAt, r[0].routeID, r[0].Transport)
Expand Down

0 comments on commit 0b23177

Please sign in to comment.