diff --git a/internal/lsp/diagnostics.go b/internal/lsp/diagnostics.go index bef6dccd46e..6cdb71a4230 100644 --- a/internal/lsp/diagnostics.go +++ b/internal/lsp/diagnostics.go @@ -18,9 +18,6 @@ func (s *Server) cacheAndDiagnose(ctx context.Context, uri span.URI, content str return err } go func() { - //TODO: this is an ugly hack to make the diagnostics call happen after the - // configuration is collected, we need to rewrite all the concurrency - <-s.configured ctx := s.view.BackgroundContext() if ctx.Err() != nil { return diff --git a/internal/lsp/protocol/protocol.go b/internal/lsp/protocol/protocol.go index e5737f8b573..123358d0df4 100644 --- a/internal/lsp/protocol/protocol.go +++ b/internal/lsp/protocol/protocol.go @@ -11,12 +11,16 @@ import ( "golang.org/x/tools/internal/jsonrpc2" ) +const defaultMessageBufferSize = 20 + func canceller(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) { conn.Notify(context.Background(), "$/cancelRequest", &CancelParams{ID: *req.ID}) } func NewClient(stream jsonrpc2.Stream, client Client) (*jsonrpc2.Conn, Server) { conn := jsonrpc2.NewConn(stream) + conn.Capacity = defaultMessageBufferSize + conn.RejectIfOverloaded = true conn.Handler = clientHandler(client) conn.Canceler = jsonrpc2.Canceler(canceller) return conn, &serverDispatcher{Conn: conn} @@ -24,6 +28,8 @@ func NewClient(stream jsonrpc2.Stream, client Client) (*jsonrpc2.Conn, Server) { func NewServer(stream jsonrpc2.Stream, server Server) (*jsonrpc2.Conn, Client) { conn := jsonrpc2.NewConn(stream) + conn.Capacity = defaultMessageBufferSize + conn.RejectIfOverloaded = true conn.Handler = serverHandler(server) conn.Canceler = jsonrpc2.Canceler(canceller) return conn, &clientDispatcher{Conn: conn} diff --git a/internal/lsp/server.go b/internal/lsp/server.go index 384e4ca52e2..c3e5090587f 100644 --- a/internal/lsp/server.go +++ b/internal/lsp/server.go @@ -28,17 +28,14 @@ import ( // NewClientServer func NewClientServer(client protocol.Client) *Server { return &Server{ - client: client, - configured: make(chan struct{}), + client: client, } } // NewServer starts an LSP server on the supplied stream, and waits until the // stream is closed. func NewServer(stream jsonrpc2.Stream) *Server { - s := &Server{ - configured: make(chan struct{}), - } + s := &Server{} s.Conn, s.client = protocol.NewServer(stream, s) return s } @@ -81,8 +78,6 @@ type Server struct { textDocumentSyncKind protocol.TextDocumentSyncKind view *cache.View - - configured chan struct{} } func (s *Server) Run(ctx context.Context) error { @@ -160,30 +155,24 @@ func (s *Server) Initialize(ctx context.Context, params *protocol.InitializePara } func (s *Server) Initialized(ctx context.Context, params *protocol.InitializedParams) error { - go func() { - // we hae to do this in a go routine to unblock the jsonrpc processor - // but we also have to block all calls to packages.Load until this is done - // TODO: we need to rewrite all the concurrency handling hin the server - defer func() { close(s.configured) }() - s.client.RegisterCapability(ctx, &protocol.RegistrationParams{ - Registrations: []protocol.Registration{{ - ID: "workspace/didChangeConfiguration", - Method: "workspace/didChangeConfiguration", - }}, - }) - config, err := s.client.Configuration(ctx, &protocol.ConfigurationParams{ - Items: []protocol.ConfigurationItem{{ - ScopeURI: protocol.NewURI(s.view.Folder), - Section: "gopls", - }}, - }) - if err != nil { - s.Error(err) - } - if err := s.processConfig(config[0]); err != nil { - s.Error(err) - } - }() + s.client.RegisterCapability(ctx, &protocol.RegistrationParams{ + Registrations: []protocol.Registration{{ + ID: "workspace/didChangeConfiguration", + Method: "workspace/didChangeConfiguration", + }}, + }) + config, err := s.client.Configuration(ctx, &protocol.ConfigurationParams{ + Items: []protocol.ConfigurationItem{{ + ScopeURI: protocol.NewURI(s.view.Folder), + Section: "gopls", + }}, + }) + if err != nil { + return err + } + if err := s.processConfig(config[0]); err != nil { + return err + } return nil }