redis/internal/error.go

92 lines
1.5 KiB
Go
Raw Normal View History

package internal
2016-07-02 15:52:10 +03:00
import (
2019-07-30 12:13:00 +03:00
"context"
2016-07-02 15:52:10 +03:00
"io"
"net"
"strings"
2018-02-22 15:14:30 +03:00
"github.com/go-redis/redis/internal/proto"
)
2016-07-02 15:52:10 +03:00
2018-08-23 16:13:42 +03:00
func IsRetryableError(err error, retryTimeout bool) bool {
2019-07-30 12:13:00 +03:00
switch err {
case nil, context.Canceled, context.DeadlineExceeded:
2018-10-14 11:27:34 +03:00
return false
2019-07-30 12:13:00 +03:00
case io.EOF:
2018-08-23 16:13:42 +03:00
return true
}
if netErr, ok := err.(net.Error); ok {
if netErr.Timeout() {
return retryTimeout
}
return true
2017-08-31 15:22:47 +03:00
}
s := err.Error()
if s == "ERR max number of clients reached" {
return true
}
if strings.HasPrefix(s, "LOADING ") {
return true
}
2018-06-18 13:07:31 +03:00
if strings.HasPrefix(s, "READONLY ") {
return true
}
2017-08-31 15:22:47 +03:00
if strings.HasPrefix(s, "CLUSTERDOWN ") {
return true
}
return false
2016-07-02 15:52:10 +03:00
}
2017-08-31 15:22:47 +03:00
func IsRedisError(err error) bool {
2018-02-22 15:14:30 +03:00
_, ok := err.(proto.RedisError)
2016-07-02 15:52:10 +03:00
return ok
}
func IsBadConn(err error, allowTimeout bool) bool {
if err == nil {
return false
}
2017-08-31 15:22:47 +03:00
if IsRedisError(err) {
// #790
return IsReadOnlyError(err)
2016-07-02 15:52:10 +03:00
}
if allowTimeout {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
return false
}
}
return true
}
func IsMovedError(err error) (moved bool, ask bool, addr string) {
2017-08-31 15:22:47 +03:00
if !IsRedisError(err) {
2016-07-02 15:52:10 +03:00
return
}
s := err.Error()
2019-07-25 13:53:00 +03:00
switch {
case strings.HasPrefix(s, "MOVED "):
2016-07-02 15:52:10 +03:00
moved = true
2019-07-25 13:53:00 +03:00
case strings.HasPrefix(s, "ASK "):
2016-07-02 15:52:10 +03:00
ask = true
2019-07-25 13:53:00 +03:00
default:
2016-07-02 15:52:10 +03:00
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
}
func IsLoadingError(err error) bool {
2017-08-15 10:12:43 +03:00
return strings.HasPrefix(err.Error(), "LOADING ")
}
func IsReadOnlyError(err error) bool {
return strings.HasPrefix(err.Error(), "READONLY ")
}