MCPcopy Create free account
hub / github.com/cortexproject/cortex / ForEachUser

Function ForEachUser

pkg/util/concurrency/runner.go:15–59  ·  view source on GitHub ↗

ForEachUser runs the provided userFunc for each userIDs up to concurrency concurrent workers. In case userFunc returns error, it will continue to process remaining users but returns an error with all errors userFunc has returned.

(ctx context.Context, userIDs []string, concurrency int, userFunc func(ctx context.Context, userID string) error)

Source from the content-addressed store, hash-verified

13// In case userFunc returns error, it will continue to process remaining users but returns an
14// error with all errors userFunc has returned.
15func ForEachUser(ctx context.Context, userIDs []string, concurrency int, userFunc func(ctx context.Context, userID string) error) error {
16 if len(userIDs) == 0 {
17 return nil
18 }
19
20 // Push all jobs to a channel.
21 ch := make(chan string, len(userIDs))
22 for _, userID := range userIDs {
23 ch <- userID
24 }
25 close(ch)
26
27 // Keep track of all errors occurred.
28 errs := multierror.MultiError{}
29 errsMx := sync.Mutex{}
30
31 wg := sync.WaitGroup{}
32 routines := min(concurrency, len(userIDs))
33 for range routines {
34 wg.Go(func() {
35
36 for userID := range ch {
37 // Ensure the context has not been canceled (ie. shutdown has been triggered).
38 if ctx.Err() != nil {
39 break
40 }
41
42 if err := userFunc(ctx, userID); err != nil {
43 errsMx.Lock()
44 errs.Add(err)
45 errsMx.Unlock()
46 }
47 }
48 })
49 }
50
51 // wait for ongoing workers to finish.
52 wg.Wait()
53
54 if ctx.Err() != nil {
55 return ctx.Err()
56 }
57
58 return errs.Err()
59}
60
61// ForEach runs the provided jobFunc for each job up to concurrency concurrent workers.
62// The execution breaks on first error encountered.

Callers 10

ListAllConfigsMethod · 0.92
cleanUpActiveUsersMethod · 0.92
cleanDeletedUsersMethod · 0.92
shipBlocksMethod · 0.92
compactBlocksMethod · 0.92
ListAllRulesMethod · 0.92
TestForEachUserFunction · 0.85

Calls 4

AddMethod · 0.95
ErrMethod · 0.95
ErrMethod · 0.65
WaitMethod · 0.45