Add ParseReq method and tweak benchmarks.

This commit is contained in:
Vladimir Mihailenco 2012-08-24 15:16:12 +03:00
parent f56748aab9
commit b6ae953e1c
5 changed files with 119 additions and 21 deletions

View File

@ -55,6 +55,66 @@ func readLine(rd reader) ([]byte, error) {
return line, nil
}
func readN(rd reader, n int) ([]byte, error) {
buf, err := rd.ReadN(n)
if err == bufio.ErrBufferFull {
newBuf := make([]byte, n)
r := copy(newBuf, buf)
buf = newBuf
for r < n {
n, err := rd.Read(buf[r:])
if err != nil {
return nil, err
}
r += n
}
} else if err != nil {
return nil, err
}
return buf, nil
}
//------------------------------------------------------------------------------
func ParseReq(rd reader) ([]string, error) {
line, err := readLine(rd)
if err != nil {
return nil, err
}
if line[0] != '*' {
return []string{string(line)}, nil
}
numReplies, err := strconv.ParseInt(string(line[1:]), 10, 64)
if err != nil {
return nil, err
}
args := make([]string, 0, numReplies)
for i := int64(0); i < numReplies; i++ {
line, err = readLine(rd)
if err != nil {
return nil, err
}
if line[0] != '$' {
return nil, fmt.Errorf("Expected '$', but got %q", line)
}
argLen, err := strconv.ParseInt(string(line[1:]), 10, 32)
if err != nil {
return nil, err
}
arg, err := readN(rd, int(argLen)+2)
if err != nil {
return nil, err
}
args = append(args, string(arg[:argLen]))
}
return args, nil
}
//------------------------------------------------------------------------------
const (
@ -99,24 +159,10 @@ func _parseReply(rd reader, multiBulkType int) (interface{}, error) {
}
replyLen := int(replyLenInt32) + 2
line, err = rd.ReadN(replyLen)
if err == bufio.ErrBufferFull {
buf := make([]byte, replyLen)
r := copy(buf, line)
for r < replyLen {
n, err := rd.Read(buf[r:])
if err != nil {
return "", err
}
r += n
}
line = buf
} else if err != nil {
line, err = readN(rd, replyLen)
if err != nil {
return "", err
}
return string(line[:len(line)-2]), nil
case '*':
if len(line) == 3 && line[1] == '-' && line[2] == '1' {

23
parser_test.go Normal file
View File

@ -0,0 +1,23 @@
package redis_test
import (
"bytes"
"github.com/vmihailenco/bufio"
. "launchpad.net/gocheck"
"github.com/vmihailenco/redis"
)
type ParserTest struct{}
var _ = Suite(&ParserTest{})
func (t *ParserTest) TestParseReq(c *C) {
buf := bytes.NewBufferString("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nhello\r\n")
rd := bufio.NewReaderSize(buf, 1024)
args, err := redis.ParseReq(rd)
c.Check(err, IsNil)
c.Check(args, DeepEquals, []string{"SET", "key", "hello"})
}

View File

@ -45,7 +45,7 @@ func (c *PubSubClient) consumeMessages(conn *Conn) {
if err != nil {
msg.Err = err
c.ch <- msg
break
return
}
reply, ok := replyIface.([]interface{})
if !ok {

View File

@ -79,6 +79,9 @@ func (c *BaseClient) conn() (*Conn, error) {
}
err = c.InitConn(client)
if err != nil {
if err := c.ConnPool.Remove(conn); err != nil {
panic(err)
}
return nil, err
}
}
@ -102,7 +105,9 @@ func (c *BaseClient) Run(req Req) {
err = c.WriteReq(conn, req)
if err != nil {
c.ConnPool.Remove(conn)
if err := c.ConnPool.Remove(conn); err != nil {
panic(err)
}
req.SetErr(err)
return
}
@ -110,15 +115,21 @@ func (c *BaseClient) Run(req Req) {
val, err := req.ParseReply(conn.Rd)
if err != nil {
if err == Nil {
c.ConnPool.Add(conn)
if err := c.ConnPool.Add(conn); err != nil {
panic(err)
}
} else {
c.ConnPool.Remove(conn)
if err := c.ConnPool.Remove(conn); err != nil {
panic(err)
}
}
req.SetErr(err)
return
}
c.ConnPool.Add(conn)
if err := c.ConnPool.Add(conn); err != nil {
panic(err)
}
req.SetVal(val)
}

View File

@ -60,10 +60,18 @@ func (t *RequestTest) BenchmarkStatusReq(c *C) {
t.benchmarkReq(c, "+OK\r\n", redis.NewStatusReq(), Equals, "OK")
}
func (t *RequestTest) BenchmarkIntReq(c *C) {
t.benchmarkReq(c, ":1\r\n", redis.NewIntReq(), Equals, int64(1))
}
func (t *RequestTest) BenchmarkStringReq(c *C) {
t.benchmarkReq(c, "$5\r\nhello\r\n", redis.NewStringReq(), Equals, "hello")
}
func (t *RequestTest) BenchmarkFloatReq(c *C) {
t.benchmarkReq(c, "$5\r\n1.111\r\n", redis.NewFloatReq(), Equals, 1.111)
}
func (t *RequestTest) BenchmarkStringSliceReq(c *C) {
t.benchmarkReq(
c,
@ -73,3 +81,13 @@ func (t *RequestTest) BenchmarkStringSliceReq(c *C) {
[]string{"hello", "hello"},
)
}
func (t *RequestTest) BenchmarkIfaceSliceReq(c *C) {
t.benchmarkReq(
c,
"*2\r\n$5\r\nhello\r\n$5\r\nhello\r\n",
redis.NewIfaceSliceReq(),
DeepEquals,
[]interface{}{"hello", "hello"},
)
}