Skip to content

Commit

Permalink
Fix cluster slots parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Feb 6, 2016
1 parent 255bb28 commit 2b2a680
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,12 +725,14 @@ func (cmd *ScanCmd) readReply(cn *conn) error {

//------------------------------------------------------------------------------

// TODO: rename to ClusterSlot
type ClusterSlotInfo struct {
Start int
End int
Addrs []string
}

// TODO: rename to ClusterSlotsCmd
type ClusterSlotCmd struct {
baseCmd

Expand Down
21 changes: 16 additions & 5 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func appendArg(b []byte, val interface{}) ([]byte, error) {
}

func appendArgs(b []byte, args []interface{}) ([]byte, error) {
b = append(b, '*')
b = append(b, arrayReply)
b = strconv.AppendUint(b, uint64(len(args)), 10)
b = append(b, '\r', '\n')
for _, arg := range args {
Expand Down Expand Up @@ -238,7 +238,9 @@ func readLine(cn *conn) ([]byte, error) {
}

func isNilReply(b []byte) bool {
return len(b) == 3 && (b[0] == '$' || b[0] == '*') && b[1] == '-' && b[2] == '1'
return len(b) == 3 &&
(b[0] == stringReply || b[0] == arrayReply) &&
b[1] == '-' && b[2] == '1'
}

func readN(cn *conn, n int) ([]byte, error) {
Expand Down Expand Up @@ -337,7 +339,7 @@ func readFloatReply(cn *conn) (float64, error) {
}

func parseArrayHeader(cn *conn, line []byte) (int64, error) {
if len(line) == 3 && line[1] == '-' && line[2] == '1' {
if isNilReply(line) {
return 0, Nil
}

Expand Down Expand Up @@ -604,8 +606,9 @@ func clusterSlotInfoSliceParser(cn *conn, n int64) (interface{}, error) {
if err != nil {
return nil, err
}
if n != 2 {
return nil, fmt.Errorf("got %d elements in cluster info address, expected 2", n)
if n != 2 && n != 3 {
err := fmt.Errorf("got %d elements in cluster info address, expected 2 or 3", n)
return nil, err
}

ip, err := readStringReply(cn)
Expand All @@ -618,6 +621,14 @@ func clusterSlotInfoSliceParser(cn *conn, n int64) (interface{}, error) {
return nil, err
}

if n == 3 {
// TODO: expose id in ClusterSlotInfo
_, err := readStringReply(cn)
if err != nil {
return nil, err
}
}

info.Addrs[i] = net.JoinHostPort(ip, strconv.FormatInt(port, 10))
}

Expand Down

0 comments on commit 2b2a680

Please sign in to comment.