Skip to content

Commit

Permalink
Get rid of custom bufio package.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 3, 2015
1 parent dace69d commit 58cb170
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 150 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ go:
- tip

install:
- go get gopkg.in/bufio.v1
- go get gopkg.in/bsm/ratelimit.v1
- go get github.com/onsi/ginkgo
- go get github.com/onsi/gomega
Expand Down
2 changes: 1 addition & 1 deletion cluster_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (pipe *ClusterPipeline) execClusterCmds(

var firstCmdErr error
for i, cmd := range cmds {
err := cmd.parseReply(cn.rd)
err := cmd.parseReply(cn)
if err == nil {
continue
}
Expand Down
68 changes: 32 additions & 36 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"strconv"
"strings"
"time"

"gopkg.in/bufio.v1"
)

var (
Expand All @@ -30,7 +28,7 @@ var (

type Cmder interface {
args() []interface{}
parseReply(*bufio.Reader) error
parseReply(*conn) error
setErr(error)
reset()

Expand Down Expand Up @@ -154,8 +152,8 @@ func (cmd *Cmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *Cmd) parseReply(rd *bufio.Reader) error {
cmd.val, cmd.err = parseReply(rd, parseSlice)
func (cmd *Cmd) parseReply(cn *conn) error {
cmd.val, cmd.err = parseReply(cn, parseSlice)
// Convert to string to preserve old behaviour.
// TODO: remove in v4
if v, ok := cmd.val.([]byte); ok {
Expand Down Expand Up @@ -193,8 +191,8 @@ func (cmd *SliceCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *SliceCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseSlice)
func (cmd *SliceCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, parseSlice)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -236,8 +234,8 @@ func (cmd *StatusCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *StatusCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, nil)
func (cmd *StatusCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, nil)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -275,8 +273,8 @@ func (cmd *IntCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *IntCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, nil)
func (cmd *IntCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, nil)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -318,8 +316,8 @@ func (cmd *DurationCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *DurationCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, nil)
func (cmd *DurationCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, nil)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -359,8 +357,8 @@ func (cmd *BoolCmd) String() string {

var ok = []byte("OK")

func (cmd *BoolCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, nil)
func (cmd *BoolCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, nil)
// `SET key value NX` returns nil when key already exists, which
// is inconsistent with `SETNX key value`.
// TODO: is this okay?
Expand Down Expand Up @@ -445,15 +443,13 @@ func (cmd *StringCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *StringCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, nil)
func (cmd *StringCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, nil)
if err != nil {
cmd.err = err
return err
}
b := v.([]byte)
cmd.val = make([]byte, len(b))
copy(cmd.val, b)
cmd.val = cn.copyBuf(v.([]byte))
return nil
}

Expand Down Expand Up @@ -486,8 +482,8 @@ func (cmd *FloatCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *FloatCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, nil)
func (cmd *FloatCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, nil)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -526,8 +522,8 @@ func (cmd *StringSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *StringSliceCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseStringSlice)
func (cmd *StringSliceCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, parseStringSlice)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -565,8 +561,8 @@ func (cmd *BoolSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *BoolSliceCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseBoolSlice)
func (cmd *BoolSliceCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, parseBoolSlice)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -604,8 +600,8 @@ func (cmd *StringStringMapCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *StringStringMapCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseStringStringMap)
func (cmd *StringStringMapCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, parseStringStringMap)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -643,8 +639,8 @@ func (cmd *StringIntMapCmd) reset() {
cmd.err = nil
}

func (cmd *StringIntMapCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseStringIntMap)
func (cmd *StringIntMapCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, parseStringIntMap)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -682,8 +678,8 @@ func (cmd *ZSliceCmd) String() string {
return cmdString(cmd, cmd.val)
}

func (cmd *ZSliceCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseZSlice)
func (cmd *ZSliceCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, parseZSlice)
if err != nil {
cmd.err = err
return err
Expand Down Expand Up @@ -723,8 +719,8 @@ func (cmd *ScanCmd) String() string {
return cmdString(cmd, cmd.keys)
}

func (cmd *ScanCmd) parseReply(rd *bufio.Reader) error {
vi, err := parseReply(rd, parseSlice)
func (cmd *ScanCmd) parseReply(cn *conn) error {
vi, err := parseReply(cn, parseSlice)
if err != nil {
cmd.err = err
return cmd.err
Expand Down Expand Up @@ -778,8 +774,8 @@ func (cmd *ClusterSlotCmd) reset() {
cmd.err = nil
}

func (cmd *ClusterSlotCmd) parseReply(rd *bufio.Reader) error {
v, err := parseReply(rd, parseClusterSlotInfoSlice)
func (cmd *ClusterSlotCmd) parseReply(cn *conn) error {
v, err := parseReply(cn, parseClusterSlotInfoSlice)
if err != nil {
cmd.err = err
return err
Expand Down
20 changes: 17 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package redis

import (
"bufio"
"net"
"time"

"gopkg.in/bufio.v1"
)

const defaultBufSize = 4096

var (
zeroTime = time.Time{}
)
Expand All @@ -30,7 +31,7 @@ func newConnDialer(opt *Options) func() (*conn, error) {
}
cn := &conn{
netcn: netcn,
buf: make([]byte, 0, 64),
buf: make([]byte, defaultBufSize),
}
cn.rd = bufio.NewReader(cn)
return cn, cn.init(opt)
Expand Down Expand Up @@ -102,3 +103,16 @@ func (cn *conn) RemoteAddr() net.Addr {
func (cn *conn) Close() error {
return cn.netcn.Close()
}

func isSameSlice(s1, s2 []byte) bool {
return len(s1) > 0 && len(s2) > 0 && &s1[0] == &s2[0]
}

func (cn *conn) copyBuf(b []byte) []byte {
if isSameSlice(b, cn.buf) {
new := make([]byte, len(b))
copy(new, b)
return new
}
return b
}
6 changes: 3 additions & 3 deletions multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ func (c *Multi) execCmds(cn *conn, cmds []Cmder) error {

// Parse queued replies.
for i := 0; i < cmdsLen; i++ {
if err := statusCmd.parseReply(cn.rd); err != nil {
if err := statusCmd.parseReply(cn); err != nil {
setCmdsErr(cmds[1:len(cmds)-1], err)
return err
}
}

// Parse number of replies.
line, err := readLine(cn.rd)
line, err := readLine(cn)
if err != nil {
setCmdsErr(cmds[1:len(cmds)-1], err)
return err
Expand All @@ -143,7 +143,7 @@ func (c *Multi) execCmds(cn *conn, cmds []Cmder) error {
// Loop starts from 1 to omit MULTI cmd.
for i := 1; i < cmdsLen; i++ {
cmd := cmds[i]
if err := cmd.parseReply(cn.rd); err != nil {
if err := cmd.parseReply(cn); err != nil {
if firstCmdErr == nil {
firstCmdErr = err
}
Expand Down
Loading

0 comments on commit 58cb170

Please sign in to comment.