Replace hashstructure package with r3labs/diff for network map updates

This commit is contained in:
bcmmbaga
2024-06-28 15:44:38 +03:00
parent 16387a823a
commit 42be72a86c
5 changed files with 56 additions and 38 deletions
+40 -26
View File
@@ -1,7 +1,7 @@
package server
import (
"github.com/mitchellh/hashstructure/v2"
"github.com/r3labs/diff"
log "github.com/sirupsen/logrus"
)
@@ -36,12 +36,12 @@ func updateAccountPeers(account *Account) {
func updateAccountPeersWithHash(account *Account) {
//start := time.Now()
var skipUpdate int
defer func() {
//duration := time.Since(start)
//log.Printf("Finished execution of updateAccountPeers, took %v\n", duration.Nanoseconds())
//log.Println("not updated peers: ", skipUpdate)
}()
//var skipUpdate int
//defer func() {
// duration := time.Since(start)
// log.Printf("Finished execution of updateAccountPeers, took %v\n", duration.Nanoseconds())
// log.Println("not updated peers: ", skipUpdate)
//}()
peers := account.GetPeers()
approvedPeersMap := make(map[string]struct{}, len(peers))
@@ -57,25 +57,39 @@ func updateAccountPeersWithHash(account *Account) {
remotePeerNetworkMap := account.GetPeerNetworkMap(peer.ID, "netbird.io", approvedPeersMap)
//log.Println("firewall rules: ", len(remotePeerNetworkMap.FirewallRules))
hashStr, err := hashstructure.Hash(remotePeerNetworkMap, hashstructure.FormatV2, &hashstructure.HashOptions{
ZeroNil: true,
IgnoreZeroValue: true,
SlicesAsSets: true,
UseStringer: true,
})
if err != nil {
log.Errorf("failed to generate network map hash: %v", err)
} else {
if peer.NetworkMapHash == hashStr {
//log.Debugf("not sending network map update to peer: %s as there is nothing new", peer.ID)
skipUpdate++
continue
}
peer.NetworkMapHash = hashStr
}
//hashStr, err := hashstructure.Hash(remotePeerNetworkMap, hashstructure.FormatV2, &hashstructure.HashOptions{
// ZeroNil: true,
// IgnoreZeroValue: true,
// SlicesAsSets: true,
// UseStringer: true,
// //Hasher: xxhash.New(),
//})
//if err != nil {
// log.Errorf("failed to generate network map hash: %v", err)
//} else {
// if peer.NetworkMapHash == hashStr {
// //log.Debugf("not sending network map update to peer: %s as there is nothing new", peer.ID)
// skipUpdate++
// continue
// }
// peer.NetworkMapHash = hashStr
//}
//postureChecks := am.getPeerPostureChecks(account, peer)
//update := toSyncResponse(nil, peer, nil, remotePeerNetworkMap, am.GetDNSDomain(), postureChecks)
//am.peersUpdateManager.SendUpdate(peer.ID, &UpdateMessage{Update: update})update
if peer.NetworkMap == nil {
peer.NetworkMap = remotePeerNetworkMap
} else {
changelog, err := diff.Diff(peer.NetworkMap, remotePeerNetworkMap)
if err != nil {
log.Errorf("failed to generate network map diff: %v", err)
} else {
if len(changelog) == 0 {
continue
}
}
}
}
}
//48868101197
// 8700718125
+2 -2
View File
@@ -40,9 +40,9 @@ type Network struct {
Dns string
// Serial is an ID that increments by 1 when any change to the network happened (e.g. new peer has been added).
// Used to synchronize state to the client apps.
Serial uint64 `hash:"ignore"`
Serial uint64 `diff:"-"`
mu sync.Mutex `json:"-" gorm:"-" hash:"ignore"`
mu sync.Mutex `json:"-" gorm:"-" diff:"-"`
}
// NewNetwork creates a new Network initializing it with a Serial=0
+11 -10
View File
@@ -18,37 +18,38 @@ type Peer struct {
// WireGuard public key
Key string `gorm:"index"`
// A setup key this peer was registered with
SetupKey string
SetupKey string `diff:"-"`
// IP address of the Peer
IP net.IP `gorm:"serializer:json"`
// Meta is a Peer system meta data
Meta PeerSystemMeta `gorm:"embedded;embeddedPrefix:meta_"`
Meta PeerSystemMeta `gorm:"embedded;embeddedPrefix:meta_" diff:"-"`
// Name is peer's name (machine name)
Name string
// DNSLabel is the parsed peer name for domain resolution. It is used to form an FQDN by appending the account's
// domain to the peer label. e.g. peer-dns-label.netbird.cloud
DNSLabel string
// Status peer's management connection status
Status *PeerStatus `gorm:"embedded;embeddedPrefix:peer_status_"`
Status *PeerStatus `gorm:"embedded;embeddedPrefix:peer_status_" diff:"-"`
// The user ID that registered the peer
UserID string
UserID string `diff:"-"`
// SSHKey is a public SSH key of the peer
SSHKey string
// SSHEnabled indicates whether SSH server is enabled on the peer
SSHEnabled bool
// LoginExpirationEnabled indicates whether peer's login expiration is enabled and once expired the peer has to re-login.
// Works with LastLogin
LoginExpirationEnabled bool
LoginExpirationEnabled bool `diff:"-"`
// LastLogin the time when peer performed last login operation
LastLogin time.Time
LastLogin time.Time `diff:"-"`
// CreatedAt records the time the peer was created
CreatedAt time.Time
CreatedAt time.Time `diff:"-"`
// Indicate ephemeral peer attribute
Ephemeral bool
Ephemeral bool `diff:"-"`
// Geo location based on connection IP
Location Location `gorm:"embedded;embeddedPrefix:location_"`
Location Location `gorm:"embedded;embeddedPrefix:location_" diff:"-"`
NetworkMapHash uint64 `hash:"ignore"`
NetworkMap any `diff:"-"`
//NetworkMapHash uint64 `hash:"ignore"`
}
type PeerStatus struct { //nolint:revive