diff --git a/go.mod b/go.mod index 7d25d98..badbb89 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b108257..9ca8e17 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/provider/utils.go b/internal/provider/utils.go index 831d47a..53c50c6 100644 --- a/internal/provider/utils.go +++ b/internal/provider/utils.go @@ -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 @@ -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.