Improve nil reply parsing.

This commit is contained in:
Vladimir Mihailenco 2015-12-22 11:02:18 +02:00
parent ba44d4d158
commit 9782e280cf
3 changed files with 18 additions and 22 deletions

View File

@ -1298,12 +1298,15 @@ var _ = Describe("Commands", func() {
}) })
It("should BRPopLPush", func() { It("should BRPopLPush", func() {
rPush := client.RPush("list1", "a", "b", "c") _, err := client.BRPopLPush("list1", "list2", time.Second).Result()
Expect(rPush.Err()).NotTo(HaveOccurred()) Expect(err).To(Equal(redis.Nil))
bRPopLPush := client.BRPopLPush("list1", "list2", 0) err = client.RPush("list1", "a", "b", "c").Err()
Expect(bRPopLPush.Err()).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(bRPopLPush.Val()).To(Equal("c"))
v, err := client.BRPopLPush("list1", "list2", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal("c"))
}) })
It("should LIndex", func() { It("should LIndex", func() {

View File

@ -174,6 +174,9 @@ func (c *Multi) execCmds(cn *conn, cmds []Cmder) error {
// Parse number of replies. // Parse number of replies.
line, err := readLine(cn) line, err := readLine(cn)
if err != nil { if err != nil {
if err == Nil {
err = TxFailedErr
}
setCmdsErr(cmds[1:len(cmds)-1], err) setCmdsErr(cmds[1:len(cmds)-1], err)
return err return err
} }
@ -182,10 +185,6 @@ func (c *Multi) execCmds(cn *conn, cmds []Cmder) error {
setCmdsErr(cmds[1:len(cmds)-1], err) setCmdsErr(cmds[1:len(cmds)-1], err)
return err return err
} }
if len(line) == 3 && line[1] == '-' && line[2] == '1' {
setCmdsErr(cmds[1:len(cmds)-1], TxFailedErr)
return TxFailedErr
}
var firstCmdErr error var firstCmdErr error

View File

@ -231,9 +231,16 @@ func readLine(cn *conn) ([]byte, error) {
if isPrefix { if isPrefix {
return line, errReaderTooSmall return line, errReaderTooSmall
} }
if isNilReply(line) {
return nil, Nil
}
return line, nil return line, nil
} }
func isNilReply(b []byte) bool {
return len(b) == 3 && (b[0] == '$' || b[0] == '*') && b[1] == '-' && b[2] == '1'
}
func readN(cn *conn, n int) ([]byte, error) { func readN(cn *conn, n int) ([]byte, error) {
var b []byte var b []byte
if cap(cn.buf) < n { if cap(cn.buf) < n {
@ -251,17 +258,6 @@ func parseErrorReply(cn *conn, line []byte) error {
return errorf(string(line[1:])) return errorf(string(line[1:]))
} }
func isNilReply(b []byte) bool {
return len(b) == 3 && b[1] == '-' && b[2] == '1'
}
func parseNilReply(cn *conn, line []byte) error {
if isNilReply(line) {
return Nil
}
return fmt.Errorf("redis: can't parse nil reply: %.100", line)
}
func parseStatusReply(cn *conn, line []byte) ([]byte, error) { func parseStatusReply(cn *conn, line []byte) ([]byte, error) {
return line[1:], nil return line[1:], nil
} }
@ -282,8 +278,6 @@ func readIntReply(cn *conn) (int64, error) {
switch line[0] { switch line[0] {
case errorReply: case errorReply:
return 0, parseErrorReply(cn, line) return 0, parseErrorReply(cn, line)
case stringReply:
return 0, parseNilReply(cn, line)
case intReply: case intReply:
return parseIntReply(cn, line) return parseIntReply(cn, line)
default: default: