Skip to content

Commit deb41df

Browse files
committed
Don't remove connection from the pool on redis errors.
1 parent 5019689 commit deb41df

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

error.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package redis
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// Redis nil reply.
8+
var Nil = errorf("redis: nil")
9+
10+
// Redis transaction failed.
11+
var TxFailedErr = errorf("redis: transaction failed")
12+
13+
type redisError struct {
14+
s string
15+
}
16+
17+
func errorf(s string, args ...interface{}) redisError {
18+
return redisError{s: fmt.Sprintf(s, args...)}
19+
}
20+
21+
func (err redisError) Error() string {
22+
return err.s
23+
}

parser.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ import (
1010

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

13-
// Redis nil reply.
14-
var Nil = errors.New("redis: nil")
15-
16-
// Redis transaction failed.
17-
var TxFailedErr = errors.New("redis: transaction failed")
18-
1913
var (
2014
errReaderTooSmall = errors.New("redis: reader is too small")
2115
errInvalidReplyType = errors.New("redis: invalid reply type")
@@ -132,7 +126,7 @@ func parseReply(rd reader, p multiBulkParser) (interface{}, error) {
132126

133127
switch line[0] {
134128
case '-':
135-
return nil, errors.New(string(line[1:]))
129+
return nil, errorf(string(line[1:]))
136130
case '+':
137131
return string(line[1:]), nil
138132
case ':':

redis.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ func (c *baseClient) init(cn *conn, password string, db int64) error {
6565
return nil
6666
}
6767

68-
func (c *baseClient) freeConn(cn *conn, err error) {
69-
if err == Nil || err == TxFailedErr {
70-
c.putConn(cn)
71-
} else {
72-
c.removeConn(cn)
68+
func (c *baseClient) freeConn(cn *conn, ei error) error {
69+
if cn.rd.Buffered() > 0 {
70+
return c.connPool.Remove(cn)
71+
}
72+
if _, ok := ei.(redisError); ok {
73+
return c.connPool.Put(cn)
7374
}
75+
return c.connPool.Remove(cn)
7476
}
7577

7678
func (c *baseClient) removeConn(cn *conn) {

0 commit comments

Comments
 (0)