refactor: parallelize report commands

This will make reports a bit faster to run, and is safe to do as reports should not change app state.
This commit is contained in:
Jose Diaz-Gonzalez
2020-12-29 23:56:10 -05:00
parent 44461e21df
commit 28c9f42a25
9 changed files with 237 additions and 120 deletions

View File

@@ -132,3 +132,27 @@ func createParallelWorkerPool(jobs chan string, results chan parallelCommandResu
wg.Wait()
close(results)
}
type ReportFunc func(string) string
func CollectReport(appName string, flags map[string]ReportFunc) map[string]string {
var sm sync.Map
var wg sync.WaitGroup
for flag, fn := range flags {
wg.Add(1)
go func(flag string, fn ReportFunc) {
defer wg.Done()
s := fn(appName)
sm.Store(flag, s)
}(flag, fn)
}
wg.Wait()
infoFlags := map[string]string{}
sm.Range(func(key interface{}, value interface{}) bool {
infoFlags[key.(string)] = value.(string)
return true
})
return infoFlags
}