From a0dd5b1b134d2eb4f2f924662dae058eeedbbb14 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Sat, 20 Aug 2016 10:42:34 -0700 Subject: [PATCH] added SetReadBuffer --- redcon.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/redcon.go b/redcon.go index 6f9b1a8..d6821e4 100644 --- a/redcon.go +++ b/redcon.go @@ -1,3 +1,4 @@ +// Package redcon provides a custom redis server implementation. package redcon import ( @@ -8,6 +9,7 @@ import ( "sync" ) +// Conn represents a client connection type Conn interface { RemoteAddr() string Close() error @@ -17,6 +19,7 @@ type Conn interface { WriteInt(num int) WriteArray(count int) WriteNull() + SetReadBuffer(bytes int) } var ( @@ -25,6 +28,8 @@ var ( errInvalidMultiBulkLength = &errProtocol{"invalid multibulk length"} ) +const defaultBufLen = 1024 * 64 + type errProtocol struct { msg string } @@ -33,6 +38,7 @@ func (err *errProtocol) Error() string { return "Protocol error: " + err.msg } +// ListenAndServe creates a new server and binds to addr. func ListenAndServe( addr string, handler func(conn Conn, cmds [][]string), accept func(conn Conn) bool, closed func(conn Conn, err error), @@ -139,21 +145,23 @@ func (c *conn) WriteNull() { func (c *conn) RemoteAddr() string { return c.addr } +func (c *conn) SetReadBuffer(bytes int) { + c.rd.buflen = bytes +} // Reader represents a RESP command reader. type reader struct { - r io.Reader // base reader - b []byte // unprocessed bytes - a []byte // static read buffer + r io.Reader // base reader + b []byte // unprocessed bytes + a []byte // static read buffer + buflen int // buffer len } -const buflen = 1024 * 8 - // NewReader returns a RESP command reader. func newReader(r io.Reader) *reader { return &reader{ - r: r, - a: make([]byte, buflen), + r: r, + buflen: defaultBufLen, } } @@ -314,7 +322,7 @@ func (r *reader) ReadCommands() ([][]string, error) { } } if len(r.a) == 0 { - r.a = make([]byte, buflen) + r.a = make([]byte, r.buflen) } n, err := r.r.Read(r.a) if err != nil { @@ -344,7 +352,7 @@ type writer struct { } func newWriter(w *net.TCPConn) *writer { - return &writer{w: w, b: make([]byte, 0, 256)} + return &writer{w: w, b: make([]byte, 0, 512)} } func (w *writer) WriteNull() error {