Skip to content

Commit

Permalink
use github.com/xiam/resp
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhe1991 committed Jun 18, 2015
1 parent 5cd2c3c commit 67b6168
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions pkg/proxy/router/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,16 @@ import (
"strconv"
"strings"
"time"

"github.com/wandoulabs/codis/pkg/utils"

"github.com/wandoulabs/codis/pkg/models"
"github.com/wandoulabs/codis/pkg/proxy/group"
"github.com/wandoulabs/codis/pkg/proxy/parser"
"github.com/wandoulabs/codis/pkg/proxy/router/topology"

log "github.com/ngaut/logging"

"github.com/juju/errors"
topo "github.com/ngaut/go-zookeeper/zk"
stats "github.com/ngaut/gostats"

respcoding "github.com/ngaut/resp"
"github.com/xiam/resp"
)

var blackList = []string{
Expand Down Expand Up @@ -76,12 +71,46 @@ func WriteMigrateKeyCmd(w io.Writer, addr string, timeoutMs int, key []byte) err
if len(hostPort) != 2 {
return errors.Errorf("invalid address " + addr)
}
respW := respcoding.NewRESPWriter(w)
respW := NewRESPWriter(w)
err := respW.WriteCommand("slotsmgrttagone", hostPort[0], hostPort[1],
strconv.Itoa(int(timeoutMs)), string(key))
return errors.Trace(err)
}

var (
arrayPrefixSlice = []byte{'*'}
bulkStringPrefixSlice = []byte{'$'}
lineEndingSlice = []byte{'\r', '\n'}
)

type RESPWriter struct {
*bufio.Writer
}

func NewRESPWriter(writer io.Writer) *RESPWriter {
return &RESPWriter{
Writer: bufio.NewWriter(writer),
}
}

func (w *RESPWriter) WriteCommand(args ...string) (err error) {
// Write the array prefix and the number of arguments in the array.
_, err = w.Write(arrayPrefixSlice)
_, err = w.WriteString(strconv.Itoa(len(args)))
_, err = w.Write(lineEndingSlice)

// Write a bulk string for each argument.
for _, arg := range args {
w.Write(bulkStringPrefixSlice)
w.WriteString(strconv.Itoa(len(arg)))
w.Write(lineEndingSlice)
w.WriteString(arg)
_, err = w.Write(lineEndingSlice)
}

return w.Flush()
}

type DeadlineReadWriter interface {
io.ReadWriter
SetWriteDeadline(t time.Time) error
Expand All @@ -104,7 +133,7 @@ func handleSpecCommand(cmd string, keys [][]byte, timeout int) ([]byte, bool, bo
case "ECHO":
if len(keys) > 0 {
var err error
b, err = respcoding.Marshal(string(keys[0]))
b, err = resp.Marshal(string(keys[0]))
if err != nil {
return nil, true, false, errors.Trace(err)
}
Expand Down

0 comments on commit 67b6168

Please sign in to comment.