2016-10-09 14:38:31 +03:00
|
|
|
package internal
|
2016-07-02 15:52:10 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const Nil = RedisError("redis: nil")
|
|
|
|
|
|
|
|
type RedisError string
|
|
|
|
|
|
|
|
func (e RedisError) Error() string { return string(e) }
|
|
|
|
|
2016-10-09 14:38:31 +03:00
|
|
|
func IsRetryableError(err error) bool {
|
2017-05-25 08:08:44 +03:00
|
|
|
return IsNetworkError(err) || err.Error() == "ERR max number of clients reached"
|
2016-07-02 15:52:10 +03:00
|
|
|
}
|
|
|
|
|
2016-10-09 14:38:31 +03:00
|
|
|
func IsInternalError(err error) bool {
|
2016-07-02 15:52:10 +03:00
|
|
|
_, ok := err.(RedisError)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2016-10-09 14:38:31 +03:00
|
|
|
func IsNetworkError(err error) bool {
|
2016-07-02 15:52:10 +03:00
|
|
|
if err == io.EOF {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
_, ok := err.(net.Error)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsBadConn(err error, allowTimeout bool) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-10-09 14:38:31 +03:00
|
|
|
if IsInternalError(err) {
|
2016-07-02 15:52:10 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if allowTimeout {
|
|
|
|
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-10-09 14:38:31 +03:00
|
|
|
func IsMovedError(err error) (moved bool, ask bool, addr string) {
|
|
|
|
if !IsInternalError(err) {
|
2016-07-02 15:52:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s := err.Error()
|
|
|
|
if strings.HasPrefix(s, "MOVED ") {
|
|
|
|
moved = true
|
|
|
|
} else if strings.HasPrefix(s, "ASK ") {
|
|
|
|
ask = true
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-14 14:00:34 +03:00
|
|
|
ind := strings.LastIndex(s, " ")
|
2016-07-02 15:52:10 +03:00
|
|
|
if ind == -1 {
|
|
|
|
return false, false, ""
|
|
|
|
}
|
|
|
|
addr = s[ind+1:]
|
|
|
|
return
|
|
|
|
}
|
2016-10-09 06:30:01 +03:00
|
|
|
|
2016-10-09 14:38:31 +03:00
|
|
|
func IsLoadingError(err error) bool {
|
2016-10-09 11:18:57 +03:00
|
|
|
return strings.HasPrefix(err.Error(), "LOADING")
|
2016-10-09 06:30:01 +03:00
|
|
|
}
|
2016-12-13 18:28:39 +03:00
|
|
|
|
|
|
|
func IsExecAbortError(err error) bool {
|
|
|
|
return strings.HasPrefix(err.Error(), "EXECABORT")
|
|
|
|
}
|