Skip to content

Commit 8c27f9a

Browse files
holimannibty
authored andcommitted
p2p/msgrate: be more lenient when calculating 'mean' (ethereum#25653)
The p2p msgrate tracker is a thing which tries to estimate some mean round-trip times. However, it did so in a very curious way: if a node had 200 peers, it would sort their 200 respective rtt estimates, and then it would pick item number 2 as the mean. So effectively taking third fastest and calling it mean. This probably works "ok" when the number of peers are low (there are other factors too, such as ttlScaling which takes some of the edge off this) -- however when the number of peers is high, it becomes very skewed. This PR instead bases the 'mean' on the square root of the length of the list. Still pretty harsh, but a bit more lenient.
1 parent 6562394 commit 8c27f9a

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

p2p/msgrate/msgrate.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ const measurementImpact = 0.1
3838
// to fetch more than some local stable value.
3939
const capacityOverestimation = 1.01
4040

41-
// qosTuningPeers is the number of best peers to tune round trip times based on.
42-
// An Ethereum node doesn't need hundreds of connections to operate correctly,
43-
// so instead of lowering our download speed to the median of potentially many
44-
// bad nodes, we can target a smaller set of vey good nodes. At worse this will
45-
// result in less nodes to sync from, but that's still better than some hogging
46-
// the pipeline.
47-
const qosTuningPeers = 5
48-
4941
// rttMinEstimate is the minimal round trip time to target requests for. Since
5042
// every request entails a 2 way latency + bandwidth + serving database lookups,
5143
// it should be generous enough to permit meaningful work to be done on top of
@@ -303,11 +295,15 @@ func (t *Trackers) medianRoundTrip() time.Duration {
303295
}
304296
sort.Float64s(rtts)
305297

306-
median := rttMaxEstimate
307-
if qosTuningPeers <= len(rtts) {
308-
median = time.Duration(rtts[qosTuningPeers/2]) // Median of our best few peers
309-
} else if len(rtts) > 0 {
310-
median = time.Duration(rtts[len(rtts)/2]) // Median of all out connected peers
298+
var median time.Duration
299+
switch len(rtts) {
300+
case 0:
301+
median = rttMaxEstimate
302+
case 1:
303+
median = time.Duration(rtts[0])
304+
default:
305+
idx := int(math.Sqrt(float64(len(rtts))))
306+
median = time.Duration(rtts[idx])
311307
}
312308
// Restrict the RTT into some QoS defaults, irrelevant of true RTT
313309
if median < rttMinEstimate {

0 commit comments

Comments
 (0)