Skip to content

Commit f2ef59b

Browse files
authored
Can set a custom max number of goroutine
1 parent cddfff0 commit f2ef59b

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

tree_entry.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"path"
1010
"path/filepath"
11+
"runtime"
1112
"sort"
1213
"strconv"
1314
"strings"
@@ -114,19 +115,31 @@ type commitInfo struct {
114115
err error
115116
}
116117

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.
120121
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) {
121130
if len(tes) == 0 {
122131
return nil, nil
123132
}
124133

134+
if maxConcurrency <= 0 {
135+
maxConcurrency = runtime.NumCPU()
136+
}
137+
125138
// Length of taskChan determines how many goroutines (subprocesses) can run at the same time.
126139
// The length of revChan should be same as taskChan so goroutines whoever finished job can
127140
// 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)
130143
doneChan := make(chan error)
131144

132145
// Receive loop will exit when it collects same number of data pieces as tree entries.

0 commit comments

Comments
 (0)