redis/error.go

145 lines
2.8 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"
2016-07-02 15:52:10 +03:00
"io"
"net"
"strings"
2022-12-12 15:55:49 +03:00
"git.internal/re/redis/v8/internal/pool"
"git.internal/re/redis/v8/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
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, ""
}
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 ")
}
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
}