mirror of
https://github.com/fosrl/gerbil.git
synced 2026-03-04 01:36:46 +00:00
main: optimize calculatePeerBandwidth to avoid nested peer scans
Build a set of current peer public keys during the primary iteration and prune lastReadings in a single pass, removing the O(n^2) nested loop. No behavior change; improves efficiency when peer lists and lastReadings grow large.
This commit is contained in:
18
main.go
18
main.go
@@ -639,7 +639,7 @@ func ensureMSSClamping() error {
|
|||||||
if out, err := addCmd.CombinedOutput(); err != nil {
|
if out, err := addCmd.CombinedOutput(); err != nil {
|
||||||
errMsg := fmt.Sprintf("Failed to add MSS clamping rule for chain %s: %v (output: %s)",
|
errMsg := fmt.Sprintf("Failed to add MSS clamping rule for chain %s: %v (output: %s)",
|
||||||
chain, err, string(out))
|
chain, err, string(out))
|
||||||
logger.Error(errMsg)
|
logger.Error("%s", errMsg)
|
||||||
errors = append(errors, fmt.Errorf("%s", errMsg))
|
errors = append(errors, fmt.Errorf("%s", errMsg))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -656,7 +656,7 @@ func ensureMSSClamping() error {
|
|||||||
if out, err := checkCmd.CombinedOutput(); err != nil {
|
if out, err := checkCmd.CombinedOutput(); err != nil {
|
||||||
errMsg := fmt.Sprintf("Rule verification failed for chain %s: %v (output: %s)",
|
errMsg := fmt.Sprintf("Rule verification failed for chain %s: %v (output: %s)",
|
||||||
chain, err, string(out))
|
chain, err, string(out))
|
||||||
logger.Error(errMsg)
|
logger.Error("%s", errMsg)
|
||||||
errors = append(errors, fmt.Errorf("%s", errMsg))
|
errors = append(errors, fmt.Errorf("%s", errMsg))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -1003,8 +1003,13 @@ func calculatePeerBandwidth() ([]PeerBandwidth, error) {
|
|||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
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 {
|
for _, peer := range device.Peers {
|
||||||
publicKey := peer.PublicKey.String()
|
publicKey := peer.PublicKey.String()
|
||||||
|
currentPeerKeys[publicKey] = struct{}{}
|
||||||
|
|
||||||
currentReading := PeerReading{
|
currentReading := PeerReading{
|
||||||
BytesReceived: peer.ReceiveBytes,
|
BytesReceived: peer.ReceiveBytes,
|
||||||
BytesTransmitted: peer.TransmitBytes,
|
BytesTransmitted: peer.TransmitBytes,
|
||||||
@@ -1061,14 +1066,7 @@ func calculatePeerBandwidth() ([]PeerBandwidth, error) {
|
|||||||
|
|
||||||
// Clean up old peers
|
// Clean up old peers
|
||||||
for publicKey := range lastReadings {
|
for publicKey := range lastReadings {
|
||||||
found := false
|
if _, exists := currentPeerKeys[publicKey]; !exists {
|
||||||
for _, peer := range device.Peers {
|
|
||||||
if peer.PublicKey.String() == publicKey {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
delete(lastReadings, publicKey)
|
delete(lastReadings, publicKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user