forked from mirror/redis
Merge pull request #226 from go-redis/fix/improve-nil-reply-parsing
Improve nil reply parsing.
This commit is contained in:
commit
ee7c50149d
|
@ -1298,12 +1298,15 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should BRPopLPush", func() {
|
||||
rPush := client.RPush("list1", "a", "b", "c")
|
||||
Expect(rPush.Err()).NotTo(HaveOccurred())
|
||||
_, err := client.BRPopLPush("list1", "list2", time.Second).Result()
|
||||
Expect(err).To(Equal(redis.Nil))
|
||||
|
||||
bRPopLPush := client.BRPopLPush("list1", "list2", 0)
|
||||
Expect(bRPopLPush.Err()).NotTo(HaveOccurred())
|
||||
Expect(bRPopLPush.Val()).To(Equal("c"))
|
||||
err = client.RPush("list1", "a", "b", "c").Err()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
v, err := client.BRPopLPush("list1", "list2", 0).Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(v).To(Equal("c"))
|
||||
})
|
||||
|
||||
It("should LIndex", func() {
|
||||
|
|
7
multi.go
7
multi.go
|
@ -174,6 +174,9 @@ func (c *Multi) execCmds(cn *conn, cmds []Cmder) error {
|
|||
// Parse number of replies.
|
||||
line, err := readLine(cn)
|
||||
if err != nil {
|
||||
if err == Nil {
|
||||
err = TxFailedErr
|
||||
}
|
||||
setCmdsErr(cmds[1:len(cmds)-1], err)
|
||||
return err
|
||||
}
|
||||
|
@ -182,10 +185,6 @@ func (c *Multi) execCmds(cn *conn, cmds []Cmder) error {
|
|||
setCmdsErr(cmds[1:len(cmds)-1], err)
|
||||
return err
|
||||
}
|
||||
if len(line) == 3 && line[1] == '-' && line[2] == '1' {
|
||||
setCmdsErr(cmds[1:len(cmds)-1], TxFailedErr)
|
||||
return TxFailedErr
|
||||
}
|
||||
|
||||
var firstCmdErr error
|
||||
|
||||
|
|
20
parser.go
20
parser.go
|
@ -231,9 +231,16 @@ func readLine(cn *conn) ([]byte, error) {
|
|||
if isPrefix {
|
||||
return line, errReaderTooSmall
|
||||
}
|
||||
if isNilReply(line) {
|
||||
return nil, 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) {
|
||||
var b []byte
|
||||
if cap(cn.buf) < n {
|
||||
|
@ -251,17 +258,6 @@ func parseErrorReply(cn *conn, line []byte) error {
|
|||
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) {
|
||||
return line[1:], nil
|
||||
}
|
||||
|
@ -282,8 +278,6 @@ func readIntReply(cn *conn) (int64, error) {
|
|||
switch line[0] {
|
||||
case errorReply:
|
||||
return 0, parseErrorReply(cn, line)
|
||||
case stringReply:
|
||||
return 0, parseNilReply(cn, line)
|
||||
case intReply:
|
||||
return parseIntReply(cn, line)
|
||||
default:
|
||||
|
|
Loading…
Reference in New Issue