From 36d4c216718374c927e59c968396067ee16cb6d5 Mon Sep 17 00:00:00 2001 From: bcmmbaga Date: Thu, 18 Jul 2024 20:37:29 +0300 Subject: [PATCH] Optimize group change effects on account peers --- management/server/group.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/management/server/group.go b/management/server/group.go index 5926681d3..220582346 100644 --- a/management/server/group.go +++ b/management/server/group.go @@ -165,12 +165,18 @@ func (am *DefaultAccountManager) SaveGroups(ctx context.Context, accountID, user eventsToStore = append(eventsToStore, events...) } - account.Network.IncSerial() + updateAccountPeers := areGroupChangesAffectPeers(account, newGroups) + if updateAccountPeers { + account.Network.IncSerial() + } + if err = am.Store.SaveGroups(account.Id, account.Groups); err != nil { return err } - am.updateAccountPeers(ctx, account) + if updateAccountPeers { + am.updateAccountPeers(ctx, account) + } for _, storeEvent := range eventsToStore { storeEvent() @@ -318,10 +324,9 @@ func (am *DefaultAccountManager) GroupAddPeer(ctx context.Context, accountID, gr group.Peers = append(group.Peers, peerID) } - updateAccountPeers := false - if areGroupChangesAffectPeers(account, []string{groupID}) { + updateAccountPeers := areGroupChangesAffectPeers(account, []*nbgroup.Group{group}) + if updateAccountPeers { account.Network.IncSerial() - updateAccountPeers = true } if err = am.Store.SaveAccount(ctx, account); err != nil { @@ -350,10 +355,9 @@ func (am *DefaultAccountManager) GroupDeletePeer(ctx context.Context, accountID, return status.Errorf(status.NotFound, "group with ID %s not found", groupID) } - updateAccountPeers := false - if areGroupChangesAffectPeers(account, []string{groupID}) { + updateAccountPeers := areGroupChangesAffectPeers(account, []*nbgroup.Group{group}) + if updateAccountPeers { account.Network.IncSerial() - updateAccountPeers = true } for i, itemID := range group.Peers { @@ -372,19 +376,15 @@ func (am *DefaultAccountManager) GroupDeletePeer(ctx context.Context, accountID, return nil } -func areGroupChangesAffectPeers(account *Account, groups []string) bool { - for _, groupID := range groups { - if _, exists := account.Groups[groupID]; !exists { - continue - } - - if linked, _ := isGroupLinkedToDns(account.NameServerGroups, groupID); linked { +func areGroupChangesAffectPeers(account *Account, groups []*nbgroup.Group) bool { + for _, group := range groups { + if linked, _ := isGroupLinkedToDns(account.NameServerGroups, group.ID); linked { return true } - if linked, _ := isGroupLinkedToPolicy(account.Policies, groupID); linked { + if linked, _ := isGroupLinkedToPolicy(account.Policies, group.ID); linked { return true } - if linked, _ := isGroupLinkedToRoute(account.Routes, groupID); linked { + if linked, _ := isGroupLinkedToRoute(account.Routes, group.ID); linked { return true } }