Cleanup error handling code.

This commit is contained in:
Vladimir Mihailenco 2016-03-19 16:33:14 +02:00
parent d2ae7d8707
commit 30ce5ebd57
3 changed files with 20 additions and 18 deletions

View File

@ -195,12 +195,12 @@ func (c *ClusterClient) process(cmd Cmder) {
// If there is no (real) error, we are done!
err := cmd.Err()
if err == nil || err == Nil || err == TxFailedErr {
if err == nil {
return
}
// On network errors try random node.
if isNetworkError(err) {
if shouldRetry(err) {
client, err = c.randomClient()
if err != nil {
return

View File

@ -25,6 +25,11 @@ func (err redisError) Error() string {
return err.s
}
func isInternalError(err error) bool {
_, ok := err.(redisError)
return ok
}
func isNetworkError(err error) bool {
if err == io.EOF {
return true
@ -37,7 +42,7 @@ func isBadConn(err error, allowTimeout bool) bool {
if err == nil {
return false
}
if _, ok := err.(redisError); ok {
if isInternalError(err) {
return false
}
if allowTimeout {
@ -53,27 +58,24 @@ func isMovedError(err error) (moved bool, ask bool, addr string) {
return
}
parts := strings.SplitN(err.Error(), " ", 3)
if len(parts) != 3 {
s := err.Error()
if strings.HasPrefix(s, "MOVED ") {
moved = true
} else if strings.HasPrefix(s, "ASK ") {
ask = true
} else {
return
}
switch parts[0] {
case "MOVED":
moved = true
addr = parts[2]
case "ASK":
ask = true
addr = parts[2]
ind := strings.LastIndexByte(s, ' ')
if ind == -1 {
return false, false, ""
}
addr = s[ind+1:]
return
}
// shouldRetry reports whether failed command should be retried.
func shouldRetry(err error) bool {
if err == nil {
return false
}
return isNetworkError(err)
}

View File

@ -104,7 +104,7 @@ func (c *baseClient) process(cmd Cmder) {
if err := writeCmd(cn, cmd); err != nil {
c.putConn(cn, err, false)
cmd.setErr(err)
if shouldRetry(err) {
if err != nil && shouldRetry(err) {
continue
}
return
@ -112,7 +112,7 @@ func (c *baseClient) process(cmd Cmder) {
err = cmd.readReply(cn)
c.putConn(cn, err, readTimeout != nil)
if shouldRetry(err) {
if err != nil && shouldRetry(err) {
continue
}