1
0
mirror of https://github.com/golang/go synced 2024-09-30 16:08:36 -06:00

internal/lsp: prevent too much open files issues

On MacOS the default open file limit is 256 files per process. For big
projects this could cause issues when reading all files (more or less)
at the same time.

By moving up the `parseLimit` so the reading of the file is only done
when it is allowed to start parsing the file, we stay well below the 256
files per process (as the `parseLimit` is currently set to 20).

Since `parseLimit` is actually only used to limit IO access, let's also
rename the const to `parseLimit`.

Change-Id: Ie8744030875d84d0d6095ee4ec2d9d553911bed1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/189437
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Sander van Harmelen 2019-08-08 18:18:43 +02:00 committed by Rebecca Stambler
parent b346f7fd45
commit 128824a23e
2 changed files with 8 additions and 5 deletions

View File

@ -18,8 +18,8 @@ import (
errors "golang.org/x/xerrors"
)
// Limits the number of parallel parser calls per process.
var parseLimit = make(chan struct{}, 20)
// Limits the number of parallel file reads per process.
var ioLimit = make(chan struct{}, 20)
// parseKey uniquely identifies a parsed Go file.
type parseKey struct {
@ -77,12 +77,14 @@ func (h *parseGoHandle) Parse(ctx context.Context) (*ast.File, error) {
func parseGo(ctx context.Context, c *cache, fh source.FileHandle, mode source.ParseMode) (*ast.File, error) {
ctx, done := trace.StartSpan(ctx, "cache.parseGo", telemetry.File.Of(fh.Identity().URI.Filename()))
defer done()
ioLimit <- struct{}{}
buf, _, err := fh.Read(ctx)
<-ioLimit // Make sure to release the token, even when an error is returned.
if err != nil {
return nil, err
}
parseLimit <- struct{}{}
defer func() { <-parseLimit }()
parserMode := parser.AllErrors | parser.ParseComments
if mode == source.ParseHeader {
parserMode = parser.ImportsOnly | parser.ParseComments

View File

@ -509,7 +509,8 @@ func (v *view) findFile(uri span.URI) (viewFile, error) {
pathStat, err := os.Stat(fname)
if os.IsNotExist(err) {
return nil, err
} else if err != nil {
}
if err != nil {
return nil, nil // the file may exist, return without an error
}
for _, c := range candidates {