|
1 | 1 | package cli |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "os" |
6 | 7 | "regexp" |
7 | 8 | "sort" |
| 9 | + "sync" |
8 | 10 | "text/tabwriter" |
9 | 11 | "time" |
10 | 12 |
|
| 13 | + "github.com/sourcegraph/conc/pool" |
11 | 14 | "github.com/urfave/cli/v2" |
12 | 15 | ) |
13 | 16 |
|
| 17 | +const maxPoolGoroutine = 8 |
| 18 | + |
14 | 19 | var reGitHub = regexp.MustCompile(`github\.com/([^/]*)/([^/]*)`) |
15 | 20 |
|
16 | 21 | type GitHubRepoData struct { |
@@ -40,47 +45,61 @@ func (a *action) Overlook(c *cli.Context) error { |
40 | 45 | // To avoid duplicate |
41 | 46 | mGHRepoData := make(map[string]struct{}) |
42 | 47 |
|
| 48 | + p := pool.New().WithMaxGoroutines(maxPoolGoroutine) |
| 49 | + var mMutex sync.RWMutex |
| 50 | + var listMutex sync.Mutex |
43 | 51 | 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() |
81 | 100 | }) |
82 | | - |
83 | 101 | } |
| 102 | + p.Wait() |
84 | 103 |
|
85 | 104 | // Sort for consistency |
86 | 105 | sort.Slice(listGHRepoData, func(i, j int) bool { |
|
0 commit comments