mirror of
https://github.com/fosrl/gerbil.git
synced 2026-02-07 21:46:40 +00:00
Merge pull request #39 from LaurenceJJones/fix/calcpeerbandwidth-optimization
feat: optimize calculatePeerBandwidth to avoid nested loops
This commit is contained in:
14
main.go
14
main.go
@@ -1003,8 +1003,13 @@ func calculatePeerBandwidth() ([]PeerBandwidth, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
// Track the set of peers currently present on the device to prune stale readings efficiently
|
||||
currentPeerKeys := make(map[string]struct{}, len(device.Peers))
|
||||
|
||||
for _, peer := range device.Peers {
|
||||
publicKey := peer.PublicKey.String()
|
||||
currentPeerKeys[publicKey] = struct{}{}
|
||||
|
||||
currentReading := PeerReading{
|
||||
BytesReceived: peer.ReceiveBytes,
|
||||
BytesTransmitted: peer.TransmitBytes,
|
||||
@@ -1061,14 +1066,7 @@ func calculatePeerBandwidth() ([]PeerBandwidth, error) {
|
||||
|
||||
// Clean up old peers
|
||||
for publicKey := range lastReadings {
|
||||
found := false
|
||||
for _, peer := range device.Peers {
|
||||
if peer.PublicKey.String() == publicKey {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
if _, exists := currentPeerKeys[publicKey]; !exists {
|
||||
delete(lastReadings, publicKey)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user