Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: async calls #311

Merged
merged 6 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
24 changes: 15 additions & 9 deletions pkg/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type AnalysisStatus string
const (
StateOK AnalysisStatus = "OK"
StateProblemDetected AnalysisStatus = "ProblemDetected"
MaxConcurrent int = 10
AlexsJones marked this conversation as resolved.
Show resolved Hide resolved
)

type JsonOutput struct {
Expand Down Expand Up @@ -110,15 +111,16 @@ func (a *Analysis) RunAnalysis() []error {
}

var errorList []error

semaphore := make(chan struct{}, MaxConcurrent)
// if there are no filters selected and no active_filters then run all of them
if len(a.Filters) == 0 && len(activeFilters) == 0 {
var wg sync.WaitGroup
var mutex sync.Mutex
for _, analyzer := range analyzerMap {
wg.Add(1)
go func(analyzer common.IAnalyzer) {

semaphore <- struct{}{}
go func(analyzer common.IAnalyzer, wg *sync.WaitGroup, semaphore chan struct{}) {
defer wg.Done()
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
mutex.Lock()
Expand All @@ -128,23 +130,24 @@ func (a *Analysis) RunAnalysis() []error {
mutex.Lock()
a.Results = append(a.Results, results...)
mutex.Unlock()
wg.Done()
}(analyzer)
<-semaphore
}(analyzer, &wg, semaphore)

}
wg.Wait()
return errorList
}

semaphore = make(chan struct{}, MaxConcurrent)
// if the filters flag is specified
if len(a.Filters) != 0 {
var wg sync.WaitGroup
var mutex sync.Mutex
for _, filter := range a.Filters {
if analyzer, ok := analyzerMap[filter]; ok {
semaphore <- struct{}{}
wg.Add(1)
go func(analyzer common.IAnalyzer) {

defer wg.Done()
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
mutex.Lock()
Expand All @@ -154,7 +157,7 @@ func (a *Analysis) RunAnalysis() []error {
mutex.Lock()
a.Results = append(a.Results, results...)
mutex.Unlock()
wg.Done()
<-semaphore
}(analyzer)
} else {
errorList = append(errorList, fmt.Errorf(fmt.Sprintf("\"%s\" filter does not exist. Please run k8sgpt filters list.", filter)))
Expand All @@ -166,11 +169,14 @@ func (a *Analysis) RunAnalysis() []error {

var wg sync.WaitGroup
var mutex sync.Mutex
semaphore = make(chan struct{}, MaxConcurrent)
// use active_filters
for _, filter := range activeFilters {
if analyzer, ok := analyzerMap[filter]; ok {
semaphore <- struct{}{}
wg.Add(1)
go func(analyzer common.IAnalyzer) {
defer wg.Done()
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
mutex.Lock()
Expand All @@ -180,7 +186,7 @@ func (a *Analysis) RunAnalysis() []error {
mutex.Lock()
a.Results = append(a.Results, results...)
mutex.Unlock()
wg.Done()
<-semaphore
}(analyzer)
}
}
Expand Down