diff --git a/internal/gobinaries/introspect_binaries.go b/internal/gobinaries/introspect_binaries.go index e36049f..06176d6 100644 --- a/internal/gobinaries/introspect_binaries.go +++ b/internal/gobinaries/introspect_binaries.go @@ -1,6 +1,10 @@ package gobinaries -import "fmt" +import ( + "fmt" + "runtime" + "sync" +) type IntrospectionResult struct { Binary GoBinary @@ -8,19 +12,31 @@ type IntrospectionResult struct { } func IntrospectBinaries(introspecter *Introspecter, binaryNames []string) []IntrospectionResult { - var results []IntrospectionResult - - for _, binaryName := range binaryNames { - binary, err := introspecter.Introspect(binaryName) - if err != nil { - err = fmt.Errorf("could not introspect binary %s: %w", binaryName, err) - } - - results = append(results, IntrospectionResult{ - Binary: binary, - Error: err, - }) + results := make([]IntrospectionResult, len(binaryNames)) + + var wg sync.WaitGroup + semaphore := make(chan struct{}, runtime.GOMAXPROCS(0)) + for i, binaryName := range binaryNames { + i, binaryName := i, binaryName + + wg.Add(1) + semaphore <- struct{}{} + go func() { + defer wg.Done() + defer func() { <-semaphore }() + + binary, err := introspecter.Introspect(binaryName) + if err != nil { + err = fmt.Errorf("could not introspect binary %s: %w", binaryName, err) + } + + results[i] = IntrospectionResult{ + Binary: binary, + Error: err, + } + }() } + wg.Wait() return results }