utils: switch to standard library min and max functions (#4218)

These functions were added in Go 1.21.
This commit is contained in:
Marten Seemann
2023-12-28 12:19:13 +07:00
committed by GitHub
parent 18c591c75a
commit 22411e16d5
24 changed files with 44 additions and 78 deletions

View File

@@ -3,27 +3,11 @@ package utils
import (
"math"
"time"
"golang.org/x/exp/constraints"
)
// InfDuration is a duration of infinite length
const InfDuration = time.Duration(math.MaxInt64)
func Max[T constraints.Ordered](a, b T) T {
if a < b {
return b
}
return a
}
func Min[T constraints.Ordered](a, b T) T {
if a < b {
return a
}
return b
}
// MinNonZeroDuration return the minimum duration that's not zero.
func MinNonZeroDuration(a, b time.Duration) time.Duration {
if a == 0 {
@@ -32,7 +16,7 @@ func MinNonZeroDuration(a, b time.Duration) time.Duration {
if b == 0 {
return a
}
return Min(a, b)
return min(a, b)
}
// MinTime returns the earlier time

View File

@@ -8,16 +8,6 @@ import (
)
var _ = Describe("Min / Max", func() {
It("returns the maximum", func() {
Expect(Max(5, 7)).To(Equal(7))
Expect(Max(5.5, 5.7)).To(Equal(5.7))
})
It("returns the minimum", func() {
Expect(Min(5, 7)).To(Equal(5))
Expect(Min(5.5, 5.7)).To(Equal(5.5))
})
It("returns the maximum time", func() {
a := time.Now()
b := a.Add(time.Second)

View File

@@ -55,7 +55,7 @@ func (r *RTTStats) PTO(includeMaxAckDelay bool) time.Duration {
if r.SmoothedRTT() == 0 {
return 2 * defaultInitialRTT
}
pto := r.SmoothedRTT() + Max(4*r.MeanDeviation(), protocol.TimerGranularity)
pto := r.SmoothedRTT() + max(4*r.MeanDeviation(), protocol.TimerGranularity)
if includeMaxAckDelay {
pto += r.MaxAckDelay()
}
@@ -126,6 +126,6 @@ func (r *RTTStats) OnConnectionMigration() {
// is larger. The mean deviation is increased to the most recent deviation if
// it's larger.
func (r *RTTStats) ExpireSmoothedMetrics() {
r.meanDeviation = Max(r.meanDeviation, (r.smoothedRTT - r.latestRTT).Abs())
r.smoothedRTT = Max(r.smoothedRTT, r.latestRTT)
r.meanDeviation = max(r.meanDeviation, (r.smoothedRTT - r.latestRTT).Abs())
r.smoothedRTT = max(r.smoothedRTT, r.latestRTT)
}