Skip to content

Commit 30ce5eb

Browse files
committed
Cleanup error handling code.
1 parent d2ae7d8 commit 30ce5eb

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

cluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ func (c *ClusterClient) process(cmd Cmder) {
195195

196196
// If there is no (real) error, we are done!
197197
err := cmd.Err()
198-
if err == nil || err == Nil || err == TxFailedErr {
198+
if err == nil {
199199
return
200200
}
201201

202202
// On network errors try random node.
203-
if isNetworkError(err) {
203+
if shouldRetry(err) {
204204
client, err = c.randomClient()
205205
if err != nil {
206206
return

error.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ func (err redisError) Error() string {
2525
return err.s
2626
}
2727

28+
func isInternalError(err error) bool {
29+
_, ok := err.(redisError)
30+
return ok
31+
}
32+
2833
func isNetworkError(err error) bool {
2934
if err == io.EOF {
3035
return true
@@ -37,7 +42,7 @@ func isBadConn(err error, allowTimeout bool) bool {
3742
if err == nil {
3843
return false
3944
}
40-
if _, ok := err.(redisError); ok {
45+
if isInternalError(err) {
4146
return false
4247
}
4348
if allowTimeout {
@@ -53,27 +58,24 @@ func isMovedError(err error) (moved bool, ask bool, addr string) {
5358
return
5459
}
5560

56-
parts := strings.SplitN(err.Error(), " ", 3)
57-
if len(parts) != 3 {
58-
return
59-
}
60-
61-
switch parts[0] {
62-
case "MOVED":
61+
s := err.Error()
62+
if strings.HasPrefix(s, "MOVED ") {
6363
moved = true
64-
addr = parts[2]
65-
case "ASK":
64+
} else if strings.HasPrefix(s, "ASK ") {
6665
ask = true
67-
addr = parts[2]
66+
} else {
67+
return
6868
}
6969

70+
ind := strings.LastIndexByte(s, ' ')
71+
if ind == -1 {
72+
return false, false, ""
73+
}
74+
addr = s[ind+1:]
7075
return
7176
}
7277

7378
// shouldRetry reports whether failed command should be retried.
7479
func shouldRetry(err error) bool {
75-
if err == nil {
76-
return false
77-
}
7880
return isNetworkError(err)
7981
}

redis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ func (c *baseClient) process(cmd Cmder) {
104104
if err := writeCmd(cn, cmd); err != nil {
105105
c.putConn(cn, err, false)
106106
cmd.setErr(err)
107-
if shouldRetry(err) {
107+
if err != nil && shouldRetry(err) {
108108
continue
109109
}
110110
return
111111
}
112112

113113
err = cmd.readReply(cn)
114114
c.putConn(cn, err, readTimeout != nil)
115-
if shouldRetry(err) {
115+
if err != nil && shouldRetry(err) {
116116
continue
117117
}
118118

0 commit comments

Comments
 (0)