Merge pull request #290 from go-redis/fix/error-handling

Cleanup error handling code.
This commit is contained in:
Vladimir Mihailenco 2016-03-20 11:06:51 +03:00
commit b9c6dcef57
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! // If there is no (real) error, we are done!
err := cmd.Err() err := cmd.Err()
if err == nil || err == Nil || err == TxFailedErr { if err == nil {
return return
} }
// On network errors try random node. // On network errors try random node.
if isNetworkError(err) { if shouldRetry(err) {
client, err = c.randomClient() client, err = c.randomClient()
if err != nil { if err != nil {
return return

View File

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

View File

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