Skip to content

Commit

Permalink
fix: errgroup limit goroutine concurrency (#168)
Browse files Browse the repository at this point in the history
Signed-off-by: rfyiamcool <rfyiamcool@163.com>
  • Loading branch information
rfyiamcool committed Sep 13, 2023
1 parent 3bb726a commit 97b015e
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions internal/github/stars.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ type Stargazer struct {

// Stargazers returns all the stargazers of a given repo.
func (gh *GitHub) Stargazers(ctx context.Context, repo Repository) (stars []Stargazer, err error) {
sem := make(chan bool, 4)

if gh.totalPages(repo) > 400 {
return stars, ErrTooManyStars
}

var g errgroup.Group
var lock sync.Mutex
var (
wg errgroup.Group
lock sync.Mutex
)

wg.SetLimit(4)
for page := 1; page <= gh.lastPage(repo); page++ {
sem <- true
page := page
g.Go(func() error {
defer func() { <-sem }()
wg.Go(func() error {
result, err := gh.getStargazersPage(ctx, repo, page)
if errors.Is(err, errNoMorePages) {
return nil
Expand All @@ -53,7 +53,8 @@ func (gh *GitHub) Stargazers(ctx context.Context, repo Repository) (stars []Star
return nil
})
}
err = g.Wait()
err = wg.Wait()

sort.Slice(stars, func(i, j int) bool {
return stars[i].StarredAt.Before(stars[j].StarredAt)
})
Expand Down

0 comments on commit 97b015e

Please sign in to comment.