From 6b6f5ca13362bb9b77d2cfd7bec085ce531e7c57 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sun, 19 Feb 2017 09:42:45 +0200 Subject: [PATCH] Separate read and write buffers for PubSub. --- internal/pool/conn.go | 5 ++--- internal/proto/reader.go | 4 ++-- internal/proto/reader_test.go | 8 ++++---- internal/proto/write_buffer.go | 4 ++-- internal/proto/write_buffer_test.go | 4 ++-- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/internal/pool/conn.go b/internal/pool/conn.go index 8a58f9aa..2a135ae9 100644 --- a/internal/pool/conn.go +++ b/internal/pool/conn.go @@ -21,12 +21,11 @@ type Conn struct { } func NewConn(netConn net.Conn) *Conn { - buf := make([]byte, 4096) cn := &Conn{ netConn: netConn, - Wb: proto.NewWriteBuffer(buf), + Wb: proto.NewWriteBuffer(), } - cn.Rd = proto.NewReader(cn.netConn, buf) + cn.Rd = proto.NewReader(cn.netConn) cn.SetUsedAt(time.Now()) return cn } diff --git a/internal/proto/reader.go b/internal/proto/reader.go index 78f32317..e5dc95ed 100644 --- a/internal/proto/reader.go +++ b/internal/proto/reader.go @@ -26,10 +26,10 @@ type Reader struct { buf []byte } -func NewReader(rd io.Reader, buf []byte) *Reader { +func NewReader(rd io.Reader) *Reader { return &Reader{ src: bufio.NewReader(rd), - buf: buf, + buf: make([]byte, 4096), } } diff --git a/internal/proto/reader_test.go b/internal/proto/reader_test.go index 4835a625..63046464 100644 --- a/internal/proto/reader_test.go +++ b/internal/proto/reader_test.go @@ -14,18 +14,18 @@ import ( var _ = Describe("Reader", func() { It("should read n bytes", func() { - data, err := proto.NewReader(strings.NewReader("ABCDEFGHIJKLMNO"), nil).ReadN(10) + data, err := proto.NewReader(strings.NewReader("ABCDEFGHIJKLMNO")).ReadN(10) Expect(err).NotTo(HaveOccurred()) Expect(len(data)).To(Equal(10)) Expect(string(data)).To(Equal("ABCDEFGHIJ")) - data, err = proto.NewReader(strings.NewReader(strings.Repeat("x", 8192)), nil).ReadN(6000) + data, err = proto.NewReader(strings.NewReader(strings.Repeat("x", 8192))).ReadN(6000) Expect(err).NotTo(HaveOccurred()) Expect(len(data)).To(Equal(6000)) }) It("should read lines", func() { - p := proto.NewReader(strings.NewReader("$5\r\nhello\r\n"), nil) + p := proto.NewReader(strings.NewReader("$5\r\nhello\r\n")) data, err := p.ReadLine() Expect(err).NotTo(HaveOccurred()) @@ -63,7 +63,7 @@ func benchmarkParseReply(b *testing.B, reply string, m proto.MultiBulkParse, wan for i := 0; i < b.N; i++ { buf.WriteString(reply) } - p := proto.NewReader(buf, nil) + p := proto.NewReader(buf) b.ResetTimer() for i := 0; i < b.N; i++ { diff --git a/internal/proto/write_buffer.go b/internal/proto/write_buffer.go index 93fb3677..019e64c8 100644 --- a/internal/proto/write_buffer.go +++ b/internal/proto/write_buffer.go @@ -12,9 +12,9 @@ type WriteBuffer struct { b []byte } -func NewWriteBuffer(b []byte) *WriteBuffer { +func NewWriteBuffer() *WriteBuffer { return &WriteBuffer{ - b: b, + b: make([]byte, 0, 4096), } } diff --git a/internal/proto/write_buffer_test.go b/internal/proto/write_buffer_test.go index fd70cd7f..c4438e4a 100644 --- a/internal/proto/write_buffer_test.go +++ b/internal/proto/write_buffer_test.go @@ -14,7 +14,7 @@ var _ = Describe("WriteBuffer", func() { var buf *proto.WriteBuffer BeforeEach(func() { - buf = proto.NewWriteBuffer(nil) + buf = proto.NewWriteBuffer() }) It("should reset", func() { @@ -53,7 +53,7 @@ var _ = Describe("WriteBuffer", func() { }) func BenchmarkWriteBuffer_Append(b *testing.B) { - buf := proto.NewWriteBuffer(nil) + buf := proto.NewWriteBuffer() args := []interface{}{"hello", "world", "foo", "bar"} for i := 0; i < b.N; i++ {