2014-06-18 16:55:49 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-04 16:46:57 +03:00
|
|
|
"io"
|
|
|
|
"net"
|
2015-03-18 13:41:24 +03:00
|
|
|
"strings"
|
2014-06-18 16:55:49 +04:00
|
|
|
)
|
|
|
|
|
2015-05-23 14:33:33 +03:00
|
|
|
// Redis nil reply, .e.g. when key does not exist.
|
2014-06-18 16:55:49 +04:00
|
|
|
var Nil = errorf("redis: nil")
|
|
|
|
|
|
|
|
// Redis transaction failed.
|
|
|
|
var TxFailedErr = errorf("redis: transaction failed")
|
|
|
|
|
|
|
|
type redisError struct {
|
|
|
|
s string
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorf(s string, args ...interface{}) redisError {
|
|
|
|
return redisError{s: fmt.Sprintf(s, args...)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err redisError) Error() string {
|
|
|
|
return err.s
|
|
|
|
}
|
2015-04-04 16:46:57 +03:00
|
|
|
|
2016-03-19 17:33:14 +03:00
|
|
|
func isInternalError(err error) bool {
|
|
|
|
_, ok := err.(redisError)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2015-04-04 16:46:57 +03:00
|
|
|
func isNetworkError(err error) bool {
|
2015-11-14 15:44:16 +03:00
|
|
|
if err == io.EOF {
|
2015-04-04 16:46:57 +03:00
|
|
|
return true
|
|
|
|
}
|
2015-11-14 15:44:16 +03:00
|
|
|
_, ok := err.(net.Error)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2016-03-08 18:18:52 +03:00
|
|
|
func isBadConn(err error, allowTimeout bool) bool {
|
2016-03-01 13:31:06 +03:00
|
|
|
if err == nil {
|
|
|
|
return false
|
2015-11-14 15:44:16 +03:00
|
|
|
}
|
2016-03-19 17:33:14 +03:00
|
|
|
if isInternalError(err) {
|
2015-11-14 15:44:16 +03:00
|
|
|
return false
|
|
|
|
}
|
2016-03-08 18:18:52 +03:00
|
|
|
if allowTimeout {
|
|
|
|
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
|
|
|
return false
|
|
|
|
}
|
2015-11-14 15:44:16 +03:00
|
|
|
}
|
|
|
|
return true
|
2015-04-04 16:46:57 +03:00
|
|
|
}
|
2015-03-18 13:41:24 +03:00
|
|
|
|
|
|
|
func isMovedError(err error) (moved bool, ask bool, addr string) {
|
|
|
|
if _, ok := err.(redisError); !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-19 17:33:14 +03:00
|
|
|
s := err.Error()
|
|
|
|
if strings.HasPrefix(s, "MOVED ") {
|
2015-03-18 13:41:24 +03:00
|
|
|
moved = true
|
2016-03-19 17:33:14 +03:00
|
|
|
} else if strings.HasPrefix(s, "ASK ") {
|
2015-03-18 13:41:24 +03:00
|
|
|
ask = true
|
2016-03-19 17:33:14 +03:00
|
|
|
} else {
|
|
|
|
return
|
2015-03-18 13:41:24 +03:00
|
|
|
}
|
|
|
|
|
2016-03-19 17:33:14 +03:00
|
|
|
ind := strings.LastIndexByte(s, ' ')
|
|
|
|
if ind == -1 {
|
|
|
|
return false, false, ""
|
|
|
|
}
|
|
|
|
addr = s[ind+1:]
|
2015-03-18 13:41:24 +03:00
|
|
|
return
|
|
|
|
}
|
2015-05-10 15:33:04 +03:00
|
|
|
|
|
|
|
// shouldRetry reports whether failed command should be retried.
|
|
|
|
func shouldRetry(err error) bool {
|
|
|
|
return isNetworkError(err)
|
|
|
|
}
|