Skip to content

Commit

Permalink
reduce slice grow
Browse files Browse the repository at this point in the history
  • Loading branch information
mlsmaycon committed Dec 30, 2024
1 parent 37c12be commit 8f6195f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
55 changes: 29 additions & 26 deletions management/server/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,37 +1046,32 @@ func (a *Account) connResourcesGenerator(ctx context.Context) (func(*PolicyRule,
// for destination group peers, call this method with an empty list of sourcePostureChecksIDs
func (a *Account) getAllPeersFromGroups(ctx context.Context, groups []string, peerID string, sourcePostureChecksIDs []string, validatedPeersMap map[string]struct{}) ([]*nbpeer.Peer, bool) {
peerInGroups := false
filteredPeers := make([]*nbpeer.Peer, 0, len(groups))
for _, g := range groups {
group, ok := a.Groups[g]
if !ok {
uniquePeerIDs := a.getUniquePeerIDsFromGroupsIDs(ctx, groups)
filteredPeers := make([]*nbpeer.Peer, 0, len(uniquePeerIDs))
for _, p := range uniquePeerIDs {
peer, ok := a.Peers[p]
if !ok || peer == nil {
continue
}

for _, p := range group.Peers {
peer, ok := a.Peers[p]
if !ok || peer == nil {
continue
}

// validate the peer based on policy posture checks applied
isValid := a.validatePostureChecksOnPeer(ctx, sourcePostureChecksIDs, peer.ID)
if !isValid {
continue
}

if _, ok := validatedPeersMap[peer.ID]; !ok {
continue
}
// validate the peer based on policy posture checks applied
isValid := a.validatePostureChecksOnPeer(ctx, sourcePostureChecksIDs, peer.ID)
if !isValid {
continue
}

if peer.ID == peerID {
peerInGroups = true
continue
}
if _, ok := validatedPeersMap[peer.ID]; !ok {
continue
}

filteredPeers = append(filteredPeers, peer)
if peer.ID == peerID {
peerInGroups = true
continue
}

filteredPeers = append(filteredPeers, peer)
}

return filteredPeers, peerInGroups
}

Expand Down Expand Up @@ -1360,20 +1355,28 @@ func (a *Account) getPostureValidPeers(inputPeers []string, postureChecksIDs []s
}

func (a *Account) getUniquePeerIDsFromGroupsIDs(ctx context.Context, groups []string) []string {
ids := make([]string, 0)
gObjs := make([]*Group, 0, len(groups))
tp := 0
for _, groupID := range groups {
group := a.GetGroup(groupID)
if group == nil {
log.WithContext(ctx).Warnf("group %s doesn't exist under account %s, will continue map generation without it", groupID, a.Id)
continue
}

if group.IsGroupAll() {
if group.IsGroupAll() || len(groups) == 1 {
return group.Peers
}

gObjs = append(gObjs, group)
tp += len(group.Peers)
}

ids := make([]string, 0, tp)
for _, group := range gObjs {
ids = append(ids, group.Peers...)
}

radix.Sort(ids)
return slices.Compact(ids)
}
Expand Down
3 changes: 3 additions & 0 deletions management/server/types/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func (p *Policy) RuleGroups() []string {

// SourceGroups returns a slice of all unique source groups referenced in the policy's rules.
func (p *Policy) SourceGroups() []string {
if len(p.Rules) == 1 {
return p.Rules[0].Sources
}
groups := make([]string, 0)
for _, rule := range p.Rules {
groups = append(groups, rule.Sources...)
Expand Down

0 comments on commit 8f6195f

Please sign in to comment.