-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid starting goroutines for memcached batch requests before gate (#…
…5301) Use the doWithBatch function to avoid starting goroutines to fetch batched results from memcached before they are allowed to run via the concurrency Gate. This avoids starting many goroutines which cannot make any progress due to a concurrency limit. Fixes #4967 Signed-off-by: Nick Pillitteri <[email protected]> Signed-off-by: Wiard van Rij <[email protected]>
- Loading branch information
1 parent
048b603
commit 18d1955
Showing
4 changed files
with
66 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package cacheutil | ||
|
||
import ( | ||
"context" | ||
|
||
"golang.org/x/sync/errgroup" | ||
|
||
"github.com/thanos-io/thanos/pkg/gate" | ||
) | ||
|
||
// doWithBatch do func with batch and gate. batchSize==0 means one batch. gate==nil means no gate. | ||
func doWithBatch(ctx context.Context, totalSize int, batchSize int, ga gate.Gate, f func(startIndex, endIndex int) error) error { | ||
if totalSize == 0 { | ||
return nil | ||
} | ||
if batchSize <= 0 { | ||
return f(0, totalSize) | ||
} | ||
g, ctx := errgroup.WithContext(ctx) | ||
for i := 0; i < totalSize; i += batchSize { | ||
j := i + batchSize | ||
if j > totalSize { | ||
j = totalSize | ||
} | ||
if ga != nil { | ||
if err := ga.Start(ctx); err != nil { | ||
return nil | ||
} | ||
} | ||
startIndex, endIndex := i, j | ||
g.Go(func() error { | ||
if ga != nil { | ||
defer ga.Done() | ||
} | ||
return f(startIndex, endIndex) | ||
}) | ||
} | ||
return g.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters