Merge pull request #506 from go-redis/fix/separate-buffer

Separate read and write buffers for PubSub.
This commit is contained in:
Vladimir Mihailenco 2017-02-19 10:48:52 +03:00 committed by GitHub
commit 135cb12c76
5 changed files with 12 additions and 13 deletions

View File

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

View File

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

View File

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

View File

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

View File

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