Skip to content

Commit

Permalink
Stagger lint and breaking checks in LSP Refresh (#3547)
Browse files Browse the repository at this point in the history
  • Loading branch information
doriable authored Dec 16, 2024
1 parent b277cf8 commit 0560ffe
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions private/buf/buflsp/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os"
"slices"
"strings"
"time"

"github.com/bufbuild/buf/private/buf/bufworkspace"
"github.com/bufbuild/buf/private/bufpkg/bufanalysis"
Expand All @@ -43,7 +44,10 @@ import (
"go.lsp.dev/protocol"
)

const descriptorPath = "google/protobuf/descriptor.proto"
const (
descriptorPath = "google/protobuf/descriptor.proto"
refreshCheckStagger = 5 * time.Millisecond
)

// file is a file that has been opened by the client.
//
Expand Down Expand Up @@ -285,9 +289,28 @@ func (f *file) Refresh(ctx context.Context) {
f.FindModule(ctx)

progress.Report(ctx, "Running Checks", 4.0/6)
f.BuildImages(ctx)
f.RunLints(ctx)
f.RunBreaking(ctx)
// Since checks are a more expensive operation, we do not want to run a check on every
// Refresh call. Instead, we can stagger the checks and only run them periodically by
// spinning them off into a go routine. Then we attempt to lock using the top-level LSP
// lock. It is safe to use because if another LSP call is made, we allow checks to finish
// before resolving a subsequent LSP request.
go func() {
// We stagger the check operation by 5ms and run it for the latest Refresh state.
time.Sleep(refreshCheckStagger)
// Call TryLock, if unnsuccessful, then another thread holds the lock, so we provide a
// debug log and move on.
if !f.lsp.lock.TryLock() {
f.lsp.logger.Debug(
fmt.Sprintf("another thread holds the LSP lock, no new checks started for %v", f.uri),
)
return
}
// We have successfully obtained the lock, we can now run the checks.
defer f.lsp.lock.Unlock()
f.BuildImages(ctx)
f.RunLints(ctx)
f.RunBreaking(ctx)
}()

progress.Report(ctx, "Indexing Symbols", 5.0/6)
f.IndexSymbols(ctx)
Expand Down

0 comments on commit 0560ffe

Please sign in to comment.