redis/error.go

160 lines
3.2 KiB
Go
Raw Permalink Normal View History

package redis
2016-07-02 15:52:10 +03:00
import (
2019-07-30 12:13:00 +03:00
"context"
"errors"
2016-07-02 15:52:10 +03:00
"io"
"net"
"strings"
"github.com/redis/go-redis/v9/internal"
2023-01-23 09:48:54 +03:00
"github.com/redis/go-redis/v9/internal/pool"
"github.com/redis/go-redis/v9/internal/proto"
2018-02-22 15:14:30 +03:00
)
2016-07-02 15:52:10 +03:00
// ErrClosed performs any operation on the closed client will return this error.
var ErrClosed = pool.ErrClosed
2022-11-17 17:18:04 +03:00
// HasErrorPrefix checks if the err is a Redis error and the message contains a prefix.
func HasErrorPrefix(err error, prefix string) bool {
var rErr Error
if !errors.As(err, &rErr) {
2022-11-17 17:18:04 +03:00
return false
}
msg := rErr.Error()
2022-11-17 17:18:04 +03:00
msg = strings.TrimPrefix(msg, "ERR ") // KVRocks adds such prefix
return strings.HasPrefix(msg, prefix)
}
type Error interface {
error
// RedisError is a no-op function but
// serves to distinguish types that are Redis
// errors from ordinary errors: a type is a
// Redis error if it has a RedisError method.
RedisError()
}
var _ Error = proto.RedisError("")
2020-07-24 14:57:12 +03:00
func shouldRetry(err error, retryTimeout bool) bool {
2019-07-30 12:13:00 +03:00
switch err {
2020-07-24 14:57:12 +03:00
case io.EOF, io.ErrUnexpectedEOF:
return true
2019-08-08 10:36:13 +03:00
case nil, context.Canceled, context.DeadlineExceeded:
2018-10-14 11:27:34 +03:00
return false
2018-08-23 16:13:42 +03:00
}
2020-07-24 14:57:12 +03:00
if v, ok := err.(timeoutError); ok {
if v.Timeout() {
2018-08-23 16:13:42 +03:00
return retryTimeout
}
return true
2017-08-31 15:22:47 +03:00
}
2019-08-02 00:59:53 +03:00
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
}
2020-11-12 12:47:04 +03:00
if strings.HasPrefix(s, "TRYAGAIN ") {
return true
}
2020-07-24 14:57:12 +03:00
2017-08-31 15:22:47 +03:00
return false
2016-07-02 15:52:10 +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, addr string) bool {
2020-12-06 12:05:42 +03:00
switch err {
case nil:
2016-07-02 15:52:10 +03:00
return false
2020-12-06 12:05:42 +03:00
case context.Canceled, context.DeadlineExceeded:
return true
2016-07-02 15:52:10 +03:00
}
2020-07-24 14:57:12 +03:00
if isRedisError(err) {
switch {
case isReadOnlyError(err):
// Close connections in read only state in case domain addr is used
// and domain resolves to a different Redis Server. See #790.
return true
case isMovedSameConnAddr(err, addr):
// Close connections when we are asked to move to the same addr
// of the connection. Force a DNS resolution when all connections
// of the pool are recycled
return true
default:
return false
}
2016-07-02 15:52:10 +03:00
}
2020-07-24 14:57:12 +03:00
2016-07-02 15:52:10 +03:00
if allowTimeout {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
2022-06-04 10:50:58 +03:00
return false
2016-07-02 15:52:10 +03:00
}
}
2020-07-24 14:57:12 +03:00
2016-07-02 15:52:10 +03:00
return true
}
func isMovedError(err error) (moved bool, ask bool, addr string) {
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, ""
}
2016-07-02 15:52:10 +03:00
addr = s[ind+1:]
addr = internal.GetAddr(addr)
2016-07-02 15:52:10 +03:00
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 ")
}
2020-07-24 14:57:12 +03:00
func isMovedSameConnAddr(err error, addr string) bool {
redisError := err.Error()
if !strings.HasPrefix(redisError, "MOVED ") {
return false
}
2021-10-04 18:20:29 +03:00
return strings.HasSuffix(redisError, " "+addr)
}
2020-07-24 14:57:12 +03:00
//------------------------------------------------------------------------------
type timeoutError interface {
Timeout() bool
}