Don't remove connection from the pool on redis errors.

This commit is contained in:
Vladimir Mihailenco 2014-06-18 15:55:49 +03:00
parent 5019689b0e
commit deb41df992
3 changed files with 31 additions and 12 deletions

23
error.go Normal file
View File

@ -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
}

View File

@ -10,12 +10,6 @@ import (
type multiBulkParser func(rd reader, n int64) (interface{}, error) 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 ( var (
errReaderTooSmall = errors.New("redis: reader is too small") errReaderTooSmall = errors.New("redis: reader is too small")
errInvalidReplyType = errors.New("redis: invalid reply type") errInvalidReplyType = errors.New("redis: invalid reply type")
@ -132,7 +126,7 @@ func parseReply(rd reader, p multiBulkParser) (interface{}, error) {
switch line[0] { switch line[0] {
case '-': case '-':
return nil, errors.New(string(line[1:])) return nil, errorf(string(line[1:]))
case '+': case '+':
return string(line[1:]), nil return string(line[1:]), nil
case ':': case ':':

View File

@ -65,12 +65,14 @@ func (c *baseClient) init(cn *conn, password string, db int64) error {
return nil return nil
} }
func (c *baseClient) freeConn(cn *conn, err error) { func (c *baseClient) freeConn(cn *conn, ei error) error {
if err == Nil || err == TxFailedErr { if cn.rd.Buffered() > 0 {
c.putConn(cn) return c.connPool.Remove(cn)
} else {
c.removeConn(cn)
} }
if _, ok := ei.(redisError); ok {
return c.connPool.Put(cn)
}
return c.connPool.Remove(cn)
} }
func (c *baseClient) removeConn(cn *conn) { func (c *baseClient) removeConn(cn *conn) {