From aae23acff347aacfb701f483375955465097f443 Mon Sep 17 00:00:00 2001 From: Zachary Rice Date: Tue, 21 Dec 2021 18:10:29 -0600 Subject: [PATCH] limit goroutines on file scanning to avoid pegging them cores (#759) --- detect/files.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/detect/files.go b/detect/files.go index f2fd2b753..b449a9c99 100644 --- a/detect/files.go +++ b/detect/files.go @@ -20,6 +20,7 @@ func FromFiles(source string, cfg config.Config, outputOptions Options) ([]repor findings []report.Finding mu sync.Mutex ) + concurrentGoroutines := make(chan struct{}, 4) g, _ := errgroup.WithContext(context.Background()) paths := make(chan string) g.Go(func() error { @@ -41,12 +42,15 @@ func FromFiles(source string, cfg config.Config, outputOptions Options) ([]repor for pa := range paths { p := pa g.Go(func() error { + concurrentGoroutines <- struct{}{} b, err := os.ReadFile(p) if err != nil { + <-concurrentGoroutines return err } if !godocutil.IsText(b) { + <-concurrentGoroutines return nil } fis := DetectFindings(cfg, b, p, "") @@ -65,6 +69,7 @@ func FromFiles(source string, cfg config.Config, outputOptions Options) ([]repor findings = append(findings, fi) mu.Unlock() } + <-concurrentGoroutines return nil }) }