Skip to content

Commit

Permalink
Use sync/errgroup to fix firewall rules reconciliation
Browse files Browse the repository at this point in the history
- Fix a "send on closed channels" panic when multiple errors were returned
- Add wait group context cancelation
- End permissions tasks on first error raised
  • Loading branch information
vincentmrg committed Nov 27, 2024
1 parent 8ccde42 commit bb3d98b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ require (
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
36 changes: 8 additions & 28 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package provider
import (
"context"
"reflect"
"sync"

"github.com/go-logr/logr"
"golang.org/x/sync/errgroup"
)

// ReconcilePermissions perform create / delete on given permissions
Expand Down Expand Up @@ -84,36 +84,16 @@ func applyPermissions(
permFunc PermFunc,
permissions []IPPermission,
) error {
var wg sync.WaitGroup
cDone := make(chan bool)
cErr := make(chan error)

eg, egCtx := errgroup.WithContext(ctx)
for _, e := range permissions {
wg.Add(1)
go func(ctx context.Context, id string, req IPPermission) {
defer wg.Done()
if err := permFunc(ctx, log, id, req); err != nil {
cErr <- err
}
}(ctx, firewallRuleID, e)
}

// Final goroutine to wait until WaitGroup is done
go func() {
wg.Wait()
close(cDone)
}()

// Wait until either WaitGroup is done or an error is received through the channel
select {
case <-cDone:
break
case err := <-cErr:
close(cErr)
return err
func(p IPPermission) {
eg.Go(func() error {
return permFunc(egCtx, log, firewallRuleID, p)
})
}(e)
}

return nil
return eg.Wait()
}

// containsPermission returns if given Permission slice contains Permission.
Expand Down

0 comments on commit bb3d98b

Please sign in to comment.