Skip to content

Commit

Permalink
Don't remove connection from the pool on redis errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Jun 18, 2014
1 parent 5019689 commit deb41df
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
23 changes: 23 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package redis

import (
"fmt"
)

// Redis nil reply.
var Nil = errorf("redis: nil")

// Redis transaction failed.
var TxFailedErr = errorf("redis: transaction failed")

type redisError struct {
s string
}

func errorf(s string, args ...interface{}) redisError {
return redisError{s: fmt.Sprintf(s, args...)}
}

func (err redisError) Error() string {
return err.s
}
8 changes: 1 addition & 7 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import (

type multiBulkParser func(rd reader, n int64) (interface{}, error)

// Redis nil reply.
var Nil = errors.New("redis: nil")

// Redis transaction failed.
var TxFailedErr = errors.New("redis: transaction failed")

var (
errReaderTooSmall = errors.New("redis: reader is too small")
errInvalidReplyType = errors.New("redis: invalid reply type")
Expand Down Expand Up @@ -132,7 +126,7 @@ func parseReply(rd reader, p multiBulkParser) (interface{}, error) {

switch line[0] {
case '-':
return nil, errors.New(string(line[1:]))
return nil, errorf(string(line[1:]))
case '+':
return string(line[1:]), nil
case ':':
Expand Down
12 changes: 7 additions & 5 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ func (c *baseClient) init(cn *conn, password string, db int64) error {
return nil
}

func (c *baseClient) freeConn(cn *conn, err error) {
if err == Nil || err == TxFailedErr {
c.putConn(cn)
} else {
c.removeConn(cn)
func (c *baseClient) freeConn(cn *conn, ei error) error {
if cn.rd.Buffered() > 0 {
return c.connPool.Remove(cn)
}
if _, ok := ei.(redisError); ok {
return c.connPool.Put(cn)
}
return c.connPool.Remove(cn)
}

func (c *baseClient) removeConn(cn *conn) {
Expand Down

0 comments on commit deb41df

Please sign in to comment.