Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: increase walker queue buffer & start walking early #579

Merged
merged 2 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions internal/langserver/handlers/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,30 @@ func (lh *logHandler) Initialize(ctx context.Context, params lsp.InitializeParam
}
walker.SetLogger(lh.logger)

var excludeModulePaths []string
for _, rawPath := range cfgOpts.ExcludeModulePaths {
modPath, err := resolvePath(rootDir, rawPath)
if err != nil {
lh.logger.Printf("Ignoring excluded module path %s: %s", rawPath, err)
continue
}
excludeModulePaths = append(excludeModulePaths, modPath)
}

walker.SetExcludeModulePaths(excludeModulePaths)
walker.EnqueuePath(fh.Dir())

// Walker runs asynchronously so we're intentionally *not*
// passing the request context here
walkerCtx := context.Background()

// Walker is also started early to allow gradual consumption
// and avoid overfilling the queue
err = walker.StartWalking(walkerCtx)
if err != nil {
return serverCaps, err
}

if len(params.WorkspaceFolders) > 0 {
for _, folderPath := range params.WorkspaceFolders {
modPath, err := pathFromDocumentURI(folderPath.URI)
Expand Down Expand Up @@ -176,24 +200,7 @@ func (lh *logHandler) Initialize(ctx context.Context, params lsp.InitializeParam
return serverCaps, nil
}

var excludeModulePaths []string
for _, rawPath := range cfgOpts.ExcludeModulePaths {
modPath, err := resolvePath(rootDir, rawPath)
if err != nil {
lh.logger.Printf("Ignoring excluded module path %s: %s", rawPath, err)
continue
}
excludeModulePaths = append(excludeModulePaths, modPath)
}

walker.SetExcludeModulePaths(excludeModulePaths)
walker.EnqueuePath(fh.Dir())

// Walker runs asynchronously so we're intentionally *not*
// passing the request context here
err = walker.StartWalking(context.Background())

return serverCaps, err
return serverCaps, nil
}

func resolvePath(rootDir, rawPath string) (string, error) {
Expand Down
27 changes: 27 additions & 0 deletions internal/langserver/handlers/initialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,30 @@ func TestInitialize_withInvalidRootURI(t *testing.T) {
"rootUri": "meh"
}`}, code.SystemError.Err())
}

func TestInitialize_multipleFolders(t *testing.T) {
rootDir := TempDir(t)
ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
rootDir.Dir(): validTfMockCalls(),
},
},
}))
stop := ls.Start(t)
defer stop()

ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {},
"rootUri": %q,
"processId": 12345,
"workspaceFolders": [
{
"uri": %q,
"name": "root"
}
]
}`, rootDir.URI(), rootDir.URI())})
}
11 changes: 10 additions & 1 deletion internal/terraform/module/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ type Walker struct {
excludeModulePaths map[string]bool
}

// queueCap represents channel buffer size
// which when reached causes EnqueuePath to block
// until a path is consumed
const queueCap = 50

func NewWalker(fs filesystem.Filesystem, modMgr ModuleManager) *Walker {
return &Walker{
fs: fs,
Expand All @@ -58,7 +63,7 @@ func NewWalker(fs filesystem.Filesystem, modMgr ModuleManager) *Walker {
walkingMu: &sync.RWMutex{},
queue: newWalkerQueue(fs),
queueMu: &sync.Mutex{},
pushChan: make(chan struct{}, 1),
pushChan: make(chan struct{}, queueCap),
doneCh: make(chan struct{}, 0),
}
}
Expand Down Expand Up @@ -100,6 +105,10 @@ func (w *Walker) EnqueuePath(path string) {
defer w.queueMu.Unlock()
heap.Push(w.queue, path)

w.triggerConsumption()
}

func (w *Walker) triggerConsumption() {
w.pushChan <- struct{}{}
}

Expand Down