From 33427f1b036407eb3cab42b030f9c545d821e013 Mon Sep 17 00:00:00 2001 From: Ian Cottrell Date: Wed, 29 Apr 2020 09:17:00 -0400 Subject: [PATCH] internal/jsonrpc2: rename NewStream to NewRawStream NewStream implies the default stream type, which it is not. NewHeaderStream is actually the default choice. Change-Id: I1744d7e902d27c13393f3b367fe2d29e5d7dc283 Reviewed-on: https://go-review.googlesource.com/c/tools/+/231618 Run-TryBot: Ian Cottrell TryBot-Result: Gobot Gobot Reviewed-by: Jonathan Amsterdam --- internal/jsonrpc2/jsonrpc2_test.go | 2 +- internal/jsonrpc2/servertest/servertest.go | 4 ++-- internal/jsonrpc2/stream.go | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/jsonrpc2/jsonrpc2_test.go b/internal/jsonrpc2/jsonrpc2_test.go index d9cc307b5d..072f1d5fa2 100644 --- a/internal/jsonrpc2/jsonrpc2_test.go +++ b/internal/jsonrpc2/jsonrpc2_test.go @@ -111,7 +111,7 @@ func run(ctx context.Context, t *testing.T, withHeaders bool, r io.ReadCloser, w if withHeaders { stream = jsonrpc2.NewHeaderStream(r, w) } else { - stream = jsonrpc2.NewStream(r, w) + stream = jsonrpc2.NewRawStream(r, w) } conn := jsonrpc2.NewConn(stream) wg.Add(1) diff --git a/internal/jsonrpc2/servertest/servertest.go b/internal/jsonrpc2/servertest/servertest.go index 13815e0cbe..ee27c8cc96 100644 --- a/internal/jsonrpc2/servertest/servertest.go +++ b/internal/jsonrpc2/servertest/servertest.go @@ -86,10 +86,10 @@ func (s *PipeServer) Connect(ctx context.Context) *jsonrpc2.Conn { cReader.Close() cWriter.Close() }) - serverStream := jsonrpc2.NewStream(sReader, cWriter) + serverStream := jsonrpc2.NewRawStream(sReader, cWriter) go s.server.ServeStream(ctx, serverStream) - clientStream := jsonrpc2.NewStream(cReader, sWriter) + clientStream := jsonrpc2.NewRawStream(cReader, sWriter) return jsonrpc2.NewConn(clientStream) } diff --git a/internal/jsonrpc2/stream.go b/internal/jsonrpc2/stream.go index c7d73f7277..c12eb003bd 100644 --- a/internal/jsonrpc2/stream.go +++ b/internal/jsonrpc2/stream.go @@ -28,23 +28,23 @@ type Stream interface { Write(context.Context, Message) (int64, error) } -// NewStream returns a Stream built on top of an io.Reader and io.Writer +// NewRawStream returns a Stream built on top of an io.Reader and io.Writer. // The messages are sent with no wrapping, and rely on json decode consistency // to determine message boundaries. -func NewStream(in io.Reader, out io.Writer) Stream { - return &plainStream{ +func NewRawStream(in io.Reader, out io.Writer) Stream { + return &rawStream{ in: json.NewDecoder(in), out: out, } } -type plainStream struct { +type rawStream struct { in *json.Decoder outMu sync.Mutex out io.Writer } -func (s *plainStream) Read(ctx context.Context) (Message, int64, error) { +func (s *rawStream) Read(ctx context.Context) (Message, int64, error) { select { case <-ctx.Done(): return nil, 0, ctx.Err() @@ -58,7 +58,7 @@ func (s *plainStream) Read(ctx context.Context) (Message, int64, error) { return msg, int64(len(raw)), err } -func (s *plainStream) Write(ctx context.Context, msg Message) (int64, error) { +func (s *rawStream) Write(ctx context.Context, msg Message) (int64, error) { select { case <-ctx.Done(): return 0, ctx.Err() @@ -74,7 +74,7 @@ func (s *plainStream) Write(ctx context.Context, msg Message) (int64, error) { return int64(n), err } -// NewHeaderStream returns a Stream built on top of an io.Reader and io.Writer +// NewHeaderStream returns a Stream built on top of an io.Reader and io.Writer. // The messages are sent with HTTP content length and MIME type headers. // This is the format used by LSP and others. func NewHeaderStream(in io.Reader, out io.Writer) Stream {