1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:04:42 -07:00

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 <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Ian Cottrell 2020-04-02 12:43:08 -04:00
parent 17cc17e0bb
commit ccaaa5c26f
6 changed files with 28 additions and 15 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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
}

View File

@ -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))))

View File

@ -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)

View File

@ -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 {