Skip to content

Commit

Permalink
Make proto/parser an internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
dim committed Jul 2, 2016
1 parent 5c3ab24 commit 7d856c5
Show file tree
Hide file tree
Showing 24 changed files with 841 additions and 712 deletions.
9 changes: 5 additions & 4 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"gopkg.in/redis.v4/internal"
"gopkg.in/redis.v4/internal/errors"
"gopkg.in/redis.v4/internal/hashtag"
"gopkg.in/redis.v4/internal/pool"
)
Expand Down Expand Up @@ -291,14 +292,14 @@ func (c *ClusterClient) Process(cmd Cmder) error {
}

// On network errors try random node.
if shouldRetry(err) {
if errors.IsRetryable(err) {
node, err = c.randomNode()
continue
}

var moved bool
var addr string
moved, ask, addr = isMovedError(err)
moved, ask, addr = errors.IsMoved(err)
if moved || ask {
master, _ := c.slotMasterNode(slot)
if moved && (master == nil || master.Addr != addr) {
Expand Down Expand Up @@ -549,11 +550,11 @@ func (c *ClusterClient) execClusterCmds(
if err == nil {
continue
}
if isNetworkError(err) {
if errors.IsNetwork(err) {
cmd.reset()
failedCmds[nil] = append(failedCmds[nil], cmds[i:]...)
break
} else if moved, ask, addr := isMovedError(err); moved {
} else if moved, ask, addr := errors.IsMoved(err); moved {
c.lazyReloadSlots()
cmd.reset()
node, err := c.nodeByAddr(addr)
Expand Down
1 change: 1 addition & 0 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ var _ = Describe("ClusterClient", func() {
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer GinkgoRecover()
defer wg.Done()

err := incr("key")
Expand Down
47 changes: 23 additions & 24 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"gopkg.in/redis.v4/internal/pool"
"gopkg.in/redis.v4/internal/proto"
)

var (
Expand Down Expand Up @@ -55,16 +56,14 @@ func resetCmds(cmds []Cmder) {
}

func writeCmd(cn *pool.Conn, cmds ...Cmder) error {
cn.Buf = cn.Buf[:0]
cn.Wb.Reset()
for _, cmd := range cmds {
var err error
cn.Buf, err = appendArgs(cn.Buf, cmd.args())
if err != nil {
if err := cn.Wb.Append(cmd.args()); err != nil {
return err
}
}

_, err := cn.Write(cn.Buf)
_, err := cn.Write(cn.Wb.Bytes())
return err
}

Expand Down Expand Up @@ -166,7 +165,7 @@ func (cmd *Cmd) String() string {
}

func (cmd *Cmd) readReply(cn *pool.Conn) error {
val, err := readReply(cn, sliceParser)
val, err := cn.Rd.ReadReply(sliceParser)
if err != nil {
cmd.err = err
return cmd.err
Expand Down Expand Up @@ -211,7 +210,7 @@ func (cmd *SliceCmd) String() string {
}

func (cmd *SliceCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, sliceParser)
v, err := cn.Rd.ReadArrayReply(sliceParser)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -251,7 +250,7 @@ func (cmd *StatusCmd) String() string {
}

func (cmd *StatusCmd) readReply(cn *pool.Conn) error {
cmd.val, cmd.err = readStringReply(cn)
cmd.val, cmd.err = cn.Rd.ReadStringReply()
return cmd.err
}

Expand Down Expand Up @@ -286,7 +285,7 @@ func (cmd *IntCmd) String() string {
}

func (cmd *IntCmd) readReply(cn *pool.Conn) error {
cmd.val, cmd.err = readIntReply(cn)
cmd.val, cmd.err = cn.Rd.ReadIntReply()
return cmd.err
}

Expand Down Expand Up @@ -325,7 +324,7 @@ func (cmd *DurationCmd) String() string {
}

func (cmd *DurationCmd) readReply(cn *pool.Conn) error {
n, err := readIntReply(cn)
n, err := cn.Rd.ReadIntReply()
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -367,7 +366,7 @@ func (cmd *BoolCmd) String() string {
var ok = []byte("OK")

func (cmd *BoolCmd) readReply(cn *pool.Conn) error {
v, err := readReply(cn, nil)
v, err := cn.Rd.ReadReply(nil)
// `SET key value NX` returns nil when key already exists. But
// `SETNX key value` returns bool (0/1). So convert nil to bool.
// TODO: is this okay?
Expand Down Expand Up @@ -410,7 +409,7 @@ func (cmd *StringCmd) reset() {
}

func (cmd *StringCmd) Val() string {
return bytesToString(cmd.val)
return string(cmd.val)
}

func (cmd *StringCmd) Result() (string, error) {
Expand Down Expand Up @@ -446,15 +445,15 @@ func (cmd *StringCmd) Scan(val interface{}) error {
if cmd.err != nil {
return cmd.err
}
return scan(cmd.val, val)
return proto.Scan(cmd.val, val)
}

func (cmd *StringCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *StringCmd) readReply(cn *pool.Conn) error {
b, err := readBytesReply(cn)
b, err := cn.Rd.ReadBytesReply()
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -498,7 +497,7 @@ func (cmd *FloatCmd) String() string {
}

func (cmd *FloatCmd) readReply(cn *pool.Conn) error {
cmd.val, cmd.err = readFloatReply(cn)
cmd.val, cmd.err = cn.Rd.ReadFloatReply()
return cmd.err
}

Expand Down Expand Up @@ -533,7 +532,7 @@ func (cmd *StringSliceCmd) String() string {
}

func (cmd *StringSliceCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, stringSliceParser)
v, err := cn.Rd.ReadArrayReply(stringSliceParser)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -573,7 +572,7 @@ func (cmd *BoolSliceCmd) String() string {
}

func (cmd *BoolSliceCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, boolSliceParser)
v, err := cn.Rd.ReadArrayReply(boolSliceParser)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -613,7 +612,7 @@ func (cmd *StringStringMapCmd) String() string {
}

func (cmd *StringStringMapCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, stringStringMapParser)
v, err := cn.Rd.ReadArrayReply(stringStringMapParser)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -653,7 +652,7 @@ func (cmd *StringIntMapCmd) reset() {
}

func (cmd *StringIntMapCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, stringIntMapParser)
v, err := cn.Rd.ReadArrayReply(stringIntMapParser)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -693,7 +692,7 @@ func (cmd *ZSliceCmd) String() string {
}

func (cmd *ZSliceCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, zSliceParser)
v, err := cn.Rd.ReadArrayReply(zSliceParser)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -737,7 +736,7 @@ func (cmd *ScanCmd) String() string {
}

func (cmd *ScanCmd) readReply(cn *pool.Conn) error {
page, cursor, err := readScanReply(cn)
page, cursor, err := cn.Rd.ReadScanReply()
if err != nil {
cmd.err = err
return cmd.err
Expand Down Expand Up @@ -789,7 +788,7 @@ func (cmd *ClusterSlotsCmd) reset() {
}

func (cmd *ClusterSlotsCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, clusterSlotsParser)
v, err := cn.Rd.ReadArrayReply(clusterSlotsParser)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -874,7 +873,7 @@ func (cmd *GeoLocationCmd) String() string {
}

func (cmd *GeoLocationCmd) readReply(cn *pool.Conn) error {
reply, err := readArrayReply(cn, newGeoLocationSliceParser(cmd.q))
reply, err := cn.Rd.ReadArrayReply(newGeoLocationSliceParser(cmd.q))
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -924,7 +923,7 @@ func (cmd *CommandsInfoCmd) reset() {
}

func (cmd *CommandsInfoCmd) readReply(cn *pool.Conn) error {
v, err := readArrayReply(cn, commandInfoSliceParser)
v, err := cn.Rd.ReadArrayReply(commandInfoSliceParser)
if err != nil {
cmd.err = err
return err
Expand Down
19 changes: 4 additions & 15 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,9 @@ import (
"time"

"gopkg.in/redis.v4/internal"
"gopkg.in/redis.v4/internal/errors"
)

func formatInt(i int64) string {
return strconv.FormatInt(i, 10)
}

func formatUint(i uint64) string {
return strconv.FormatUint(i, 10)
}

func formatFloat(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}

func readTimeout(timeout time.Duration) time.Duration {
if timeout == 0 {
return 0
Expand All @@ -38,7 +27,7 @@ func formatMs(dur time.Duration) string {
dur, time.Millisecond,
)
}
return formatInt(int64(dur / time.Millisecond))
return strconv.FormatInt(int64(dur/time.Millisecond), 10)
}

func formatSec(dur time.Duration) string {
Expand All @@ -48,7 +37,7 @@ func formatSec(dur time.Duration) string {
dur, time.Second,
)
}
return formatInt(int64(dur / time.Second))
return strconv.FormatInt(int64(dur/time.Second), 10)
}

type cmdable struct {
Expand Down Expand Up @@ -1515,7 +1504,7 @@ func (c *cmdable) shutdown(modifier string) *StatusCmd {
}
} else {
// Server did not quit. String reply contains the reason.
cmd.err = errorf(cmd.val)
cmd.err = errors.RedisError(cmd.val)
cmd.val = ""
}
return cmd
Expand Down
81 changes: 0 additions & 81 deletions error.go

This file was deleted.

Loading

0 comments on commit 7d856c5

Please sign in to comment.