chore(general): modernize min/max (#4898)

This commit is contained in:
Julio Lopez
2025-10-22 22:22:54 -07:00
committed by GitHub
parent 11765d696c
commit af62386ecd
11 changed files with 17 additions and 71 deletions

View File

@@ -468,11 +468,7 @@ func (e *Manager) refreshLocked(ctx context.Context) error {
contentlog.Log2(ctx, e.log, "refresh attempt failed", logparam.Error("error", err), logparam.Duration("nextDelayTime", nextDelayTime))
time.Sleep(nextDelayTime)
nextDelayTime = time.Duration(float64(nextDelayTime) * maxRefreshAttemptSleepExponent)
if nextDelayTime > maxRefreshAttemptSleep {
nextDelayTime = maxRefreshAttemptSleep
}
nextDelayTime = min(time.Duration(float64(nextDelayTime)*maxRefreshAttemptSleepExponent), maxRefreshAttemptSleep)
}
return nil

View File

@@ -85,10 +85,7 @@ func (b *Bytes) AppendSectionTo(w io.Writer, offset, size int) error {
s := b.Slices[sliceNdx]
// l is how many bytes we consume out of the current slice
l := size
if l > len(s) {
l = len(s)
}
l := min(size, len(s))
if _, err := w.Write(s[0:l]); err != nil {
return errors.Wrap(err, "error appending")

View File

@@ -140,10 +140,7 @@ func (b *WriteBuffer) Append(data []byte) {
remaining = cap(b.inner.Slices[ndx]) - len(b.inner.Slices[ndx])
}
chunkSize := remaining
if chunkSize > len(data) {
chunkSize = len(data)
}
chunkSize := min(remaining, len(data))
b.inner.Slices[ndx] = append(b.inner.Slices[ndx], data[0:chunkSize]...)
data = data[chunkSize:]

View File

@@ -84,11 +84,7 @@ func internalRetry[T any](ctx context.Context, desc string, attempt func() (T, e
log(ctx).Debugf("got error %v when %v (#%v), sleeping for %v before retrying", err, desc, i, sleepAmount)
time.Sleep(sleepAmount)
sleepAmount = time.Duration(float64(sleepAmount) * factor)
if sleepAmount > maxSleep {
sleepAmount = maxSleep
}
sleepAmount = min(time.Duration(float64(sleepAmount)*factor), maxSleep)
}
return defaultT, errors.Wrapf(lastError, "unable to complete %v despite %v retries", desc, i)

View File

@@ -54,10 +54,7 @@ func NewTimer(nowFunc func() time.Time, until time.Time) *Timer {
return
}
nextSleepTime := until.Sub(now)
if nextSleepTime > maxSleepTime {
nextSleepTime = maxSleepTime
}
nextSleepTime := min(until.Sub(now), maxSleepTime)
if currentTimer != nil {
currentTimer.Stop()

View File

@@ -39,10 +39,7 @@ func (v Estimator) Estimate(completed, total float64) (Timings, bool) {
predictedSeconds := elapsed.Seconds() / completedRatio
predictedEndTime := v.startTime.Add(time.Duration(predictedSeconds) * time.Second)
dt := predictedEndTime.Sub(clock.Now()).Truncate(time.Second)
if dt < 0 {
dt = 0
}
dt := max(predictedEndTime.Sub(clock.Now()).Truncate(time.Second), 0)
return Timings{
PercentComplete: 100 * completed / total,

View File

@@ -106,10 +106,9 @@ func (m *Manager) WaitForTask(ctx context.Context, taskID string, maxWaitTime ti
deadline := clock.Now().Add(maxWaitTime)
sleepInterval := maxWaitTime / 10 //nolint:mnd
if sleepInterval > maxWaitInterval {
sleepInterval = maxWaitInterval
}
sleepInterval := min(
//nolint:mnd
maxWaitTime/10, maxWaitInterval)
if sleepInterval < minWaitInterval {
sleepInterval = minWaitInterval

View File

@@ -758,13 +758,7 @@ func compareMetadata(tb testing.TB, a, b versionMetadata) {
func compareVersionSlices(tb testing.TB, a, b []versionMetadata) {
tb.Helper()
l := len(a)
if len(b) < l {
l = len(b)
}
for i := range a[:l] {
for i := range min(len(a), len(b)) {
compareMetadata(tb, a[i], b[i])
}

View File

@@ -106,10 +106,7 @@ func RecoverFormatBlob(ctx context.Context, st blob.Storage, blobID blob.ID, opt
}
func recoverFormatBlobWithLength(ctx context.Context, st blob.Storage, blobID blob.ID, length int64) ([]byte, error) {
chunkLength := int64(maxRecoverChunkLength)
if chunkLength > length {
chunkLength = length
}
chunkLength := min(int64(maxRecoverChunkLength), length)
if chunkLength <= minRecoverableChunkLength {
return nil, errFormatBlobNotFound

View File

@@ -28,17 +28,8 @@ func (rs *buzhash32Splitter) NextSplitPoint(b []byte) int {
// until minSize, only hash the last splitterSlidingWindowSize bytes
if left := rs.minSize - rs.count - 1; left > 0 {
fastPathBytes = left
if fastPathBytes > len(b) {
fastPathBytes = len(b)
}
var i int
i = fastPathBytes - splitterSlidingWindowSize
if i < 0 {
i = 0
}
fastPathBytes = min(left, len(b))
i := max(fastPathBytes-splitterSlidingWindowSize, 0)
for ; i < fastPathBytes; i++ {
rs.rh.Roll(b[i])
@@ -50,10 +41,7 @@ func (rs *buzhash32Splitter) NextSplitPoint(b []byte) int {
// until the max size, check if we have any splitting point
if left := rs.maxSize - rs.count; left > 0 {
fp := left
if fp >= len(b) {
fp = len(b)
}
fp := min(left, len(b))
for i, b := range b[0:fp] {
rs.rh.Roll(b)

View File

@@ -28,17 +28,8 @@ func (rs *rabinKarp64Splitter) NextSplitPoint(b []byte) int {
// until minSize, only hash the last splitterSlidingWindowSize bytes
if left := rs.minSize - rs.count - 1; left > 0 {
fastPathBytes = left
if fastPathBytes > len(b) {
fastPathBytes = len(b)
}
var i int
i = fastPathBytes - splitterSlidingWindowSize
if i < 0 {
i = 0
}
fastPathBytes = min(left, len(b))
i := max(fastPathBytes-splitterSlidingWindowSize, 0)
for ; i < fastPathBytes; i++ {
rs.rh.Roll(b[i])
@@ -50,10 +41,7 @@ func (rs *rabinKarp64Splitter) NextSplitPoint(b []byte) int {
// until the max size, check if we have any splitting point
if left := rs.maxSize - rs.count; left > 0 {
fp := left
if fp >= len(b) {
fp = len(b)
}
fp := min(left, len(b))
for i, b := range b[0:fp] {
rs.rh.Roll(b)