Skip to content

Commit

Permalink
use errgroup
Browse files Browse the repository at this point in the history
  • Loading branch information
hunshcn committed May 14, 2024
1 parent df9986b commit b8a9774
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
1 change: 1 addition & 0 deletions gazelle/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use_repo(
"com_github_smacker_go_tree_sitter",
"com_github_stretchr_testify",
"in_gopkg_yaml_v2",
"org_golang_x_sync",
)

non_module_deps = use_extension("//:def.bzl", "non_module_deps")
Expand Down
1 change: 1 addition & 0 deletions gazelle/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/ghodss/yaml v1.0.0
github.com/smacker/go-tree-sitter v0.0.0-20240422154435-0628b34cbf9c
github.com/stretchr/testify v1.9.0
golang.org/x/sync v0.2.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
2 changes: 2 additions & 0 deletions gazelle/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAG
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
1 change: 1 addition & 0 deletions gazelle/python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ go_library(
"@com_github_emirpasic_gods//utils",
"@com_github_smacker_go_tree_sitter//:go-tree-sitter",
"@com_github_smacker_go_tree_sitter//python",
"@org_golang_x_sync//errgroup",
],
)

Expand Down
50 changes: 18 additions & 32 deletions gazelle/python/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ import (
"context"
_ "embed"
"fmt"
"path/filepath"
"strings"
"sync"

"github.com/emirpasic/gods/sets/treeset"
godsutils "github.com/emirpasic/gods/utils"
"golang.org/x/sync/errgroup"
)

// python3Parser implements a parser for Python files that extracts the modules
Expand Down Expand Up @@ -64,43 +63,30 @@ func (p *python3Parser) parseSingle(pyFilename string) (*treeset.Set, map[string
func (p *python3Parser) parse(pyFilenames *treeset.Set) (*treeset.Set, map[string]*treeset.Set, *annotations, error) {
modules := treeset.NewWith(moduleComparator)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var wg sync.WaitGroup
g, ctx := errgroup.WithContext(context.Background())
ch := make(chan struct{}, 6) // Limit the number of concurrent parses.
chRes := make(chan *ParserOutput, len(pyFilenames.Values()))
chErr := make(chan error, 1)
for _, v := range pyFilenames.Values() {
wg.Add(1)
go func(filename string) {
defer func() {
<-ch
wg.Done()
}()
if err := ctx.Err(); err != nil {
return
}
ch <- struct{}{}
res, err := NewFileParser().ParseFile(ctx, p.repoRoot, p.relPackagePath, filename)
if err != nil {
cancel()
select {
case chErr <- fmt.Errorf("failed to parse %s: %w", filepath.Join(p.relPackagePath, filename), err):
default:
ch <- struct{}{}
g.Go(func(filename string) func() error {
return func() error {
defer func() {
<-ch
}()
res, err := NewFileParser().ParseFile(ctx, p.repoRoot, p.relPackagePath, filename)
if err != nil {
return err
}
return
chRes <- res
return nil
}
chRes <- res
}(v.(string))
}(v.(string)))
}
wg.Wait()
close(chErr)
close(chRes)
if len(chErr) > 0 {
return nil, nil, nil, <-chErr
if err := g.Wait(); err != nil {
return nil, nil, nil, err
}

close(ch)
close(chRes)
mainModules := make(map[string]*treeset.Set, len(chRes))
allAnnotations := new(annotations)
allAnnotations.ignore = make(map[string]struct{})
Expand Down

0 comments on commit b8a9774

Please sign in to comment.