From 8a9ac1b8064014c8bab32a509a3214b09beeb8a8 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 2 Apr 2015 13:05:03 -0400 Subject: [PATCH] go/loader: limit concurrency of I/O operations during parsing This is a putative fix for the file descriptor exhaustion problem described in https://github.com/golang/go/issues/10306. Change-Id: If603fb9bbaec1b53f6b44d15b2c202e4670035ce Reviewed-on: https://go-review.googlesource.com/8421 Reviewed-by: Matt Joiner Reviewed-by: David Crawshaw --- go/loader/util.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/go/loader/util.go b/go/loader/util.go index 1166c922..0404e991 100644 --- a/go/loader/util.go +++ b/go/loader/util.go @@ -17,6 +17,10 @@ import ( "golang.org/x/tools/go/buildutil" ) +// We use a counting semaphore to limit +// the number of parallel I/O calls per process. +var sema = make(chan bool, 10) + // parseFiles parses the Go source files within directory dir and // returns the ASTs of the ones that could be at least partially parsed, // along with a list of I/O and parse errors encountered. @@ -38,7 +42,11 @@ func parseFiles(fset *token.FileSet, ctxt *build.Context, displayPath func(strin } wg.Add(1) go func(i int, file string) { - defer wg.Done() + sema <- true // wait + defer func() { + wg.Done() + <-sema // signal + }() var rd io.ReadCloser var err error if ctxt.OpenFile != nil {