|
8 | 8 | "fmt"
|
9 | 9 | "path"
|
10 | 10 | "path/filepath"
|
| 11 | + "runtime" |
11 | 12 | "sort"
|
12 | 13 | "strconv"
|
13 | 14 | "strings"
|
@@ -114,19 +115,31 @@ type commitInfo struct {
|
114 | 115 | err error
|
115 | 116 | }
|
116 | 117 |
|
117 |
| -// GetCommitsInfo takes advantages of concurrey to speed up getting information |
118 |
| -// of all commits that are corresponding to these entries. |
119 |
| -// TODO: limit max goroutines number should be configurable |
| 118 | +// GetCommitsInfo takes advantages of concurrency to speed up getting information |
| 119 | +// of all commits that are corresponding to these entries. This method will automatically |
| 120 | +// choose the right number of goroutine (concurrency) to use related of the host CPU. |
120 | 121 | func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interface{}, error) {
|
| 122 | + return tes.GetCommitsInfoWithCustomConcurrency(commit, treePath, 0) |
| 123 | +} |
| 124 | + |
| 125 | +// GetCommitsInfoWithCustomConcurrency takes advantages of concurrency to speed up getting information |
| 126 | +// of all commits that are corresponding to these entries. If the given maxConcurrency is negative or |
| 127 | +// equal to zero: the right number of goroutine (concurrency) to use will be choosen related of the |
| 128 | +// host CPU. |
| 129 | +func (tes Entries) GetCommitsInfoWithCustomConcurrency(commit *Commit, treePath string, maxConcurrency int) ([][]interface{}, error) { |
121 | 130 | if len(tes) == 0 {
|
122 | 131 | return nil, nil
|
123 | 132 | }
|
124 | 133 |
|
| 134 | + if maxConcurrency <= 0 { |
| 135 | + maxConcurrency = runtime.NumCPU() |
| 136 | + } |
| 137 | + |
125 | 138 | // Length of taskChan determines how many goroutines (subprocesses) can run at the same time.
|
126 | 139 | // The length of revChan should be same as taskChan so goroutines whoever finished job can
|
127 | 140 | // exit as early as possible, only store data inside channel.
|
128 |
| - taskChan := make(chan bool, 10) |
129 |
| - revChan := make(chan commitInfo, 10) |
| 141 | + taskChan := make(chan bool, maxConcurrency) |
| 142 | + revChan := make(chan commitInfo, maxConcurrency) |
130 | 143 | doneChan := make(chan error)
|
131 | 144 |
|
132 | 145 | // Receive loop will exit when it collects same number of data pieces as tree entries.
|
|
0 commit comments