p2p/msgrate: return capacity as integer, clamp to max uint32 (#22943)
* p2p/msgrate: return capacity as integer * eth/protocols/snap: remove conversions * p2p/msgrate: add overflow test * p2p/msgrate: make the capacity overflow test actually overflow * p2p/msgrate: clamp capacity to max int32 * p2p/msgrate: fix min/max confusion
This commit is contained in:
@ -19,6 +19,7 @@ package msgrate
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
@ -162,7 +163,7 @@ func NewTracker(caps map[uint64]float64, rtt time.Duration) *Tracker {
|
||||
// the load proportionally to the requested items, so fetching a bit more might
|
||||
// still take the same RTT. By forcefully overshooting by a small amount, we can
|
||||
// avoid locking into a lower-that-real capacity.
|
||||
func (t *Tracker) Capacity(kind uint64, targetRTT time.Duration) float64 {
|
||||
func (t *Tracker) Capacity(kind uint64, targetRTT time.Duration) int {
|
||||
t.lock.RLock()
|
||||
defer t.lock.RUnlock()
|
||||
|
||||
@ -171,7 +172,14 @@ func (t *Tracker) Capacity(kind uint64, targetRTT time.Duration) float64 {
|
||||
|
||||
// Return an overestimation to force the peer out of a stuck minima, adding
|
||||
// +1 in case the item count is too low for the overestimator to dent
|
||||
return 1 + capacityOverestimation*throughput
|
||||
return roundCapacity(1 + capacityOverestimation*throughput)
|
||||
}
|
||||
|
||||
// roundCapacity gives the integer value of a capacity.
|
||||
// The result fits int32, and is guaranteed to be positive.
|
||||
func roundCapacity(cap float64) int {
|
||||
const maxInt32 = float64(1<<31 - 1)
|
||||
return int(math.Min(maxInt32, math.Max(1, math.Ceil(cap))))
|
||||
}
|
||||
|
||||
// Update modifies the peer's capacity values for a specific data type with a new
|
||||
@ -435,7 +443,7 @@ func (t *Trackers) detune() {
|
||||
|
||||
// Capacity is a helper function to access a specific tracker without having to
|
||||
// track it explicitly outside.
|
||||
func (t *Trackers) Capacity(id string, kind uint64, targetRTT time.Duration) float64 {
|
||||
func (t *Trackers) Capacity(id string, kind uint64, targetRTT time.Duration) int {
|
||||
t.lock.RLock()
|
||||
defer t.lock.RUnlock()
|
||||
|
||||
|
28
p2p/msgrate/msgrate_test.go
Normal file
28
p2p/msgrate/msgrate_test.go
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright 2021 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package msgrate
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCapacityOverflow(t *testing.T) {
|
||||
tracker := NewTracker(nil, 1)
|
||||
tracker.Update(1, 1, 100000)
|
||||
cap := tracker.Capacity(1, 10000000)
|
||||
if int32(cap) < 0 {
|
||||
t.Fatalf("Negative: %v", int32(cap))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user