another log and different approach for meta calc

This commit is contained in:
crn4 2025-06-18 18:16:45 +02:00
parent 9dc9402deb
commit ebf3d26c91
3 changed files with 31 additions and 50 deletions

View File

@ -166,7 +166,8 @@ func (s *GRPCServer) Sync(req *proto.EncryptedMessage, srv proto.ManagementServi
ctx = context.WithValue(ctx, nbContext.AccountIDKey, accountID)
realIP := getRealIP(ctx)
log.WithContext(ctx).Debugf("Sync request from peer [%s] [%s] [%s]", req.WgPubKey, realIP.String(), metaHash(extractPeerMeta(ctx, syncReq.GetMeta())))
sip := realIP.String()
log.WithContext(ctx).Debugf("Sync request from peer [%s] [%s] [%s]", req.WgPubKey, sip, metaHash(extractPeerMeta(ctx, syncReq.GetMeta()), sip))
if syncReq.GetMeta() == nil {
log.WithContext(ctx).Tracef("peer system meta has to be provided on sync. Peer %s, remote addr %s", peerKey.String(), realIP)

View File

@ -15,6 +15,9 @@ const (
filterTimeout = 5 * time.Minute // Duration to secure the previous login information in the filter
loggingLimit = 100
loggingLimitOnePeer = 30
loggingTresholdOnePeer = 5 * time.Minute
)
type loginFilter struct {
@ -53,6 +56,13 @@ func (l *loginFilter) addLogin(wgPubKey, metaHash string) {
"elapsed time for number of attempts": time.Since(mh.start),
"number of syncs": mh.counter,
}).Info(mh.prepareHashes())
} else if mh.counter%loggingLimitOnePeer == 0 && time.Since(mh.start) > loggingTresholdOnePeer && len(mh.hashes) == 1 {
log.WithFields(log.Fields{
"wgPubKey": wgPubKey,
"elapsed time for number of attempts": time.Since(mh.start),
"number of syncs": mh.counter,
}).Info(mh.prepareHashes())
mh.start = time.Now()
}
l.logged[wgPubKey] = mh
}
@ -66,8 +76,10 @@ func (m *metahash) prepareHashes() string {
return sb.String()
}
func metaHash(meta nbpeer.PeerSystemMeta) string {
estimatedSize := len(meta.WtVersion) + len(meta.OSVersion) + len(meta.KernelVersion) + len(meta.Hostname) + len(meta.SystemSerialNumber) + 4
func metaHash(meta nbpeer.PeerSystemMeta, pubip string) string {
mac := getMacAddress(meta.NetworkAddresses)
estimatedSize := len(meta.WtVersion) + len(meta.OSVersion) + len(meta.KernelVersion) + len(meta.Hostname) + len(meta.SystemSerialNumber) +
len(pubip) + len(mac) + 6
var b strings.Builder
b.Grow(estimatedSize)
@ -81,6 +93,21 @@ func metaHash(meta nbpeer.PeerSystemMeta) string {
b.WriteString(meta.Hostname)
b.WriteByte('|')
b.WriteString(meta.SystemSerialNumber)
b.WriteByte('|')
b.WriteString(pubip)
b.WriteByte('|')
b.WriteString(mac)
return b.String()
}
func getMacAddress(nas []nbpeer.NetworkAddress) string {
if len(nas) == 0 {
return ""
}
macs := make([]string, 0, len(nas))
for _, na := range nas {
macs = append(macs, na.Mac)
}
return strings.Join(macs, "/")
}

View File

@ -1,47 +0,0 @@
package server
import (
"fmt"
"hash/fnv"
"testing"
"github.com/netbirdio/netbird/management/server/peer"
nbpeer "github.com/netbirdio/netbird/management/server/peer"
)
func BenchmarkMetaHash(b *testing.B) {
meta := peer.PeerSystemMeta{
WtVersion: "1.0.0",
OSVersion: "Linux 5.4.0",
KernelVersion: "5.4.0-42-generic",
Hostname: "test-host",
}
b.Run("fnv", func(b *testing.B) {
for i := 0; i < b.N; i++ {
metaHashFnv(meta)
}
})
b.Run("builder", func(b *testing.B) {
for i := 0; i < b.N; i++ {
metaHash(meta)
}
})
b.Run("strings", func(b *testing.B) {
for i := 0; i < b.N; i++ {
metaHashStrings(meta)
}
})
}
func metaHashStrings(meta nbpeer.PeerSystemMeta) string {
return meta.WtVersion + meta.OSVersion + meta.KernelVersion + meta.Hostname
}
func metaHashFnv(meta nbpeer.PeerSystemMeta) string {
h := fnv.New64a()
h.Write([]byte(meta.WtVersion))
h.Write([]byte(meta.OSVersion))
h.Write([]byte(meta.KernelVersion))
h.Write([]byte(meta.Hostname))
return fmt.Sprintf("%x", h.Sum64())
}