Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(vcs): refactor getCommits to use errgroup #3533

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions pkg/querier/vcs/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package vcs
import (
"context"
"fmt"
"sync"

"golang.org/x/sync/errgroup"

vcsv1 "github.com/grafana/pyroscope/api/gen/proto/go/vcs/v1"
)

const maxConcurrentRequests = 10

type gitHubCommitGetter interface {
GetCommit(context.Context, string, string, string) (*vcsv1.CommitInfo, error)
}
Expand All @@ -25,20 +28,25 @@ func getCommits(ctx context.Context, client gitHubCommitGetter, owner, repo stri
err error
}

var wg sync.WaitGroup
resultsCh := make(chan result, len(refs))
resultsCh := make(chan result, maxConcurrentRequests)
g, ctx := errgroup.WithContext(ctx)
g.SetLimit(maxConcurrentRequests)

for _, ref := range refs {
wg.Add(1)
go func(ref string) {
defer wg.Done()
ref := ref
g.Go(func() error {
commit, err := tryGetCommit(ctx, client, owner, repo, ref)
resultsCh <- result{commit, err}
}(ref)
select {
case resultsCh <- result{commit, err}:
case <-ctx.Done():
return ctx.Err()
}
return nil
})
}

go func() {
wg.Wait()
_ = g.Wait() // ignore errors since they're handled in the `resultsCh`.
close(resultsCh)
}()

Expand Down
Loading