mirror of
https://github.com/golang/go
synced 2024-11-18 16:44:43 -07:00
75aaabac35
also change the return type to be and end function and not an incomplete span Change-Id: Icd99d93ac98a0f8088f33e905cf1ee3fe410c024 Reviewed-on: https://go-review.googlesource.com/c/tools/+/185349 Run-TryBot: Ian Cottrell <iancottrell@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package protocol
|
|
|
|
import (
|
|
"context"
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
"golang.org/x/tools/internal/lsp/telemetry/trace"
|
|
"golang.org/x/tools/internal/lsp/xlog"
|
|
"golang.org/x/tools/internal/xcontext"
|
|
)
|
|
|
|
const defaultMessageBufferSize = 20
|
|
const defaultRejectIfOverloaded = false
|
|
|
|
func canceller(ctx context.Context, conn *jsonrpc2.Conn, id jsonrpc2.ID) {
|
|
ctx = xcontext.Detach(ctx)
|
|
ctx, done := trace.StartSpan(ctx, "protocol.canceller")
|
|
defer done()
|
|
conn.Notify(ctx, "$/cancelRequest", &CancelParams{ID: id})
|
|
}
|
|
|
|
func NewClient(stream jsonrpc2.Stream, client Client) (*jsonrpc2.Conn, Server, xlog.Logger) {
|
|
log := xlog.New(NewLogger(client))
|
|
conn := jsonrpc2.NewConn(stream)
|
|
conn.Capacity = defaultMessageBufferSize
|
|
conn.RejectIfOverloaded = defaultRejectIfOverloaded
|
|
conn.Handler = clientHandler(log, client)
|
|
conn.Canceler = jsonrpc2.Canceler(canceller)
|
|
return conn, &serverDispatcher{Conn: conn}, log
|
|
}
|
|
|
|
func NewServer(stream jsonrpc2.Stream, server Server) (*jsonrpc2.Conn, Client, xlog.Logger) {
|
|
conn := jsonrpc2.NewConn(stream)
|
|
client := &clientDispatcher{Conn: conn}
|
|
log := xlog.New(NewLogger(client))
|
|
conn.Capacity = defaultMessageBufferSize
|
|
conn.RejectIfOverloaded = defaultRejectIfOverloaded
|
|
conn.Handler = serverHandler(log, server)
|
|
conn.Canceler = jsonrpc2.Canceler(canceller)
|
|
return conn, client, log
|
|
}
|
|
|
|
func sendParseError(ctx context.Context, log xlog.Logger, req *jsonrpc2.Request, err error) {
|
|
if _, ok := err.(*jsonrpc2.Error); !ok {
|
|
err = jsonrpc2.NewErrorf(jsonrpc2.CodeParseError, "%v", err)
|
|
}
|
|
if err := req.Reply(ctx, nil, err); err != nil {
|
|
log.Errorf(ctx, "%v", err)
|
|
}
|
|
}
|