From ccaaa5c26f8a627c16b713fee928468693a8555d Mon Sep 17 00:00:00 2001 From: Ian Cottrell Date: Thu, 2 Apr 2020 12:43:08 -0400 Subject: [PATCH] internal/jsonrpc2: dont add any handlers by default This pushes the handler construction out to the user, allowing flexability of use, and is the final stage of the switch to the new handler API. Change-Id: Id2e61813a817df0d6e4d20dd47ce8c92b0ae87db Reviewed-on: https://go-review.googlesource.com/c/tools/+/227024 Run-TryBot: Ian Cottrell TryBot-Result: Gobot Gobot Reviewed-by: Robert Findley --- internal/jsonrpc2/jsonrpc2.go | 2 -- internal/lsp/cmd/cmd.go | 7 ++++--- internal/lsp/fake/editor.go | 5 ++++- internal/lsp/lsprpc/lsprpc.go | 18 ++++++++++-------- internal/lsp/lsprpc/lsprpc_test.go | 4 +++- internal/lsp/protocol/protocol.go | 7 +++++++ 6 files changed, 28 insertions(+), 15 deletions(-) diff --git a/internal/jsonrpc2/jsonrpc2.go b/internal/jsonrpc2/jsonrpc2.go index f93ab68bc1..08234d2a91 100644 --- a/internal/jsonrpc2/jsonrpc2.go +++ b/internal/jsonrpc2/jsonrpc2.go @@ -276,8 +276,6 @@ type combined struct { // It must be called exactly once for each Conn. // It returns only when the reader is closed or there is an error in the stream. func (c *Conn) Run(runCtx context.Context, handler Handler) error { - handler = MustReply(handler) - handler = AsyncHandler(handler) for { // get the data for a message data, n, err := c.stream.Read(runCtx) diff --git a/internal/lsp/cmd/cmd.go b/internal/lsp/cmd/cmd.go index cf5342d245..e05d2b5035 100644 --- a/internal/lsp/cmd/cmd.go +++ b/internal/lsp/cmd/cmd.go @@ -234,9 +234,10 @@ func (app *Application) connectRemote(ctx context.Context, remote string) (*conn cc := jsonrpc2.NewConn(stream) connection.Server = protocol.ServerDispatcher(cc) ctx = protocol.WithClient(ctx, connection.Client) - go cc.Run(ctx, protocol.CancelHandler( - protocol.ClientHandler(connection.Client, - jsonrpc2.MethodNotFound))) + go cc.Run(ctx, + protocol.Handlers( + protocol.ClientHandler(connection.Client, + jsonrpc2.MethodNotFound))) return connection, connection.initialize(ctx, app.options) } diff --git a/internal/lsp/fake/editor.go b/internal/lsp/fake/editor.go index d4fb182480..1cd213ae1b 100644 --- a/internal/lsp/fake/editor.go +++ b/internal/lsp/fake/editor.go @@ -57,7 +57,10 @@ func NewConnectedEditor(ctx context.Context, ws *Workspace, conn *jsonrpc2.Conn) e := NewEditor(ws) e.server = protocol.ServerDispatcher(conn) e.client = &Client{Editor: e} - go conn.Run(ctx, protocol.ClientHandler(e.client, jsonrpc2.MethodNotFound)) + go conn.Run(ctx, + protocol.Handlers( + protocol.ClientHandler(e.client, + jsonrpc2.MethodNotFound))) if err := e.initialize(ctx); err != nil { return nil, err } diff --git a/internal/lsp/lsprpc/lsprpc.go b/internal/lsp/lsprpc/lsprpc.go index c516ab73d3..5c971f2ef6 100644 --- a/internal/lsp/lsprpc/lsprpc.go +++ b/internal/lsp/lsprpc/lsprpc.go @@ -140,10 +140,11 @@ func (s *StreamServer) ServeStream(ctx context.Context, stream jsonrpc2.Stream) executable = "" } ctx = protocol.WithClient(ctx, client) - return conn.Run(ctx, protocol.CancelHandler( - handshaker(dc, executable, - protocol.ServerHandler(server, - jsonrpc2.MethodNotFound)))) + return conn.Run(ctx, + protocol.Handlers( + handshaker(dc, executable, + protocol.ServerHandler(server, + jsonrpc2.MethodNotFound)))) } // A Forwarder is a jsonrpc2.StreamServer that handles an LSP stream by @@ -257,9 +258,10 @@ func (f *Forwarder) ServeStream(ctx context.Context, stream jsonrpc2.Stream) err // Forward between connections. g, ctx := errgroup.WithContext(ctx) g.Go(func() error { - return serverConn.Run(ctx, protocol.CancelHandler( - protocol.ClientHandler(client, - jsonrpc2.MethodNotFound))) + return serverConn.Run(ctx, + protocol.Handlers( + protocol.ClientHandler(client, + jsonrpc2.MethodNotFound))) }) // Don't run the clientConn yet, so that we can complete the handshake before // processing any client messages. @@ -298,7 +300,7 @@ func (f *Forwarder) ServeStream(ctx context.Context, stream jsonrpc2.Stream) err } g.Go(func() error { return clientConn.Run(ctx, - protocol.CancelHandler( + protocol.Handlers( forwarderHandler( protocol.ServerHandler(server, jsonrpc2.MethodNotFound)))) diff --git a/internal/lsp/lsprpc/lsprpc_test.go b/internal/lsp/lsprpc/lsprpc_test.go index 1de8b92829..37a3e9a8cd 100644 --- a/internal/lsp/lsprpc/lsprpc_test.go +++ b/internal/lsp/lsprpc/lsprpc_test.go @@ -129,7 +129,9 @@ func TestRequestCancellation(t *testing.T) { t.Run(test.serverType, func(t *testing.T) { cc := test.ts.Connect(baseCtx) sd := protocol.ServerDispatcher(cc) - go cc.Run(baseCtx, protocol.CancelHandler(jsonrpc2.MethodNotFound)) + go cc.Run(baseCtx, + protocol.Handlers( + jsonrpc2.MethodNotFound)) ctx := context.Background() ctx1, cancel1 := context.WithCancel(ctx) diff --git a/internal/lsp/protocol/protocol.go b/internal/lsp/protocol/protocol.go index 174ccedcfe..00610ade2e 100644 --- a/internal/lsp/protocol/protocol.go +++ b/internal/lsp/protocol/protocol.go @@ -33,6 +33,13 @@ func ServerDispatcher(conn *jsonrpc2.Conn) Server { return &serverDispatcher{Conn: conn} } +func Handlers(handler jsonrpc2.Handler) jsonrpc2.Handler { + return CancelHandler( + CancelHandler( + jsonrpc2.AsyncHandler( + jsonrpc2.MustReply(handler)))) +} + func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler { handler, canceller := jsonrpc2.CancelHandler(handler) return func(ctx context.Context, req *jsonrpc2.Request) error {