Skip to content

Commit f9aed5b

Browse files
committed
feat: use conc pool to speed up
1 parent e1b7749 commit f9aed5b

File tree

3 files changed

+71
-38
lines changed

3 files changed

+71
-38
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/google/go-github/v53 v53.2.0
77
github.com/make-go-great/color-go v0.5.0
88
github.com/make-go-great/netrc-go v0.0.2
9+
github.com/sourcegraph/conc v0.3.0
910
github.com/urfave/cli/v2 v2.25.7
1011
golang.org/x/oauth2 v0.8.0
1112
)
@@ -21,6 +22,8 @@ require (
2122
github.com/mattn/go-isatty v0.0.17 // indirect
2223
github.com/russross/blackfriday/v2 v2.1.0 // indirect
2324
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
25+
go.uber.org/atomic v1.7.0 // indirect
26+
go.uber.org/multierr v1.9.0 // indirect
2427
golang.org/x/crypto v0.7.0 // indirect
2528
golang.org/x/net v0.10.0 // indirect
2629
golang.org/x/sys v0.8.0 // indirect

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEM
66
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
77
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
88
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
9+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
910
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
11+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1012
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
1113
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
1214
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -30,13 +32,22 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
3032
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
3133
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
3234
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
35+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3336
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
3437
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
38+
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
39+
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
40+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
41+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
3542
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
3643
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
3744
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
3845
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
3946
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
47+
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
48+
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
49+
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
50+
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
4051
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4152
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
4253
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=

internal/cli/action_overlook.go

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package cli
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"regexp"
78
"sort"
9+
"sync"
810
"text/tabwriter"
911
"time"
1012

13+
"github.com/sourcegraph/conc/pool"
1114
"github.com/urfave/cli/v2"
1215
)
1316

17+
const maxPoolGoroutine = 8
18+
1419
var reGitHub = regexp.MustCompile(`github\.com/([^/]*)/([^/]*)`)
1520

1621
type GitHubRepoData struct {
@@ -40,47 +45,61 @@ func (a *action) Overlook(c *cli.Context) error {
4045
// To avoid duplicate
4146
mGHRepoData := make(map[string]struct{})
4247

48+
p := pool.New().WithMaxGoroutines(maxPoolGoroutine)
49+
var mMutex sync.RWMutex
50+
var listMutex sync.Mutex
4351
for module := range mapImportedModules {
44-
if !reGitHub.MatchString(module) {
45-
continue
46-
}
47-
48-
parts := reGitHub.FindStringSubmatch(module)
49-
if len(parts) != 3 {
50-
continue
51-
}
52-
53-
ghRepoName := parts[0]
54-
if _, ok := mGHRepoData[ghRepoName]; ok {
55-
continue
56-
}
57-
mGHRepoData[ghRepoName] = struct{}{}
58-
59-
owner := parts[1]
60-
repo := parts[2]
61-
62-
ghRepo, _, err := a.ghClient.Repositories.Get(c.Context, owner, repo)
63-
if err != nil {
64-
a.log("Failed to get GitHub %s/%s: %s\n", owner, repo, err)
65-
}
66-
67-
var ghStar int
68-
if ghRepo.StargazersCount != nil {
69-
ghStar = *ghRepo.StargazersCount
70-
}
71-
72-
var ghUpdatedAt time.Time
73-
if ghRepo.UpdatedAt != nil {
74-
ghUpdatedAt = ghRepo.UpdatedAt.Time
75-
}
76-
77-
listGHRepoData = append(listGHRepoData, GitHubRepoData{
78-
UpdatedAt: ghUpdatedAt,
79-
Name: ghRepoName,
80-
Star: ghStar,
52+
module := module
53+
p.Go(func() {
54+
if !reGitHub.MatchString(module) {
55+
return
56+
}
57+
58+
parts := reGitHub.FindStringSubmatch(module)
59+
if len(parts) != 3 {
60+
return
61+
}
62+
63+
ghRepoName := parts[0]
64+
mMutex.RLock()
65+
if _, ok := mGHRepoData[ghRepoName]; ok {
66+
mMutex.RUnlock()
67+
return
68+
}
69+
mMutex.RUnlock()
70+
71+
mMutex.Lock()
72+
mGHRepoData[ghRepoName] = struct{}{}
73+
mMutex.Unlock()
74+
75+
owner := parts[1]
76+
repo := parts[2]
77+
78+
ghRepo, _, err := a.ghClient.Repositories.Get(context.Background(), owner, repo)
79+
if err != nil {
80+
a.log("Failed to get GitHub %s/%s: %s\n", owner, repo, err)
81+
}
82+
83+
var ghStar int
84+
if ghRepo.StargazersCount != nil {
85+
ghStar = *ghRepo.StargazersCount
86+
}
87+
88+
var ghUpdatedAt time.Time
89+
if ghRepo.UpdatedAt != nil {
90+
ghUpdatedAt = ghRepo.UpdatedAt.Time
91+
}
92+
93+
listMutex.Lock()
94+
listGHRepoData = append(listGHRepoData, GitHubRepoData{
95+
UpdatedAt: ghUpdatedAt,
96+
Name: ghRepoName,
97+
Star: ghStar,
98+
})
99+
listMutex.Unlock()
81100
})
82-
83101
}
102+
p.Wait()
84103

85104
// Sort for consistency
86105
sort.Slice(listGHRepoData, func(i, j int) bool {

0 commit comments

Comments
 (0)