From 21d8cf378407f4405ea69a864522372a814ef911 Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Wed, 5 May 2021 10:41:28 -0400 Subject: [PATCH] channel: Remove the Varint framing. https://github.com/creachadair/jrpc2/issues/46#issuecomment-831963902 --- channel/bench_test.go | 1 - channel/channel_test.go | 1 - channel/chanutil/chanutil.go | 8 ++--- channel/varint.go | 64 ------------------------------------ server/loop_test.go | 2 +- 5 files changed, 4 insertions(+), 72 deletions(-) delete mode 100644 channel/varint.go diff --git a/channel/bench_test.go b/channel/bench_test.go index bbdb47f..5233d9b 100644 --- a/channel/bench_test.go +++ b/channel/bench_test.go @@ -43,7 +43,6 @@ func BenchmarkFramingCost(b *testing.B) { {"LSP", channel.LSP}, {"NUL", channel.Split('\x00')}, {"RawJSON", channel.RawJSON}, - {"Varint", channel.Varint}, } msg, err := json.Marshal(benchMessage) diff --git a/channel/channel_test.go b/channel/channel_test.go index e6d96f0..b04788d 100644 --- a/channel/channel_test.go +++ b/channel/channel_test.go @@ -84,7 +84,6 @@ var tests = []struct { {"RS", Split('\x1e')}, {"RawJSON", RawJSON}, {"StrictHeader", StrictHeader("text/plain")}, - {"Varint", Varint}, } // N.B. the messages in this list must be valid JSON, since the RawJSON framing diff --git a/channel/chanutil/chanutil.go b/channel/chanutil/chanutil.go index 499400c..0cd075b 100644 --- a/channel/chanutil/chanutil.go +++ b/channel/chanutil/chanutil.go @@ -16,7 +16,6 @@ import ( // line -- corresponds to channel.Line // lsp -- corresponds to channel.LSP // raw -- corresponds to channel.RawJSON -// varint -- corresponds to channel.Varint // func Framing(name string) channel.Framing { if t := strings.TrimPrefix(name, "header:"); t != name { @@ -29,8 +28,7 @@ func Framing(name string) channel.Framing { } var framings = map[string]channel.Framing{ - "line": channel.Line, - "lsp": channel.LSP, - "raw": channel.RawJSON, - "varint": channel.Varint, + "line": channel.Line, + "lsp": channel.LSP, + "raw": channel.RawJSON, } diff --git a/channel/varint.go b/channel/varint.go deleted file mode 100644 index cb46b6d..0000000 --- a/channel/varint.go +++ /dev/null @@ -1,64 +0,0 @@ -package channel - -import ( - "bufio" - "bytes" - "encoding/binary" - "io" -) - -// Varint is a framing that transmits and receives messages on r and wc, with -// each message prefixed by its length encoded in a varint as defined by the -// encoding/binary package. -func Varint(r io.Reader, wc io.WriteCloser) Channel { - return &varint{ - wc: wc, - rd: bufio.NewReader(r), - buf: bytes.NewBuffer(nil), - } -} - -// A varint implements Channel for varint-prefixed messages. -type varint struct { - wc io.WriteCloser - rd *bufio.Reader - buf *bytes.Buffer -} - -// Send implements part of the Channel interface. It encodes len(msg) as a -// varint, concatenates it with the message body, and writes the framed message -// to the underlying writer. -func (v *varint) Send(msg []byte) error { - var ln [binary.MaxVarintLen64]byte - nb := binary.PutUvarint(ln[:], uint64(len(msg))) - - v.buf.Reset() - v.buf.Grow(nb + len(msg)) - v.buf.Write(ln[:nb]) - v.buf.Write(msg) - _, err := v.wc.Write(v.buf.Next(v.buf.Len())) - return err -} - -// Recv implements part of the Channel interface. It decodes a varint message -// length then reads that many bytes from the underlying reader. -func (v *varint) Recv() ([]byte, error) { - ln, err := v.decode() - if err != nil { - return nil, err - } - out := make([]byte, ln) - nr, err := io.ReadFull(v.rd, out) - return out[:nr], err -} - -// Close implements part of the Channel interface. -func (v *varint) Close() error { return v.wc.Close() } - -func (v *varint) decode() (int, error) { - ln, err := binary.ReadUvarint(v.rd) - if err != nil { - return 0, err - } - return int(ln), nil -} diff --git a/server/loop_test.go b/server/loop_test.go index a13a9c2..df61a16 100644 --- a/server/loop_test.go +++ b/server/loop_test.go @@ -13,7 +13,7 @@ import ( "github.com/creachadair/jrpc2/handler" ) -var newChan = channel.Varint +var newChan = channel.Line // A static test service that returns the same thing each time. var testService = NewStatic(handler.Map{