Skip to content

Commit 4e9cea8

Browse files
committed
Add proper SingleConnPool implementation
1 parent d6a99e7 commit 4e9cea8

File tree

7 files changed

+211
-68
lines changed

7 files changed

+211
-68
lines changed

cluster.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -787,15 +787,15 @@ func (c *ClusterClient) process(ctx context.Context, cmd Cmder) error {
787787
}
788788

789789
// If slave is loading - pick another node.
790-
if c.opt.ReadOnly && internal.IsLoadingError(err) {
790+
if c.opt.ReadOnly && isLoadingError(err) {
791791
node.MarkAsFailing()
792792
node = nil
793793
continue
794794
}
795795

796796
var moved bool
797797
var addr string
798-
moved, ask, addr = internal.IsMovedError(err)
798+
moved, ask, addr = isMovedError(err)
799799
if moved || ask {
800800
node, err = c.nodes.Get(addr)
801801
if err != nil {
@@ -804,12 +804,12 @@ func (c *ClusterClient) process(ctx context.Context, cmd Cmder) error {
804804
continue
805805
}
806806

807-
if err == pool.ErrClosed || internal.IsReadOnlyError(err) {
807+
if err == pool.ErrClosed || isReadOnlyError(err) {
808808
node = nil
809809
continue
810810
}
811811

812-
if internal.IsRetryableError(err, true) {
812+
if isRetryableError(err, true) {
813813
// First retry the same node.
814814
if attempt == 0 {
815815
continue
@@ -1173,9 +1173,9 @@ func (c *ClusterClient) pipelineReadCmds(
11731173
continue
11741174
}
11751175

1176-
if c.opt.ReadOnly && internal.IsLoadingError(err) {
1176+
if c.opt.ReadOnly && isLoadingError(err) {
11771177
node.MarkAsFailing()
1178-
} else if internal.IsRedisError(err) {
1178+
} else if isRedisError(err) {
11791179
continue
11801180
}
11811181

@@ -1192,7 +1192,7 @@ func (c *ClusterClient) pipelineReadCmds(
11921192
func (c *ClusterClient) checkMovedErr(
11931193
cmd Cmder, err error, failedCmds *cmdsMap,
11941194
) bool {
1195-
moved, ask, addr := internal.IsMovedError(err)
1195+
moved, ask, addr := isMovedError(err)
11961196

11971197
if moved {
11981198
c.state.LazyReload()
@@ -1346,7 +1346,7 @@ func (c *ClusterClient) txPipelineReadQueued(
13461346
continue
13471347
}
13481348

1349-
if c.checkMovedErr(cmd, err, failedCmds) || internal.IsRedisError(err) {
1349+
if c.checkMovedErr(cmd, err, failedCmds) || isRedisError(err) {
13501350
continue
13511351
}
13521352

@@ -1418,7 +1418,7 @@ func (c *ClusterClient) WatchContext(ctx context.Context, fn func(*Tx) error, ke
14181418
c.state.LazyReload()
14191419
}
14201420

1421-
moved, ask, addr := internal.IsMovedError(err)
1421+
moved, ask, addr := isMovedError(err)
14221422
if moved || ask {
14231423
node, err = c.nodes.Get(addr)
14241424
if err != nil {
@@ -1427,15 +1427,15 @@ func (c *ClusterClient) WatchContext(ctx context.Context, fn func(*Tx) error, ke
14271427
continue
14281428
}
14291429

1430-
if err == pool.ErrClosed || internal.IsReadOnlyError(err) {
1430+
if err == pool.ErrClosed || isReadOnlyError(err) {
14311431
node, err = c.slotMasterNode(slot)
14321432
if err != nil {
14331433
return err
14341434
}
14351435
continue
14361436
}
14371437

1438-
if internal.IsRetryableError(err, true) {
1438+
if isRetryableError(err, true) {
14391439
continue
14401440
}
14411441

internal/error.go renamed to error.go

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
package internal
1+
package redis
22

33
import (
44
"context"
5-
"errors"
65
"io"
76
"net"
87
"strings"
98

9+
"github.com/go-redis/redis/internal/pool"
1010
"github.com/go-redis/redis/internal/proto"
1111
)
1212

13-
var ErrSingleConnPoolClosed = errors.New("redis: SingleConnPool is closed")
14-
15-
func IsRetryableError(err error, retryTimeout bool) bool {
13+
func isRetryableError(err error, retryTimeout bool) bool {
1614
switch err {
17-
case nil, context.Canceled, context.DeadlineExceeded:
15+
case nil, context.Canceled, context.DeadlineExceeded, pool.ErrBadConn:
1816
return false
1917
case io.EOF:
2018
return true
@@ -25,9 +23,6 @@ func IsRetryableError(err error, retryTimeout bool) bool {
2523
}
2624
return true
2725
}
28-
if err == ErrSingleConnPoolClosed {
29-
return true
30-
}
3126

3227
s := err.Error()
3328
if s == "ERR max number of clients reached" {
@@ -45,18 +40,20 @@ func IsRetryableError(err error, retryTimeout bool) bool {
4540
return false
4641
}
4742

48-
func IsRedisError(err error) bool {
43+
func isRedisError(err error) bool {
4944
_, ok := err.(proto.RedisError)
5045
return ok
5146
}
5247

53-
func IsBadConn(err error, allowTimeout bool) bool {
54-
if err == nil {
48+
func isBadConn(err error, allowTimeout bool) bool {
49+
switch err {
50+
case nil:
5551
return false
52+
case pool.ErrBadConn:
53+
return true
5654
}
57-
if IsRedisError(err) {
58-
// #790
59-
return IsReadOnlyError(err)
55+
if isRedisError(err) {
56+
return isReadOnlyError(err) // #790
6057
}
6158
if allowTimeout {
6259
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
@@ -66,8 +63,8 @@ func IsBadConn(err error, allowTimeout bool) bool {
6663
return true
6764
}
6865

69-
func IsMovedError(err error) (moved bool, ask bool, addr string) {
70-
if !IsRedisError(err) {
66+
func isMovedError(err error) (moved bool, ask bool, addr string) {
67+
if !isRedisError(err) {
7168
return
7269
}
7370

@@ -89,10 +86,10 @@ func IsMovedError(err error) (moved bool, ask bool, addr string) {
8986
return
9087
}
9188

92-
func IsLoadingError(err error) bool {
89+
func isLoadingError(err error) bool {
9390
return strings.HasPrefix(err.Error(), "LOADING ")
9491
}
9592

96-
func IsReadOnlyError(err error) bool {
93+
func isReadOnlyError(err error) bool {
9794
return strings.HasPrefix(err.Error(), "READONLY ")
9895
}

example_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,27 @@ func ExampleClient() {
151151
// missing_key does not exist
152152
}
153153

154+
func ExampleConn() {
155+
conn := redisdb.Conn()
156+
157+
err := conn.ClientSetName("foobar").Err()
158+
if err != nil {
159+
panic(err)
160+
}
161+
162+
// Open other connections.
163+
for i := 0; i < 10; i++ {
164+
go redisdb.Ping()
165+
}
166+
167+
s, err := conn.ClientGetName().Result()
168+
if err != nil {
169+
panic(err)
170+
}
171+
fmt.Println(s)
172+
// Output: foobar
173+
}
174+
154175
func ExampleClient_Set() {
155176
// Last argument is expiration. Zero means the key has no
156177
// expiration time.

0 commit comments

Comments
 (0)