Skip to content

Commit

Permalink
Use builtin min function
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalterman committed Aug 7, 2024
1 parent dfa83b4 commit ac0e3ab
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
5 changes: 2 additions & 3 deletions concurrency/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"go.uber.org/atomic"
"golang.org/x/sync/errgroup"

"github.com/grafana/dskit/internal/math"
"github.com/grafana/dskit/multierror"
)

Expand All @@ -31,7 +30,7 @@ func ForEachUser(ctx context.Context, userIDs []string, concurrency int, userFun
errsMx := sync.Mutex{}

wg := sync.WaitGroup{}
for ix := 0; ix < math.Min(concurrency, len(userIDs)); ix++ {
for ix := 0; ix < min(concurrency, len(userIDs)); ix++ {
wg.Add(1)
go func() {
defer wg.Done()
Expand Down Expand Up @@ -108,7 +107,7 @@ func ForEachJob(ctx context.Context, jobs int, concurrency int, jobFunc func(ctx

// Start workers to process jobs.
g, ctx := errgroup.WithContext(ctx)
for ix := 0; ix < math.Min(concurrency, jobs); ix++ {
for ix := 0; ix < min(concurrency, jobs); ix++ {
g.Go(func() error {
for ctx.Err() == nil {
idx := int(indexes.Inc())
Expand Down
1 change: 1 addition & 0 deletions internal/math/math.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package math

// Min returns the minimum of two ints.
// Deprecated: Use the builtin min function instead.
func Min(a, b int) int {
if a < b {
return a
Expand Down
5 changes: 2 additions & 3 deletions ring/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/grafana/dskit/flagext"
dsmath "github.com/grafana/dskit/internal/math"
"github.com/grafana/dskit/internal/slices"
"github.com/grafana/dskit/kv"
shardUtil "github.com/grafana/dskit/ring/shard"
Expand Down Expand Up @@ -423,7 +422,7 @@ func (r *Ring) findInstancesForKey(key uint32, op Operation, bufDescs []Instance
distinctHosts = bufHosts[:0]
distinctZones = bufZones[:0]
)
for i := start; len(distinctHosts) < dsmath.Min(maxInstances, n) && len(distinctZones) < maxZones && iterations < len(r.ringTokens); i++ {
for i := start; len(distinctHosts) < min(maxInstances, n) && len(distinctZones) < maxZones && iterations < len(r.ringTokens); i++ {
iterations++
// Wrap i around in the ring.
i %= len(r.ringTokens)
Expand Down Expand Up @@ -528,7 +527,7 @@ func (r *Ring) GetReplicationSetForOperation(op Operation) (ReplicationSet, erro
// Given data is replicated to RF different zones, we can tolerate a number of
// RF/2 failing zones. However, we need to protect from the case the ring currently
// contains instances in a number of zones < RF.
numReplicatedZones := dsmath.Min(len(r.ringZones), r.cfg.ReplicationFactor)
numReplicatedZones := min(len(r.ringZones), r.cfg.ReplicationFactor)
minSuccessZones := (numReplicatedZones / 2) + 1
maxUnavailableZones = minSuccessZones - 1

Expand Down
5 changes: 2 additions & 3 deletions ring/ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/grafana/dskit/concurrency"
"github.com/grafana/dskit/flagext"
"github.com/grafana/dskit/httpgrpc"
dsmath "github.com/grafana/dskit/internal/math"
"github.com/grafana/dskit/internal/slices"
"github.com/grafana/dskit/kv"
"github.com/grafana/dskit/kv/consul"
Expand Down Expand Up @@ -2953,9 +2952,9 @@ func BenchmarkRing_Get(b *testing.B) {
buf, bufHosts, bufZones := MakeBuffersForGet()
r := rand.New(rand.NewSource(time.Now().UnixNano()))

expectedInstances := dsmath.Min(benchCase.numInstances, benchCase.replicationFactor)
expectedInstances := min(benchCase.numInstances, benchCase.replicationFactor)
if ring.cfg.ZoneAwarenessEnabled {
expectedInstances = dsmath.Min(benchCase.numZones, benchCase.replicationFactor)
expectedInstances = min(benchCase.numZones, benchCase.replicationFactor)
}

b.Run(benchName, func(b *testing.B) {
Expand Down

0 comments on commit ac0e3ab

Please sign in to comment.