From af62386ecd8cbafb60516cb75a8115f4bd29ddc6 Mon Sep 17 00:00:00 2001 From: Julio Lopez <1953782+julio-lopez@users.noreply.github.com> Date: Wed, 22 Oct 2025 22:22:54 -0700 Subject: [PATCH] chore(general): modernize min/max (#4898) --- internal/epoch/epoch_manager.go | 6 +----- internal/gather/gather_bytes.go | 5 +---- internal/gather/gather_write_buffer.go | 5 +---- internal/retry/retry.go | 6 +----- internal/sleepable/sleepable_timer.go | 5 +---- internal/timetrack/estimator.go | 5 +---- internal/uitask/uitask_manager.go | 7 +++---- repo/blob/s3/s3_versioned_test.go | 8 +------- repo/format/format_blob.go | 5 +---- repo/splitter/splitter_buzhash32.go | 18 +++--------------- repo/splitter/splitter_rabinkarp64.go | 18 +++--------------- 11 files changed, 17 insertions(+), 71 deletions(-) diff --git a/internal/epoch/epoch_manager.go b/internal/epoch/epoch_manager.go index 5945d6575..46f7b8d7c 100644 --- a/internal/epoch/epoch_manager.go +++ b/internal/epoch/epoch_manager.go @@ -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 diff --git a/internal/gather/gather_bytes.go b/internal/gather/gather_bytes.go index 8fb34974f..8907fc109 100644 --- a/internal/gather/gather_bytes.go +++ b/internal/gather/gather_bytes.go @@ -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") diff --git a/internal/gather/gather_write_buffer.go b/internal/gather/gather_write_buffer.go index 6e1f0b4e9..2bafc72f5 100644 --- a/internal/gather/gather_write_buffer.go +++ b/internal/gather/gather_write_buffer.go @@ -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:] diff --git a/internal/retry/retry.go b/internal/retry/retry.go index c2b78b45c..6310c601d 100644 --- a/internal/retry/retry.go +++ b/internal/retry/retry.go @@ -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) diff --git a/internal/sleepable/sleepable_timer.go b/internal/sleepable/sleepable_timer.go index c2ac1ee5c..3de5a1bcc 100644 --- a/internal/sleepable/sleepable_timer.go +++ b/internal/sleepable/sleepable_timer.go @@ -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() diff --git a/internal/timetrack/estimator.go b/internal/timetrack/estimator.go index 617abad55..ef95ef5d5 100644 --- a/internal/timetrack/estimator.go +++ b/internal/timetrack/estimator.go @@ -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, diff --git a/internal/uitask/uitask_manager.go b/internal/uitask/uitask_manager.go index 3876959b6..4e36012e3 100644 --- a/internal/uitask/uitask_manager.go +++ b/internal/uitask/uitask_manager.go @@ -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 diff --git a/repo/blob/s3/s3_versioned_test.go b/repo/blob/s3/s3_versioned_test.go index 96f8a3720..77fcd0634 100644 --- a/repo/blob/s3/s3_versioned_test.go +++ b/repo/blob/s3/s3_versioned_test.go @@ -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]) } diff --git a/repo/format/format_blob.go b/repo/format/format_blob.go index 53ae50e39..65f35669c 100644 --- a/repo/format/format_blob.go +++ b/repo/format/format_blob.go @@ -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 diff --git a/repo/splitter/splitter_buzhash32.go b/repo/splitter/splitter_buzhash32.go index c6fe666ea..3304375c9 100644 --- a/repo/splitter/splitter_buzhash32.go +++ b/repo/splitter/splitter_buzhash32.go @@ -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) diff --git a/repo/splitter/splitter_rabinkarp64.go b/repo/splitter/splitter_rabinkarp64.go index 31899df50..9553d37f0 100644 --- a/repo/splitter/splitter_rabinkarp64.go +++ b/repo/splitter/splitter_rabinkarp64.go @@ -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)