Closed yuleib closed 8 months ago
PR Description updated to latest commit (https://github.com/kubescape/storage/commit/67e8e96dc6fd6bba539873708198da7d58312184)
sync
package to manage the synchronization of these goroutines.๐ก General suggestions: The PR is generally well-structured and the use of goroutines should improve the performance of the network policy generation process. However, it would be beneficial to add some tests to ensure that the parallelization works as expected and does not introduce any race conditions.
relevant file | pkg/apis/softwarecomposition/networkpolicy/networkpolicy.go |
suggestion | Consider using a mutex to protect the shared resources (`networkPolicy.Spec.Ingress`, `networkPolicy.Spec.Egress`, `generatedNetworkPolicy.PoliciesRef`, `ingressHash`, `egressHash`) from concurrent access which could lead to race conditions. [important] |
relevant line | "+ go func(neighborIngress softwarecomposition.NetworkNeighbor) {" |
relevant file | pkg/apis/softwarecomposition/networkpolicy/networkpolicy.go |
suggestion | The `wg.Done()` should be deferred after `wg.Add(1)` to ensure it's called even if the function panics. [important] |
relevant line | "+ wg.Done()" |
We see the generating is very fast, is there is no need to add routines
Type
enhancement
Description
sync
package and declared async.WaitGroup
variablewg
at the global scope.GenerateNetworkPolicy
function, goroutines are created for each ingress and egress neighbor, and thewg.Add(1)
andwg.Done()
calls are used to manage the wait group counter.wg.Wait()
call to block until all goroutines have finished their execution.PR changes walkthrough
1 files
networkpolicy.go
pkg/apis/softwarecomposition/networkpolicy/networkpolicy.go
**The changes in this file are focused on improving the
performance of the network policy generation process. This
is achieved by introducing goroutines to handle the
generation of ingress and egress rules in parallel. A
`sync.WaitGroup` is used to ensure all goroutines complete
their execution before proceeding. The `sync` package is
imported and a `sync.WaitGroup` variable `wg` is declared at
the global scope. In the `GenerateNetworkPolicy` function,
goroutines are created for each ingress and egress neighbor,
and the `wg.Add(1)` and `wg.Done()` calls are used to manage
the wait group counter. Finally, a `wg.Wait()` call is added
to block until all goroutines have finished their execution.**
User description
adding routines for better performance of NP