Skip to content

Commit

Permalink
Improve nil reply parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Dec 22, 2015
1 parent ba44d4d commit 9782e28
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
13 changes: 8 additions & 5 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 3 additions & 4 deletions multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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

Expand Down
20 changes: 7 additions & 13 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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:
Expand Down

0 comments on commit 9782e28

Please sign in to comment.