Skip to content

Commit

Permalink
addrmgr: set min value and optimize address chance
Browse files Browse the repository at this point in the history
This updates the (*KnownAddress).chance method in the following ways:
1. Apply a lower limit to the return value.  Using 0.01  as this minimum
   prevents tiny chance values from causing excessive iterations in
   (*AddrManager).GetAddress, currently the only caller.
2. Replace an inefficient loop with a single math.Pow.

These changes are mitigations to excessive CPU use in the GetAddress
method that manifest when the number of attempts for a KnownAddress
becomes large.  However, this method and much of addrmgr should be
rewritten in the future since there are some ad hoc and questionable
approaches to candidate address selection.
  • Loading branch information
chappjc authored and davecgh committed Jan 21, 2023
1 parent 1c09ad7 commit 3bc18a3
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions addrmgr/knownaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package addrmgr

import (
"math"
"sync"
"time"
)
Expand Down Expand Up @@ -69,19 +70,18 @@ func (ka *KnownAddress) chance() float64 {
lastAttempt = 0
}

c := 1.0
// Apply a lower limit to the value returned.
const minChance = 0.01

// Very recent attempts are less likely to be retried.
if lastAttempt < 10*time.Minute {
c *= 0.01
return minChance
}

// Failed attempts deprioritise.
for i := ka.attempts; i > 0; i-- {
c /= 1.5
}
c := 1.0 / math.Pow(1.5, float64(ka.attempts))

return c
return math.Max(c, minChance)
}

// isBad returns true if the address in question has not been tried in the last
Expand Down

0 comments on commit 3bc18a3

Please sign in to comment.