github.com/thanos-io/thanos@v0.32.5/pkg/cacheutil/cacheutil.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package cacheutil
     5  
     6  import (
     7  	"context"
     8  
     9  	"golang.org/x/sync/errgroup"
    10  
    11  	"github.com/thanos-io/thanos/pkg/gate"
    12  )
    13  
    14  // doWithBatch do func with batch and gate. batchSize==0 means one batch. gate==nil means no gate.
    15  func doWithBatch(ctx context.Context, totalSize int, batchSize int, ga gate.Gate, f func(startIndex, endIndex int) error) error {
    16  	if totalSize == 0 {
    17  		return nil
    18  	}
    19  	if batchSize <= 0 {
    20  		return f(0, totalSize)
    21  	}
    22  	g, ctx := errgroup.WithContext(ctx)
    23  	for i := 0; i < totalSize; i += batchSize {
    24  		j := i + batchSize
    25  		if j > totalSize {
    26  			j = totalSize
    27  		}
    28  		if ga != nil {
    29  			if err := ga.Start(ctx); err != nil {
    30  				return nil
    31  			}
    32  		}
    33  		startIndex, endIndex := i, j
    34  		g.Go(func() error {
    35  			if ga != nil {
    36  				defer ga.Done()
    37  			}
    38  			return f(startIndex, endIndex)
    39  		})
    40  	}
    41  	return g.Wait()
    42  }