diff --git a/error.go b/error.go new file mode 100644 index 0000000..667fffd --- /dev/null +++ b/error.go @@ -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 +} diff --git a/parser.go b/parser.go index 729946d..a1a0f55 100644 --- a/parser.go +++ b/parser.go @@ -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") @@ -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 ':': diff --git a/redis.go b/redis.go index 7e96a26..8ebab47 100644 --- a/redis.go +++ b/redis.go @@ -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) {